Game Development Community

Variables?

by Rachel P · in Technical Issues · 08/01/2008 (4:51 am) · 4 replies

Hello again!

Well, I was able to get some more of my game done, but I am having trouble with variables right now. Here is exactly what I want to do...

On a collision with a particular object, I want to set the object's speed to a certain number, but ONLY if a certain variable is true. This is what I have for code so far (they are in my player.cs file which is set to load upon the game starting):

function playerClass::onLevelLoaded(%this, %scenegraph)
{
moveMap.bindCmd(keyboard, "w", "GoUp();", "GoUp();");
moveMap.bindCmd(keyboard, "s", "GoDown();", "GoDown();");

function GoUp(){
$varGoUp = 1;//true
}

}

function playerClass::onCollision(%srcObj, %dstObj, %srcRef, %dstRef, %time, %normal, %contactCount, %contacts)
{
if(%dstObj.class $= "wallClass") && $varGoUp = true;
{
$player.setLinearVelocityPolar(45, 100);
}
else
{$player.setLinearVelocityPolar(-45, 100);}
}

I'm doing my best to try to solve the problems I can on my own, but I have been trying all night with no luck. I have searched the TDN and the documentation/reference files that came with Torque Game Builder but I can't seem to find what it is that is making this not work. I have "Callback" checked on my player Collision list. Weird thing is, all Callback makes the player do is move all the way up to the ceiling and slide all the way left until it stops. Even if I delete the above code, if I have callback checked it still does that. But if callback is not checked, the rest of my collision settings work fine (but of course the above code is not taken into account).

Could someone people check the code above and if there are any glaring errors out there, let me know? I plan on using variables like this a lot in my game (ex, if player does somethingAND a certain variable is true, then something happens) so if I can just get the hang of how to program one of these functions correctly, it will help a whole lot :)

#1
08/01/2008 (7:28 am)
If(%dstObj.class $= "wallClass") && $varGoUp = true; <- Huh?

Shouldn't that be
if(%dstObj.class $= "wallClass" && $varGoUp = true)
#2
08/01/2008 (7:41 am)
Actaully that is close but what you meant was
if(%dstObj.class $= "wallClass" && $varGoUp $= true)

$varGoUp $= true is a test
$varGoUp = true is an assignment
#3
08/01/2008 (10:23 am)
There is also no inner-function support, as far as I'm aware (GoUp() is defined inside of playerClass::onLevelLoaded()). If you run the game and scroll up in the console, it will give you compilation errors for your scripts (look for the red text).

Also, a clarification: $= is used for checking strings, == is used for comparing boolean values (true/false). In this case, either will work; until you have a good grasp of the difference between the two operators, I would recommend using $= for equality-comparisons, as it will likely trip you up less than ==.
#4
08/01/2008 (11:40 pm)
Ah, thanks a bunch guys! I was able to get it work quickly after your advice! :) I fixed the script to what Anthony suggested, and also fixed a } that was in the wrong place in the function GoUp part which I see was a problem after looking in the console.

Thanks again!