Jump to content
  • Advertisement
Sign in to follow this  
TechRogue

Overriding class behavior

This topic is 2307 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've been familiarizing myself with Angelscript over the last few weeks, and I've started hooking it up to the classes in my game engine. I think I have a good grasp on the API / way of doing things, but I'm sure there are some simpe things I've overlooked. Please bear with me!

My goal is to have a base Object class, and use Angelscript to define individual behaviors. I'll add instances of the Object class to an object manager, which will update them by calling an update() function. In a previous project, I implemented this by extending the Object class and overriding the update(), render() etc functions. Since Angelscript doesn't allow script classes to extend C++ classes, this tactic is out.

I saw this video demonstrating the scripting system in Overgrowth, and it seems to do exactly what I need it to. The script can be seen here:

http://www.youtube.c...xnm5qscA#t=161s

I'm confused as to how this works. The functions clearly refer to 'this', even though they aren't defined as member method. Is there some way to call a function from within the context of a class instance? Or is Angelscript like Javascript, where 'this' refers to the calling object, instead of the owner of the function?

Share this post


Link to post
Share on other sites
Advertisement
I have seen that...was hoping to avoid doing things that way though. The way Wolfire is doing things seems really nice, and would also allow me to reload the script without having to create new instances or restart the game.

Share this post


Link to post
Share on other sites
They may be doing some funny script preprocessing (like putting the definition 'inside' an object before compiling) in order for that to work. Offhand, I think 'this' *MAY* also be a pseudotoken that can be used as a variable identifier even outside an object-- you could theoretically make a global variable named 'this' that returns some sort of application-defined marshalling type and then just call methods on that as if it were the proverbial real thing.

Either way, I would advise running the hell away from a design created by Wolfire, as Lugaru is basically a case study in bad design decisions and unmaintainable code. You're also sacrificing some neat stuff like inheritance if you go the 'fake object' route.

EDIT: Though, to be fair, the preprocessing can be used for some really cool stuff. I've put together something very akin to C#'s attributes concept using entirely pre-existing language and add-on functionality.

Share this post


Link to post
Share on other sites
I was afraid there might have been some preprocessing magic going on. Looks like I may have to go the composition inheritance / delegation route. Thanks for the help (and thanks for that thread / source code, SiCrane).

Share this post


Link to post
Share on other sites
'this' is not a reserved keyword, and can be used to declare global / local variables.

For script class methods, 'this' do refer to the owner of the function, unless a local variable has been declared that overrides that.

From the video it's not clear what 'this' refers to. It really depends on how Wolfire has integrated AngelScript. It is possible that they use pre-processing to place all the code inside a script class, which would make 'this' refer to the script class. Another possibility is that they have registered a global property called 'this' that they use to point to the in-engine object that is being controlled by the script.

@InvalidPointer: I don't think it is fair to advice to stay away from any design made by Wolfire. Lugaru, is a quite old game (released in 2005). I bet the Wolfire team has learned a lot since then, and they are probably making a lot better design decisions when writing Overgrowth.

Share this post


Link to post
Share on other sites

@InvalidPointer: I don't think it is fair to advice to stay away from any design made by Wolfire. Lugaru, is a quite old game (released in 2005). I bet the Wolfire team has learned a lot since then, and they are probably making a lot better design decisions when writing Overgrowth.


Perhaps a bit extreme, yes, but Lugaru is (as a random sample) pretty bad. Perhaps just exercise caution?

Share this post


Link to post
Share on other sites
I asked David Rosen on Twitter and he says he uses a global variable called 'this' to refer to the object in question. My first instinct would be that he's creating a script context for every scriptable object, but that seems like it could be wasteful in terms of memory.


Perhaps a bit extreme, yes, but Lugaru is (as a random sample) pretty bad. Perhaps just exercise caution?


Good grief, that's quite horrible. I'll certainly be careful. Most of the design and technical tips I'm taking are from recent blog posts, though, and there's little source code to be seen. I guess I'll just have to make my own dumb design mistakes. ;)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!