Brainstorming: How to make Goals scriptable?

Brainstorming: How to make Goals scriptable?

Postby StefanP.MUC » 22 Mar 2012, 18:42

Because I needed a break from the annoying and problematic AttackableObject refactoring I started to think a bit about how to make the Goals scriptable. I think Goals are the first thing that should be made scriptable because they are the most "separate" and smallest objects (I think they even can be made scriptable without having the refactoring task done), so it's easy to test how extended scripting works before we start making more complex things like AI scriptable.
IMHO, the current way of hardcoding tons of Goal subclasses/types and having endless #include lists of Goal header files is the wrong way to go. This totally cancels out possibility to add user defined Goals and do fine-tuning.

I thought of it this way:
Instead of giving each goal an own class that inherits from Goal, we should have only one class Goal that provides some hardcoded features like which player does it belong to, a name, a win/fail message, isMet() and so on. They will basically be getter methods for some simple variables so that other parts of the game are able to find out the state of this goal. The values that these getter methods return are set by Angelscript. Up until here everything sounds fine, I believe.

The specific conditions and checks for each Goal then must be implemented with Angelscript. Still fine.

Now the the problem I currently see with this way:
Our scripts can then instantiate Goals and set their messages, but the script can not inherit from C++ classes nor can it implement virtual functions. The script has to have a script-global functions for each individual Goal. This means that the script needs some kind of table that associates the individual Goal with some checkIfMet() function. This adds complexity I was hoping to avoid. This is not ideal, but I currently can't think of a better way, hence the brainstorming.

An alternative way could be to completly rewrite the Goal class in Angelscript and drop all C++ Goal code. This way we could store objects of types "asIScriptObject" in the C++ code that represent the scripted Goals. Then we could use the full advantages of inheritance within AS. I have no experience with this way, but it looks to me that this is a way that quickly can get buggy and hard to debug and maybe also slow. (another argument could be: why have a C++ program if the only purpose is to start a script? Then we could just use JavaScript and a browser to wrote the game... :D )

Probably the best but also assumingly hardest way would be to extend the C++ Goal class in a complex way so that it's possible for the script do define individual Goals without inheritances and C++ adjustments. But I'm absoluetly not sure where we had to start this way and how hard it would be.

Here are some question to be answered:
What's the best way here to go?
Any completly different approaches?
What parts of Goal should(n't) / can('t) / must(n't) be hardcoded/scripted?
Any other ideas about Goals in general?
StefanP.MUC
 

Re: Brainstorming: How to make Goals scriptable?

Postby oln » 23 Mar 2012, 10:25

As I suggested in the other thread, maybe we should have an event system (I've seen similar in other games), that fires events whenever things a creature dies, takes damage, etc. Then you can make the goal "subscribe" to the event, and be notified whenever something related to it has happened. Whenever this event is fired, the event processor notifies the goal object, which can then check if the conditions are met based on the information in the event. This makes the scripting more general, not just purely for making goals to finish the level. There could also be some hardcoded often used conditions, so you can e.g set the script to activate on a certain creature dying, or a certain amount of gold met.

I worked with some minecraft server modding last year, and the modding API worked in a similar way (though with Java and not a scripting language). When I looked at some other FOSS rts games it seems like they use a similar system.
User avatar
oln
 
Posts: 1020
Joined: 26 Oct 2010, 22:16
Location: Norway

Re: Brainstorming: How to make Goals scriptable?

Postby StefanP.MUC » 23 Mar 2012, 11:09

Ah, you mean something like the Observer pattern (https://en.wikipedia.org/wiki/Observer_pattern)? This sound indeed like a good idea.
StefanP.MUC
 

Who is online

Users browsing this forum: No registered users and 0 guests