• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
fourvector

Am I over engineering? A generalized Game Action framework

10 posts in this topic

I'm writing a roguelike in Java, using an entity-component architecture I put together. I want to develop a framework for defining game actions that entities can take, and can be taken on, for instance "pick up", "attack", "eat" etc.

My design followed from the premise that the definition of every action can be thought of as a triplet consisting of a subject, verb, and object. So for instance, Hand - pick up - item. So I defined three interfaces: [CODE]Action[/CODE], [CODE]CanAct<type extends Action>[/CODE], [CODE]CanBeActedUpon<type extends Action>[/CODE].
Then what I would do would be to define a new action class, such as
[CODE]Class PickUp implements Action{
PickUp(CanPickUp, CanBePickedUp){..}
}[/CODE], with two interfaces [CODE]CanPickUp extends CanAct<PickUp>[/CODE] and [CODE]CanBePickedUp extends CanBeActedUpon<PickUp>[/CODE]

The idea was that with this framework I could assign different components different CanAct interfaces, and then if an entity lost the component, for instance your hands are cut off, they would lose the ability to take actions, unless of course they had other components which also performed those actions. In addition, I could create menus of possible actions on the fly, since all I'd have to do was take the main entity, compile a list of all it's possible actions (for component in entity, if component instanceof CanAct, component.getType()), then look at it's surroundings for other entities with components that have CanBeActedUpon of that type. Meanwhile the action would provide it's own name, so the menu code could be completely oblivious.

The problem is that Java won't allow an object to inherit two interfaces from the same base interface. So for instance, if I add Hands, I want them to be able to PickUp, Equip, Attack, and Open. CanPickUp extends CanAct<PickUp>,CanEquip extends CanAct<Equip>. My Hands object can't implement both.

Additionally, it gets very messy for some reason. Because everything that handles these actions has to be type agnostic, I end up doing a lot of strange typecasting that I don't fully understand. For example:

[CODE]
public static CanAct<?> canActor(GameObject g, Class<? extends Action> actionType){
for(Part p : g.allParts){
if(p instanceof CanAct<?>){
CanAct<?> CA=(CanAct<?>)p;
if(CA.actionType()==actionType){
return CA;
}
}
}
return null;
}
[/CODE]

Where for this code I should point out that both the CanAct and CanBeActedUpon interface have the function actionType() which returns a Class<? extends Action>, and then CanPickUp for instance would return PickUp.class . I haven't been able to figure out how to do this in a more elegant way... Not to mention having to iterate through all the parts in the Entity.


I suppose my question is, does anyone have any suggestions for me? Am I over engineering my problem? Is there an easier approach? Is there a pattern I've not tried here? Or am I on the right track, and if so, what is my work around for implementing two of the same interfaces with different types?
0

Share this post


Link to post
Share on other sites
[quote name='fourvector' timestamp='1337202183' post='4940764']
The problem is that Java won't allow an object to inherit two interfaces from the same base interface.
[/quote]
That is very well possible - if I understand correctly what you mean.

The approach seems weird to me.
Using instanceof is sometimes a valid shortcut to working code, but using it in the first place is an indicator of something's going on wrong.
Does an action know what it can do ? The actor should know about it. Then, there is also a backwards dependency from Can* to Action.
It is hard to understand - which is a sign to redesign.
0

Share this post


Link to post
Share on other sites
ApochPiQ
I considered implementing it as data, but I wanted different CanAct and CanBeActedUpon objects to be able to modify the Action. So for instance this is PickUp:

[CODE]
public class PickUp implements Action{

CanPickUp subject;
CanBePickedUp object;

public PickUp(CanPickUp subject,CanBePickedUp object){
this.object=object;
this.subject=subject;
}

public void Act(){
//iff the subject can pickup
if(subject.isInReach(object) && subject.add(object)){
//remove the object from the region
object.removeFromWorld();
}
}


public String getName(){
return "Pick Up "+object.getGameObject().name;
}
}
[/CODE]

With the CanPickUp and CanBePickedUp interfaces having the functions implied here. Now different components can implement these interfaces in different ways. For instance a land mine, when it is "removeFromWorld()" might, in addition to leaving the world, also do a test to see if it explodes, or something to that effect. In general it makes sense to me that different nouns should modify the action. With a data implementation, I'd have to construct some kind of huge case structure to account for different functionality.

6510
Is using instanceof expensive, or is it just considered bad form? To me it seems like the right thing to do. I use it frequently for my event distribution system to my components, since it seems more type safe than having some kind of string as an ID, which could be tricked, for instance, I could send a RegionChanged event, any component listening for RegionChanged events would check the type and respond in the right way.
0

Share this post


Link to post
Share on other sites
[quote name='fourvector' timestamp='1337244535' post='4940877']
Is using instanceof expensive, or is it just considered bad form? To me it seems like the right thing to do. I use it frequently for my event distribution system to my components, since it seems more type safe than having some kind of string as an ID, which could be tricked, for instance, I could send a RegionChanged event, any component listening for RegionChanged events would check the type and respond in the right way.
[/quote]
Good or bad... it is a warning indicator of incomplete OOP design, of lacking polymorphism, most probably if used frequently.
When dealing with various kind of events, you would rather have different event handler implementations as well.
0

Share this post


Link to post
Share on other sites
[quote name='fourvector' timestamp='1337244535' post='4940877']
With a data implementation, I'd have to construct some kind of huge case structure to account for different functionality.[/quote]

Isn't that exactly the kind of structure you're going to wind up creating with this approach?


In my proposal, you implement your verbs as code, and everything else as data. You still get the ability to do things like your "pick up" example, you just eliminate a lot of boilerplate for all the nouns.

All actions derive from a base interface, which has a single method: Act. Act accepts two Noun parameters, a subject and an object. So you get something like this:

[code]if(Phrases.CanUsePhrase(SomeVerbTag, SomeSubject.GetNoun(), SomeObject.GetNoun()))
{
Verbs.Get(SomeVerbTag).Act(SomeSubject, SomeObject);
}
else
{
ShowUIMessage("I'm sorry Dave, I'm afraid I can't let you do that.");
}

// Elsewhere, we implement a verb, such as Pick Up:
void Act(Noun subject, Noun object)
{
if(subject.CanReach(object))
{
if(subject.Add(object))
{
World.Remove(object);
ShowUIMessage("You got the " + object.GetName());
}
}
else
{
ShowUIMessage("Almost... there...");
}
}
[/code]
0

Share this post


Link to post
Share on other sites
ApochPiQ,

Perhaps I am confused about what you mean by data. Data wouldn't have any functionality besides getters and setters and such. I would imagine here that the noun data would just be an enum, : {CanPickUp, CanBePickedUp, etc...}. But then in the example you've given, we have a function like subject.CanReach(object). How are these functions defined? Additionally, does every Noun need to have a CanReach(object) function, shouldn't only the ones that CanPickUp? I appreciate your help, I just feel like I must not be understanding something that you're trying to say.
0

Share this post


Link to post
Share on other sites
I'm still mulling over and playing with what you've said, ApochPiQ. I believe I understand and agree with what you've said about Data: turn CanPickUp into a piece of data instead of an interface, and you can move it around and use its functionality in a more versatile way.

I also like the idea of having Nouns as being a fundamental sort of object for storing these pieces of Data, because it closely mirrors the Entity-Component architecture.

I'm a bit confused about the distinction between a capability and a verb. It seems to me that in defining a capability, what you're really doing is implicitly defining a verb. This is how I understand what you're saying:

A capability has a perform() function, in which it does the thing which it is capable of doing, and a canPerform() function, in which it determines if it can do the thing it is capable of doing. In your example you include three capabilities:
PickUp, ContainerCapability, and ItemCapability.
In addition to their perform() and canPerform() functions, ContainerCapability and ItemCapability also have functions which are related to our understanding of how Containers and Items operate, such as Container.canFit(Item) or Item.getContainer(). I'll call functions of this type "the meat", they are what make the actual game behave in a specific fashion, as opposed to the bones, which is the scaffolding, like perform(). So capabilities hold the meat. This makes sense to me. After the meat has been defined in the capabilities, you can then construct a phrase list, with verbs being the linking up of two different capabilities. This linking defines the verb itself.

I attempted to implement this structure, but got stuck at what exactly the Container.perform() function should do. It seems as though the functionality of this function had been preempted by the PickUp.perform() function. It seems to me that Container.perform() should do what PickUp.perform() does, namely, cause an item to be contained by it. Here is where I realized that Container, in this scheme, implicitly defines a verb, namely "contain", through its perform() function. I would assume then that Item.perform() would do the exact same thing as Container.perform(), only maybe in a sort of converse way. We could really rename container and item to CanContain and CanBeContained, and the perform function on both of them does the same thing.

This implies that when we make our list of acceptable phrases, there's a natural verb that should be defined, namely contain, with the acceptable triplet being
[CanContain contain CanBeContained]

Which, when executed, does this: CanContain.perform(CanContain, CanBeContained), or CanBeContained.perform(CanContain, CanBeContained).

I suppose I'm stuck at this point. How might I define other verbs in my phrase list besides the natural verb implied by the perform() function? For instance, it makes sense that I should be able to remove an item from a container. But no combination of perform() calls from the two capabilities would implement that functionality. Do I create a CanRemove capability, with a perform() function that deals with the CanContain and CanBeContained capabilities, in a similar way to how the PickUp capability did before I subsumed it into my CanContain (Container) and CanBeContained (item) capabilities. Isn't this verging too closely on precisely what I was doing before?

I don't mean to be difficult, but I feel like I'm running around in circles in my head.


As an aside, I've been giving some thought to the kinds of functions I'd like to call on a general subject-verb-object structure. There are three groups:
[CODE]
Group A
boolean CanVerbNow(verb, subject, object);
boolean CanBeVerbedNow(verb, object, subject);
void do(verb, subject, object);
Group B
boolean CanVerb(verb, subject);
booean CanBeVerbed(verb,object);
Group C
set<Noun> getVerbed(verb, subject);
set<Noun> getVerbedBy(verb, object);
boolean isVerbed(verb, object);
boolean isVerbedBy(verb, subject)
[/CODE]

Group A contains the meat, these are the functions that actually care about what the verb DOES

Group B only really care about what the verb does on a conceptual design level, but not on an actual functional programming level. They guarantee that the noun does these things, but they don't care what these things are.

Group C don't care at all what the verb does, but instead only care about semantic relationships. I've realized group C are actually questions about the topology of a directed graph, where nodes are nouns, and verbs are edges. I think it's a very interesting observation with bearing on how I should engineer my code.

A little bit of googling indicates that this problem is being though about, but maybe not much for game programming: [url="http://en.wikipedia.org/wiki/Semantic_network"]http://en.wikipedia.org/wiki/Semantic_network[/url]
0

Share this post


Link to post
Share on other sites
Consider: there is no such thing as "remove."

There is "take item from container A and put it in container B." That's it. A and B might be very different [i]sorts[/i] of container (a dungeon floor and a backpack, for instance) but that's the only relationship you ever need to express: take it from one, put it in another.


Beyond that... I would encourage you to limit your noun/verb thinking to [i]only things the player or AI can do[/i]. Supplemental stuff like spawning creatures, consuming items from your inventory, etc. should be hard-coded game logic. Otherwise you end up needing conditional clauses, or prepositional phrases ("consume Wand of Summoning from Backpack") or whatever else, and sooner or later you're on the road to implementing semantic networks and such which is massive overkill for a roguelike.


I respect that you want to engineer something generic, robust, and so forth, but be cautioned that this is a slippery slope. Premature generalization is a serious design sin. Design something that does [i]what you need it to do[/i], not something that does everything under the sun. Not all of your implementation detail has to be perfectly generalized and elegant and reusable and whatever else; as long as your one-off hard-coded stuff is genuinely one-off and not systematically duplicated all over the code base, you should be fine.
0

Share this post


Link to post
Share on other sites
Hi ApochPiQ. I've taken your very last piece of advice and decided to do away with a generalized action framework. I've realized it's more important to write a specific piece of code to handle picking things up than a framework for doing EVERYTHING. At least for now, maybe when I've written some specific pieces of code for different actions I might more easily be able to recognize a common theme among the various actions. Edited by fourvector
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0