Scripting / GUI command issue
by James Steele · in Torque Game Engine · 05/18/2006 (3:12 am) · 4 replies
The following has more to do with my lack of udnerstanding of Troque Script than anything else, so please don't hurt me for being ignorant ;)
I have a set of GUI radio buttons which are organised in a group. For each button I have set the command to be something along the lines of
UnitSelect_ButtonTurrets is the name of a button in the dialog.
In my UnitSelectGui.cs file, I have a script function along the lines of the following code;
The problem that I'm running into is that when the script loads and compiles, I get an error that tells me "statement always results in 0": or something along those lines. Essentialy, the compiler is a bit confused about the first if statement in the function and bails out.
I can get around this problem by having a seperate command for each button, but that sort of offends my coding sensibilties. It leads to repeated code, which can lead to bugs and just pure evilness.
What occurs to me is that the if statement is conceptualy wrong. I'm not comparing pointers. I'm comparing instances and so ofcourse the statement will always be false (the instances are not the same object*doh*). So how do I determine if a variable references a specific/known object?
I have a set of GUI radio buttons which are organised in a group. For each button I have set the command to be something along the lines of
UnitSelectGui::OnUnitFilterButton(UnitSelect_ButtonTurrets);
UnitSelect_ButtonTurrets is the name of a button in the dialog.
In my UnitSelectGui.cs file, I have a script function along the lines of the following code;
function UnitSelectGui::OnUnitFilterButton(%button)
{
if (%button == UnitSelect_ButtonTurrets)
{
// Do something
}
else if (%button == UnitSelect_ButtonLightVehicles)
{
// Do something else
}
// Do something that applies to all conditions
}The problem that I'm running into is that when the script loads and compiles, I get an error that tells me "statement always results in 0": or something along those lines. Essentialy, the compiler is a bit confused about the first if statement in the function and bails out.
I can get around this problem by having a seperate command for each button, but that sort of offends my coding sensibilties. It leads to repeated code, which can lead to bugs and just pure evilness.
What occurs to me is that the if statement is conceptualy wrong. I'm not comparing pointers. I'm comparing instances and so ofcourse the statement will always be false (the instances are not the same object*doh*). So how do I determine if a variable references a specific/known object?
About the author
#2
05/18/2006 (5:20 am)
That would work for now, but not if/when the strings for the GUI are localised into other languages.
#3
Or you could just call different functions depending on the button pressed, e.g. use commands like:
If you're still stuck, try the following.
On your buttons, use commands like :
UnitSelect_ButtonTurrets
UnitSelect_ButtonLightVehicles
...etc.
Then in the filter button code, you can do this :
05/18/2006 (5:30 am)
Quite a few solutions to this problem. Perhaps you could try comparing the object id's instead of the names, e.g.if (%button.getId() == UnitSelect_ButtonTurrets.getId())
Or you could just call different functions depending on the button pressed, e.g. use commands like:
UnitSelectGui::OnUnitFilterButton_Turrets();
If you're still stuck, try the following.
On your buttons, use commands like :
UnitSelect_ButtonTurrets
UnitSelectGui::OnUnitFilterButton(0);
UnitSelect_ButtonLightVehicles
UnitSelectGui::OnUnitFilterButton(1);
...etc.
Then in the filter button code, you can do this :
function UnitSelectGui::OnUnitFilterButton(%button)
{
if (%button == 0) // UnitSelect_ButtonTurrets
{
// Do something
}
else if (%button == 1) // UnitSelect_ButtonLightVehicles
{
// Do something else
}
// ...etc... (or use a the switch statement)
// Do something that applies to all conditions
}(Might be faster and more generic than a string compare)
#4
I have already gone down seperate functions rotue, but as I mentioned earlier; it leads to duplicated code and this is just an invitation to introduce bugs, especially if I use the same work-around for other areas of script code.
I would rather hit the nail on the head now, instead of making painful mistakes later on.
Passing an integer in the argument or comparing the object's ID seems a much better way to go about it though. I'll give that a go when I get home this evening.
05/18/2006 (5:45 am)
Thanks James (oo-er...bit of a weird moment there, addressing somebody with the same name as me).I have already gone down seperate functions rotue, but as I mentioned earlier; it leads to duplicated code and this is just an invitation to introduce bugs, especially if I use the same work-around for other areas of script code.
I would rather hit the nail on the head now, instead of making painful mistakes later on.
Passing an integer in the argument or comparing the object's ID seems a much better way to go about it though. I'll give that a go when I get home this evening.
Torque 3D Owner Jesse (Midhir) Liles
if (%button.getText $= "Turrets") { // Do Something } else if (%button.getText $= "Light Vehicles") { // Do Something }would work?