View Full Version : Empty loop, how to avoid them?
I'm making a monkey thiever script, and making sure that it can escape the prison by itself.
LockPickJailDoor picklock the door and make sure that it successfully picklocked it with various fail safe. I want it to try again if it failled (which is why there is a loop there. A simple if statement isn't enough.
MarkTime(t);
repeat
until (TimeFromMark(t) > 6000) or LockPickJailDoor;
Now as you can see, this code compiles, it works, but it does alot of instruction. It waste ressources. My bad, it doesn't, because it waits for the return value of LockPickJailDoor to continue looping.
I know some of you will tell me: Put a wait in there like this. Problem solved!
MarkTime(t);
repeat
Wait(1000 + Random(500));
until (TimeFromMark(t) > 6000) or LockPickJailDoor;
But no... that's not fixing a problem, thats just a work around in my opinion. I want a real solution.
My programming teacher would burn me at the stake if I gave him a solution like this.
Is there a way to avoid this problem?
Right now I'm thinking about adding recursivity in the LockPickJailDoor function so that when it fail to lockpick, we give up and logout, or if it is succesfull, we proceed with escaping the prison.
Kyle Undefined
03-19-2012, 09:50 PM
Or something like this:
MarkTime(t);
while(not(LockPickJailDoor))do
begin
if(TimeFromMark(t) > 6000)then
Break;
Wait(750);
end;
Since it will check the LockPickJailDoor value before continuing the loop.
Use a while..do loop.
E*
FFS STUPID 60 SECOND TIMER
nickrules
03-19-2012, 09:53 PM
I'm making a monkey thiever script, and making sure that it can escape the prison by itself.
LockPickJailDoor picklock the door and make sure that it successfully picklocked it with various fail safe. I want it to try again if it failled (which is why there is a loop there. A simple if statement isn't enough.
MarkTime(t);
repeat
until (TimeFromMark(t) > 6000) or LockPickJailDoor;
Now as you can see, this code compiles, it works, but it does alot of instruction. It waste ressources.
I know some of you will tell me: Put a wait in there like this. Problem solved!
MarkTime(t);
repeat
until (TimeFromMark(t) > 6000) or LockPickJailDoor;
But no... that's not fixing a problem, thats just a work around in my opinion. I want a real solution.
Is there a way to avoid this problem?
Right now I'm thinking about adding recursivity in the LockPickJailDoor function so that when it fail to lockpick, we give up and logout, or if it is succesfull, we proceed with escaping the prison.
Reducing the number of actions performed (by adding a wait) is by no means a workaround. However, if you insist on not doing that, then just throw FindBlackChatMessage(ChatMsg: string) into your lockpickjaildoor function. Then just set the player false (but hey, you should be doing putting things like that in by default anyways ;)).
@Kyle I don't understand how that's at all different from just adding a wait....
Reducing the number of actions performed (by adding a wait) is by no means a workaround. However, if you insist on not doing that, then just throw FindBlackChatMessage(ChatMsg: string) into your lockpickjaildoor function. Then just set the player false (but hey, you should be doing putting things like that in by default anyways ;)).
Funny you say all that because that's exacly what I did, I must be on the good track.
FindBlackChatMessage(ChatMsg: string) -> already doing that lol
But it isn't enough, because you can fail to find the object, or fail to picklock the door even if you click it.
You must be able to try again if one of those happen.
I haven't added multiplayer yet but the player false is basicaly exacly what I was saying when I said we "give up" and logout.
And @ kyle, you did the same thing as me with another kind of loop. It's still and empty loop.
Use a while..do loop.
E*
FFS STUPID 60 SECOND TIMER
That stupid 60 second timer is a failsafe to make sure you don't stay stuck in prison for 6 hours.
I don't know how you could see this as stupid in any way.
nickrules
03-19-2012, 10:01 PM
That stupid 60 second timer is a failsafe to make sure you don't stay stuck in prison for 6 hours.
Not too sure it's that stupid.
He got ninja'd. 60 second timer between posts.
Anyways, there's really no way to avoid an "empty" loop here. You could maybe call antiban, if your really want.
E: @OP, the loop will not restart until it has tested all break parameters. Which means that it's not calling your lockpick function as fast as it can, it's calling it, waiting for it to finish, then checking the time, and repeating. I think that might be your concern.
Kyle Undefined
03-19-2012, 10:03 PM
Technically, your function should handle that loop. You should just call that function and check if it returns True/False.
He got ninja'd. 60 second timer between posts.
Anyways, there's really no way to avoid an "empty" loop here. You could maybe call antiban, if your really want.
E: @OP, the loop will not restart until it has tested all break parameters. Which means that it's not calling your lockpick function as fast as it can, it's calling it, waiting for it to finish, then checking the time, and repeating. I think that might be your concern.
That stupid 60 second timer is a failsafe to make sure you don't stay stuck in prison for 6 hours.
I don't know how you could see this as stupid in any way.
:duh: x1000.
Technically, your function should handle that loop. You should just call that function and check if it returns True/False.
Yes I was thinking about this, but the fact remains that it is an empty loop.
The objective of a loop is to to repeat code inside the loop. Not outside of it in the condition.
I know you can't do multithreading in pascal but a loop like this is basicaly busy waiting, and busy waiting isn't the way to go.
@ Sin
I'm not used to spam the forum so I didn't knew there was a 60000 timer, and to add to the confusion, I also had a 60000 timer in my loop lol..
bolshak25
03-19-2012, 10:12 PM
MarkTime(t);
repeat
If LockPickJailDoor then
break;
until (TimeFromMark(t) > 6000)
no longer empty and eith item will break you out
MarkTime(t);
repeat
If LockPickJailDoor then
break;
until (TimeFromMark(t) > 6000)
no longer empty and eith item will break you out
Still the same as the other loop, you just broke the condition in 2 part.
I'm watching at my code of PickLockDoor and realise that I could easily implement the check without using an empty loop. I don't know how come I haven't noticed it before.
But from watching all the srl member posting the same solution I guess there is no real way to avoid empty loop.
E: typo, by the way thanks guys for the suggestions.
nickrules
03-19-2012, 10:45 PM
I guess there is no real way to avoid empty loop.
There really isn't, because you can't multithread. If you're not waiting for a wait in your loop, then you're waiting for something else (antiban? Time check? Doesn't matter what). But you're waiting for something, no matter what.
masterBB
03-19-2012, 11:05 PM
WaitFunc(@LockPickJailDoor,100,6000);
Also you need that wait. Do you expect your CPU to run at 100% till it finds the door or the time has passed?
WaitFunc(@LockPickJailDoor,100,6000);
Also you need that wait. Do you expect your CPU to run at 100% till it finds the door or the time has passed?
LockPickDoor is a function, not a variable. The function return true if it was successful.
I don't need a loop anymore since the function make sure everything is alright.
masterBB
03-19-2012, 11:15 PM
LockPickDoor is a function, not a variable. The function return true if it was successful.
I don't need a loop anymore since the function make sure everything is alright.
I know it is a function.... I used a @ in front of it, that's the way you pass a function to a function.
Powered by vBulletin® Version 4.2.1 Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.