Wait Question

Posted By: DLively

Wait Question - 05/18/14 03:50

Quote:
wait(1) is often used at the end of a while loop which is to be executed once per frame cycle.


As the manual states, a wait is generally used at the end of a while loop. If all my loops have a wait(1) at the beginning of them, could that really effect my code in a negative way?

Could It make things behave in such a way that they "wait before they do", rather than "do then wait".. kinda resulting in bad performance? NOt sure if this example is clear enough :?
Posted By: xbox

Re: Wait Question - 05/18/14 05:31

I think, and I could be wrong, that putting a wait(1) at the end of a while loop is just good practice. Personally, after learning it at the end of a while loop, I can't put it any where else. I'm sure someone with more experience will correct me if I'm wrong but having the wait(1) at the beginning technically will be a "wait before they do" rather than "do then wait" but it will only be for a single frame so it won't be noticeable at all, unless the frame rate drops substantially. If worse comes to worse, at least moving a single line to the end of a loop isn't difficult to do tongue
Posted By: WretchedSid

Re: Wait Question - 05/18/14 10:40

The only difference you will see is when your function yields back to others and when it performs its operation. You can also have a wait in the middle if that tickles your fancy, your local variables and my and you will be restored. However, note that wait() allows other functions to resume execution which may or may not have side effects on globals that you have to take into account. Of course only if it applies to your use case.

Something I really want to stress though: You don't need a wait() in your loops if they terminate by themselves! That is, unless you have a loop that you want to execute once per frame (or in similar intervals), putting a wait() will be decremental to your performance! I see that way too often that beginners assume that a wait() is a must within a loop or otherwise their program will halt.
Posted By: DLively

Re: Wait Question - 05/18/14 16:00

My practice has been to put the waits just after the while's curly bracket {wait(1);..

My question now, is how to have a loop without a wait??? I always get an engine crash without one... laugh
Posted By: Ch40zzC0d3r

Re: Wait Question - 05/18/14 16:04

Code:
int i;
for(i = 0; i < 5; i++)
{
     //do some things here
}

//and continue without a crash



Code:
while(str_len(strMsg) > 0)
{
    str_trunc(strMsg, 1);
}



and so on...
Posted By: WretchedSid

Re: Wait Question - 05/18/14 16:25

Originally Posted By: DLively
My question now, is how to have a loop without a wait??? I always get an engine crash without one... laugh

Hanging != crashing!

Crashing means that all computation stops, hanging just means that you do computation without giving the window event handler any time to do its own processing, which the OS will then take as: Alright, it's not responding.

This, will hang your program, but not crash it:
Code:
while(1)
{}



Soo... How do you write loops that don't hang? Well, have them terminate at some point!

Take this strcpy function as an example:
Code:
char *strcpy(char *dst, const char *src)
{
	char *d = dst;

	while(1)
	{
		*dst = *src;

		if(*src == '\0')
			break;

		dst ++;
		src ++;
	}

	return d;
}



The loop will automatically terminate once the source string reached the end. So, why shouldn't you put a wait in there? Quite simply, first of all because it has a return value and that would never reach the caller with a wait (remember, wait doesn't allow the callee to return values), and secondly because it's superfluous in the first place!

You don't want your string copy operation to take multiple frames, you want to call it and have the result immediately. Your program won't hang, even for long strings, because copying the memory around is insanely fast especially once the CPUs branch predictor kicks in.


Of course, that doesn't mean wait in loops doesn't have its use case! Everything that you want to get periodically done every frame or so, well, use wait, that's what it's there for!
Posted By: DLively

Re: Wait Question - 05/19/14 01:17

Thanks Guys laugh

Awesome Read Sid, I really enjoyed how detailed you made it. You couldn't have made that any more clear for me laugh I am excited to put it to practice now ^_^
Your tip should be put in the manual laugh
© 2024 lite-C Forums