Advertisement

Overriding class behavior

Started by April 17, 2012 05:41 PM
6 comments, last by TechRogue 12 years, 7 months ago
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?
It is possible to fake inheriting from a C++ class in AngelScript. See this thread.
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.
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.
clb: At the end of 2012, the positions of jupiter, saturn, mercury, and deimos are aligned so as to cause a denormalized flush-to-zero bug when computing earth's gravitational force, slinging it to the sun.
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).
'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.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Advertisement

@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?
clb: At the end of 2012, the positions of jupiter, saturn, mercury, and deimos are aligned so as to cause a denormalized flush-to-zero bug when computing earth's gravitational force, slinging it to the sun.
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. ;)

This topic is closed to new replies.

Advertisement