Passing TorqueScript array using DECLARE_CALLBACK
by Nathan Bowhay - ESAL · in Torque 3D Professional · 10/08/2010 (11:02 pm) · 5 replies
I was wondering if there is a way to use DECLARE_CALLBACK to pass a TorqueScript array. In theory I can do it like this:
In theory this would work cause TorqueScript arrays are just variables (haven't tested this).
My main question is if there is or will be in the future, an easier way of doing this? At least in engine using the macro. Even if I had to still define all the args in script it would be nice to not have to in engine as well, that way the engine code is more future proof.
Yes I could pass a char* that is space delimited and then use getWord, but this isn't as efficient and easy. The other known option is to use an array object or something like it to add things to then pass the object ptr to script, but again you are constructing an object then destroying it so this is a bit of work as well (unless you reuse it).
If anyone has any ideas let me know. the main thing is some tricky way of using the engine macros.
DECLARE_CALLBACK(void, foo, (S32 size, F64 data0, F64 data1, F64 data2, F64 data3, F64 data4) );then in script I could have this:
function foo(%size, %data0, %data1, %data2, %data3, %data4)
{
for(%i = 0; %i < %size; %i++)
{
echo("Array[" @ %i @ "] = " @ %data[%i]));
}
}In theory this would work cause TorqueScript arrays are just variables (haven't tested this).
My main question is if there is or will be in the future, an easier way of doing this? At least in engine using the macro. Even if I had to still define all the args in script it would be nice to not have to in engine as well, that way the engine code is more future proof.
Yes I could pass a char* that is space delimited and then use getWord, but this isn't as efficient and easy. The other known option is to use an array object or something like it to add things to then pass the object ptr to script, but again you are constructing an object then destroying it so this is a bit of work as well (unless you reuse it).
If anyone has any ideas let me know. the main thing is some tricky way of using the engine macros.
#2
The main thing I am wondering about is the torque script arrays specifically:
10/09/2010 (12:09 am)
Yeah that was the 2nd thing I knew about:Quote:
The other known option is to use an array object or something like it to add things to then pass the object ptr to script, but again you are constructing an object then destroying it so this is a bit of work as well (unless you reuse it).
The main thing I am wondering about is the torque script arrays specifically:
%myArray[0]= 1; %myArray[1]= 2; %myArray[2]= 3; echo(%myArray1); // Prints 2 echo(%myArray[1]); // Also prints 2 %i = 1; echo(%myArray[%i]); // Also prints 2
#3
I reconstruct many thousands or even millions of these with no problems.
At last glance in our test environment about 500,000 aiplayers had been spawned and they all use these ArrayObject to do their wandering and path finding. That server was not keeping pathing stats so I don't know just how many of these objects were created, however there were no issues.
I always .delete the ArrayObject when the path is done, could probably allocate and reuse this but its just simpler to delete and make new ones right now.
I'm not sure if its a acceptable approach to you but it seems to work great for us even with many being created and destroyed.
10/09/2010 (12:20 am)
I see, did not read that part.I reconstruct many thousands or even millions of these with no problems.
At last glance in our test environment about 500,000 aiplayers had been spawned and they all use these ArrayObject to do their wandering and path finding. That server was not keeping pathing stats so I don't know just how many of these objects were created, however there were no issues.
I always .delete the ArrayObject when the path is done, could probably allocate and reuse this but its just simpler to delete and make new ones right now.
I'm not sure if its a acceptable approach to you but it seems to work great for us even with many being created and destroyed.
#4
10/09/2010 (12:21 am)
From what I understand the torquescript array modifier just creates a new local variable as {VariableName}{Index}={Value}
#5
That is the main reason I was focused on efficiency. If I reused an array object it probably wouldn't be that, bad.
Like I said the main thing is just curiosity.
10/09/2010 (12:33 am)
Yeah it does, that is why I figured there should be an easy way to do this in the macro. Not really a big deal just wondering. Oh and just give you an idea of where I am doing this. It is a function called in engine whenever a packet comes in so it can be used in script. The position packets are sent at 100/s and the command probably around 10/s. So pretty rapidly granted torque doesn't have time to look at all 100 so at some point they are dropped or something. We also have some code to ignore a bunch of them so the frame-rate doesn't die.That is the main reason I was focused on efficiency. If I reused an array object it probably wouldn't be that, bad.
Like I said the main thing is just curiosity.
Torque 3D Owner Chris
I pass back path ArrayObjects back to TorqueScript like this. Just sending the S32 value back into TorqueScript
S32 MeshPath::ToConsoleObject() { ArrayObject *co = new ArrayObject(); vector<Point3F>::iterator iter; for(iter = m_Path.begin(); iter != m_Path.end();) { char temp[255]; dSprintf(temp, 255, "%f %f %f", (*iter).x, (*iter).y, (*iter).z); co->push_back(temp, temp); iter++; } co->registerObject(); return co->getId(); }for(%i=0;%i<%this.NavMeshPath.count();%i++) { %this.Log("Marking : " @ %this.NavMeshPath.getKey(%i)); %this.Debug_Mark(%this.NavMeshPath.getKey(%i)); }