Game Development Community

Having a terrible time trying to get code to repeat

by Isaac Dutton · in Technical Issues · 03/30/2006 (6:14 pm) · 18 replies

I resently just posted this in general, thought I was here in programming so with a request to delete the other thread I will repost here!

I have been trying to figure out how to get my function to repeat as long as the key is held, its driving me crazy trying to figure this out, so with that here is my code any help would be greatly wanted...

My function is as follows

function TurnLeft(%val) //enter function with 1
{
while(%val==1) {
$mvYaw -= %val * ($cameraFov / 90) * 0.02; //adjust yaw
} else {
// Key up
}
}


and my bind

moveMap.bind(keyboard, "a", turnLeft);

Thanks in advance guys!

#1
03/30/2006 (6:21 pm)
That doesn't look like it will repeat. It looks like it will go into an endless loop! What are you trying to accomplish?
#2
03/30/2006 (6:24 pm)
No it dosnt go into an endless loop, I thought it would when I first wrote it but I tried it anyways....

What i am trying to do is make the player rotate while the button is held.... It rotates fine but I have to keep pressing it over and over.

I have searched alot and found very little to help me with this problem, it seems to be common but I could not get my hands on a solution.
#3
03/30/2006 (7:49 pm)
Well, it *should* go into an endless loop - the "while" will never exit. (perhaps the script system shortcircuits the loop - who knows). All it's doing is constantly changing a variable. Unless the code exits, the scene won't get drawn again. And even if it does exit, it will only have the last value of $mvYaw.

Anyway, the default behavior is to turn constantly while the button is down. Thats why it sets the "mvYawLeftSpeed" to either 0, or a value (depending on if the key is pressed when the function is called). It gets called first on down, and sets the speed to 0.1 (the default value of $Pref::Input::KeyboardTurnSpeed), and then gets called again on Key Up, to reset the speed to 0. mvYawLeftSpeed is used internally to rotate the character.

If you're wanting to emulate the default behavior, you'll need to do something similar - keep a global variable, set it in the KeyDown, and clear it in the KeyUp. And then process each frame, tick, or event (depending on how you code it) to update the Yaw based on the gloal variable.
#4
03/30/2006 (8:04 pm)
I have done this before and it was much easier, I just cant remember how...
#5
03/30/2006 (8:15 pm)
I am not sure a while is a good idea. You might look into having it schedule a function and then assigning %val to a global variable like $Abuttondown and then stopping the schedule when it is false.
#6
03/30/2006 (8:20 pm)
Might as well write it out, I have time.
function startTurnLeft(%val) //enter function with 1
{
$Abuttondown = %val
if (%val)
schedule( 250, 0,  TurnLeft);
}
function TurnLeft()
{
if ($Abuttondown){
$mvYaw -= ($cameraFov / 90) * 0.02; //adjust yaw
schedule( 250, 0,  TurnLeft);
}
}

moveMap.bind(keyboard, "a", startturnLeft);
that may do it
#7
03/30/2006 (8:42 pm)
Hmm, when I map this it dosnt work, it calls the function (or at least I feel it does because I dont get any errors)
though after I cleaned up the code and call the function from the console it works fine...
#8
03/30/2006 (8:48 pm)
I've never seen a "while .. else" before.. nice!
#9
03/30/2006 (8:51 pm)
Ya, was suppose to be if, been playing around with it..... It hates me, but seriously if you cant be helpfull I dont see a point in posting, I relise my mistake, thank you for pointing it out to me again..
#10
03/30/2006 (9:02 pm)
Try
moveMap.bind(keyboard, "a", startTurnLeft);

can't remember if its a stickler on those capitols and add it to config.cs also. then add an echo into start turn left to see if it working.
#11
03/30/2006 (9:06 pm)
Way ahead of ya, and its a no go...
#12
03/30/2006 (9:13 pm)
So you can call it from the console but not the map? That obviously means sumething is up with the map the only things that come to mind are

A. its not calling it

B. %val is messed up.

neither seem likely but I would check them with an echo. if I get a moment I'll try this on my computer.
#13
03/30/2006 (9:15 pm)
I already tried checking them with an echo, it echos if I call from the console, but not if I try it from the map, however, if I comment out the function it gives me an error that it cant find it, so its finding the function and everything, I feel though that %val isnt being set to true...
#14
03/30/2006 (9:31 pm)
$Abuttondown = %val;

i missed a semi colon on the above line did you catch that?
#15
03/30/2006 (9:34 pm)
Yeppers this is what I am using now

function startTurnLeft(%val) //enter function with 1
{
$Abuttondown = %val;
if (%val){
schedule( 250, 0, TurnLeft);
}
}

function TurnLeft()
{
if ($Abuttondown){
$mvYaw -= ($cameraFov / 90) * 0.02; //adjust yaw
schedule( 250, 0, TurnLeft);
}
}
#16
03/30/2006 (9:43 pm)
Alright, I am going to bed now, ill take a closer look at it in the morning
#17
03/30/2006 (9:51 pm)
Got it to work

function startTurnLeft(%val) //enter function with 1
{
$Abuttondown = %val;
if (%val){
schedule( 100, 0,  "doTurnLeft");
doTurnLeft();
}
}
function doTurnLeft()
{
   echo($Abuttondown);
if ($Abuttondown){
$mvYaw -= 0.1; //adjust yaw
schedule( 250, 0,  "doTurnLeft");
}
}

changed turnleft to do turn left because I had an old turnleft function interfering. And I well go to bed also.
#18
03/31/2006 (8:54 am)
That did it thanks!!!!