Wintermute Engine Forum

Wintermute Engine => Technical forum => Topic started by: greg on January 06, 2007, 01:03:28 AM

Title: Accessing an Object's Member Variable
Post by: greg on January 06, 2007, 01:03:28 AM
Hi All,

How can I access an object's member variable from a non-member function?

Within the game daemon script, I'm trying to access a member variable of the active object:

== my_object.script ==

...
var myState = "foo";
...

== game_daemon.script ==

...
var active_object = Game.GetActiveObject();
var active_object_state = active_object.myState;
Game.Msg(active_object_state); // this prints [null]
...

So far, the only way I can get this to work is by creating a GetMyState() method in my_object.script that returns myState and then calling active_object.GetMyState() from game_daemon.script.

Do I need to write a wrapper method for every variable I need to access from a non-member function?  If so, is there a way to put the method in a script attached to the object (via this.AttachScript()), so that I don't need to copy the wrapper methods into every instance of the object?  (In my testing, calling Game.Msg(this.myState) from the attached script also displays [null].)

Thanks,
Greg
Title: Re: Accessing an Object's Member Variable
Post by: metamorphium on January 06, 2007, 02:10:30 AM
correct way to do this is:

== my_object.script ==

...
this.myState = "foo"; // note that since it's a member declaration you don't precede it with var.
...


Hope this helps.