Need help with the dSprintf/executef commands
by Daniel Hopkins · in Torque Game Engine · 09/10/2008 (4:03 pm) · 4 replies
First, the code I have:
////////////////////////////////////////////////////////////////
char buf[3][32];
dSprintf(buf[0], 32, "%d", event.modifier);
dSprintf(buf[1], 32, "%d %d %d", event.vec.x, event.vec.y,event.vec.z);
dSprintf(buf[2], 32, "%d %d %d", event.pos.x, event.pos.y,event.pos.z);
Con::executef(this, 4, name, buf[0], buf[1], buf[2]);
////////////////////////////////////////////////////////////////
The 'name' and 'event' variables are defined previous to this code block, so no problems there.
The problem is that the 'event.vec.x/y/z' and 'event.pos.x/y/z' variables are floats and I don't think it's exporting them properly. For example, the 'event.vec' is a 3D vector which has been normalized, but the values passed to the script function, which I print to the console via echo(), are anywhere from -534543543 to 12543535--far from normalized values. So, I figured it's got to be my lack of knowledge of using the dSprintf() and/or Con::execute() functions.
Can anyone fix the above code so that it will pass the values correctly?
Thanks
////////////////////////////////////////////////////////////////
char buf[3][32];
dSprintf(buf[0], 32, "%d", event.modifier);
dSprintf(buf[1], 32, "%d %d %d", event.vec.x, event.vec.y,event.vec.z);
dSprintf(buf[2], 32, "%d %d %d", event.pos.x, event.pos.y,event.pos.z);
Con::executef(this, 4, name, buf[0], buf[1], buf[2]);
////////////////////////////////////////////////////////////////
The 'name' and 'event' variables are defined previous to this code block, so no problems there.
The problem is that the 'event.vec.x/y/z' and 'event.pos.x/y/z' variables are floats and I don't think it's exporting them properly. For example, the 'event.vec' is a 3D vector which has been normalized, but the values passed to the script function, which I print to the console via echo(), are anywhere from -534543543 to 12543535--far from normalized values. So, I figured it's got to be my lack of knowledge of using the dSprintf() and/or Con::execute() functions.
Can anyone fix the above code so that it will pass the values correctly?
Thanks
#2
*quote*"All the cool kids do this..."
Be aware that the code is a modified Torque source code block--I guess the guys at GG aren't cool kids :)
09/10/2008 (5:23 pm)
Thanks a lot!! I'll try it out.*quote*"All the cool kids do this..."
Be aware that the code is a modified Torque source code block--I guess the guys at GG aren't cool kids :)
#3
09/10/2008 (5:32 pm)
Nevermind, misread what you were doing.
#4
09/10/2008 (5:34 pm)
@Gary: Works like a charm!! Thank you!!
Torque Owner Gary "ChunkyKs" Briggs
1) All the cool kids do this:
dSprintf(buf[0], sizeof(buf[0]), ... )
etc so that when someone else comes along in future and changes the declaration do char buf[3][16], it won't crash or cause security holes, or when they change it to char buf[3][1024], it'll do what they want.
2) If they're floats, you probably want %f in your conversion string instead of %d
3) I would guess that you should be able to pass:
Con::executef(this, 4, name, event.modifier, event.vec, event.pos);
And if you can't, the Correct and Good Thing To Do is to fix it so that passing what's presumably a Vector3 as one of those args does the right thing, which IIRC is actually quite easy to do if you find the right place in the torque source.
Gary (-;