Skip to main content
gamedev.net
🔒 Locked

Help with a simple wait problem

Started by goomyman May 12, 2003 at 3:18 PM 0 replies 950+ views
Original Post
goomyman
goomyman
I am making my own wait function. I want it to wait a set amount of time. like for example: wait(15); The function would look something like this bool wait(int amountofseconds) { return true; // if 15 seconds has past since being called return false; // if 15 seconds has not past since being called } There is the problem. How do I set it up so that it starts counting seconds after it has been called. Not only that but how do i set it up so that the function can be called more than once at a time. I want to avoid using classes if possible since I have been writting the entire project in C and I dont want a mixture of C and C++. It would be a lot like this but in a function call. static int CurrentTime = TimeGetTime(); If(TimeGetTime() - CurrentTime > AMOUNT_OF_WAIT_TIME) { //Do stuff }
NaliXL
NaliXL
You''d need a dynamic array of end-times. Unfortunately, you''d need a separate function to tell you if the specified time has passed or not. Some pseudo-code in Pascal :

var EndTimes : Array of Word; // Creates an array of 32-bit unsigned integers (Word that is, under Pascal).

// Function SetTimer takes a 32-bit unsigned int as waiting-time, and returns a 32-bits unsigned int as function instance
function SetTimer(TimeToWait : Word) : Word;
begin
// Enlarge the EndTimes array by one element
SetLength(EndTimes, Length(EndTimes) + 1);
// Give the created element of EndTimes it''s value
EndTimes[Length(EndTimes) - 1] := (GetTime() + TimeToWait);
// Return the index of the element we just created.
Result := (Length(EndTimes) -1);
end;

// Function TimerExpired returns true if the timer with the given index has expired
function TimerExpired(Index : Word) : Boolean;
var TemporaryCounter : Word;
begin
// If the current time is more than timer-start + time before expire
if (EndTimes[Index] < GetTime()) then begin
Result := true; // return true
EndTimes[Index] := 0; // Set the current element of EndTimes to 0 to indicate it''s not used
TotalNow := 0; // Set total to 0 (used later)
// for the current index + higher indexes
for TemporaryCounter := Index to (Length(EndTimes) - 1) do begin
// Add the value of the current EndTimes index to TotalNow
TotalNow := TotalNow + EndTimes[TemporaryCounter];
end;
// If TotalNow is still 0, it means the current element and above can be
// safely removed from the array, so let''s do so.
if (TotalNow = 0) then SetLength(EndTimes, Index);
end else Result := false; // else return false
end;

Newbie programmers think programming is hard.Amature programmers think programming is easy.Professional programmers know programming is hard.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.