Creating member variable client side in script
by Dumbledore · in Torque Game Engine · 07/27/2008 (9:48 am) · 4 replies
How come if you create a member variable on the server:
You can then access it like this:
But if I try this on the client:
Note: there are a few differences between the code on the server that works as expected, and the code on the client which does not. Here's a list of differences that I can come up with:
- The server creates the member variable within the scope of the object from within the datablock onAdd() method, the client creates the member variable from within the scope of the object itself (But this shouldn't matter because %obj is the object and it shouldn't matter in what scope we are in when we add it.)
- The client accesses the script created member variable within the same namespace that it created it. Maybe this has something to do with it as on the client when I try to access it with someFunction(), someFunction is not within the 'Object' namespace.
Can anyone shed light on why it isn't working on the client?
function Datablock::onAdd(%this, %obj)
{
%obj.myNewMemberVariable = "Hello";
}You can then access it like this:
function Datablock::someRandomMethod(%this, %obj)
{
echo(%obj.myNewMemberVariable);
}
// output >>
HelloBut if I try this on the client:
Object::onAdd(%this)
{
%this.myNewMemberVariable = "Hello";
echo("onAdd ->" SPC %this.myNewMemberVariable);
}
function someFunction(%theObject)
{
echo("someFunction ->" SPC %theObject.myNewMemberVariable);
}
//output>>
onAdd -> Hello
someFunction ->Note: there are a few differences between the code on the server that works as expected, and the code on the client which does not. Here's a list of differences that I can come up with:
- The server creates the member variable within the scope of the object from within the datablock onAdd() method, the client creates the member variable from within the scope of the object itself (But this shouldn't matter because %obj is the object and it shouldn't matter in what scope we are in when we add it.)
- The client accesses the script created member variable within the same namespace that it created it. Maybe this has something to do with it as on the client when I try to access it with someFunction(), someFunction is not within the 'Object' namespace.
Can anyone shed light on why it isn't working on the client?
#2
07/27/2008 (10:30 am)
Object::onAdd(%this)
{
%this.myNewMemberVariable = "Hello";
echo("onAdd ->" SPC %this.myNewMemberVariable);
}Needs to be[b]function [/b]Object::onAdd(%this)
{
%this.myNewMemberVariable = "Hello";
echo("onAdd ->" SPC %this.myNewMemberVariable);
}That "function" there is necessary for defining a function.
#3
07/27/2008 (11:59 am)
The code above is just contrived to show what I am trying to do. I gather that what I am doing is possible then? In short, I am adding a field %obj.myField in script and then calling it in a different function. For some reason the dynamic field doesn't seem to work in the other function, but I have verified that it works correctly in the function where I add it.
#4
07/27/2008 (12:04 pm)
Okay yes it does work. I had a misnamed variable that caused the problem.
Associate James Ford
Sickhead Games
Are you sure Object::onAdd is getting called? Are you sure that the object passed into the function is really the same object?