How to call subclassed objects stored in dictionary?

Started by
2 comments, last by 93i 11 years, 8 months ago
Hi there, i am trying to build a state system with AngelScript, here it is:


class GGameState {
string name;
GGameState() {
name = "";
}
GGameState(string nm) {
name = nm;
}
void update(float t) {
print("update in base class");
}
}

class GStateManager {
dictionary states;
string currentState;
void addState(object@ st, bool makeCurrent = true) {
states.set(st.name, st);
if (makeCurrent) {
currentState = st.name;
}
}
void update(float t) {
GGameState @state;
if (states.get(currentState, @state)) {
state.update(t);
} else {
print("notfound "+currentState);
}
}
}



Now if i execute the following code:



class MyState : GGameState
{
MyState(string nm) {
super(nm);
}
void update(float t) {
print("update in state");
}
}
GStateManager @man = GStateManager();
MyState @mainState = MyState("aggi");
man.addState(mainState);
man.update(0.1);


I get "update in base class", not as i would expect (and want) "update in state"

How can i achieve this in angelscript?

Thanx
Advertisement
this prints "child" you want

class Base
{
void update()
{
dbg("base");
}
};
class Child : Base
{
void update()
{
dbg("child");
}
};

int main()
{
array<Base@> bases;
Child @c = Child();
bases.insertLast(c);
bases[0].update();
}


methods are already virtual in angelscript.

but for virtual call to work, you gotta save them as handles to them.
not copy them in each call.
When updating the dictionary to set the state object you should pass the object by handle, and not by value, i.e.


void addState(object@ st, bool makeCurrent = true) {
states.set(st.name, @st); // <-- Make sure to pass the object by handle
if (makeCurrent) {
currentState = st.name;
}
}


Regards,
Andreas

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

thanx, those references gonna bug me a lot, thats for sure ;-)

This topic is closed to new replies.

Advertisement