12-02-2024, 04:50 PM
Usually when I need to do what Pete is suggesting, I create a self-monitoring routine and only have it report an a solution once it's finished.
Let's say I have an interactive game where you're in a spaceship flying around with other players all connected via TCP/IP. You can use the mouse to move and interact with the spaceship. You want to type a message to others.
Now, at this point, you DON'T want the game to freeze up, so I'd set a few flags.
IF ALT + M THEN 'the player hit the alt-m buttons to flag they were sending a message
PlayerMessage = True 'set a flag so we now check for what they're typing
END IF
Below all these flag setters, I'd have some code to call my routines:
IF PlayerMessage Then
Msg2Send$ = GetInputForMessage
IF Msg2Send$ <> "" THEN PlayerMessage = 0 'we got a message. we don't need to look for anymore
END IF
And outside my main loop would be something like:
FUNCTION GetInputForMessage$ (k as Long) 'key is my key stroke as read in the main loop and passed along faithfully
STATIC partMsg$
IF k <> CHR$(13) THEN
partMsg$ = partMsg$ + chr$(k)
ELSE
GetInputForMessage = partMsg$ 'the player hit ENTER, finishing what he was trying to type in
partMsg$ = ""
END IF
END FUNCTION
The idea here is that main loop keeps looping. The subroutines ONLY get called when necessary to reduce strain and resource usage in the main loop. Those subroutines ONLY return a value once they're finished do whatever they do, otherwise, they just return blank.
If it's a routine that returns a number, I'll choose some fanciful non-existent type number for them, or else I'll pass a FINISHED flag with them if I can't pass an invalid solution. For example, think of a routine that asks for someone to enter a number from 0 to 9. IF they haven't hit 0 to 9 yet, I'd simply return a value of 123 as a "nope! no valid info here yet. We're still counting fingers and toes waiting on it..."
So a lot of my longer junk that I code relies on this style coding. Set a flag, only check the routine when that flag is set, keep checking it until that flag is cleared or the process is complete, use the completed result and reset that flag so we don't check that routine anymore and free up processing cycles.
Let's say I have an interactive game where you're in a spaceship flying around with other players all connected via TCP/IP. You can use the mouse to move and interact with the spaceship. You want to type a message to others.
Now, at this point, you DON'T want the game to freeze up, so I'd set a few flags.
IF ALT + M THEN 'the player hit the alt-m buttons to flag they were sending a message
PlayerMessage = True 'set a flag so we now check for what they're typing
END IF
Below all these flag setters, I'd have some code to call my routines:
IF PlayerMessage Then
Msg2Send$ = GetInputForMessage
IF Msg2Send$ <> "" THEN PlayerMessage = 0 'we got a message. we don't need to look for anymore
END IF
And outside my main loop would be something like:
FUNCTION GetInputForMessage$ (k as Long) 'key is my key stroke as read in the main loop and passed along faithfully
STATIC partMsg$
IF k <> CHR$(13) THEN
partMsg$ = partMsg$ + chr$(k)
ELSE
GetInputForMessage = partMsg$ 'the player hit ENTER, finishing what he was trying to type in
partMsg$ = ""
END IF
END FUNCTION
The idea here is that main loop keeps looping. The subroutines ONLY get called when necessary to reduce strain and resource usage in the main loop. Those subroutines ONLY return a value once they're finished do whatever they do, otherwise, they just return blank.
If it's a routine that returns a number, I'll choose some fanciful non-existent type number for them, or else I'll pass a FINISHED flag with them if I can't pass an invalid solution. For example, think of a routine that asks for someone to enter a number from 0 to 9. IF they haven't hit 0 to 9 yet, I'd simply return a value of 123 as a "nope! no valid info here yet. We're still counting fingers and toes waiting on it..."
So a lot of my longer junk that I code relies on this style coding. Set a flag, only check the routine when that flag is set, keep checking it until that flag is cleared or the process is complete, use the completed result and reset that flag so we don't check that routine anymore and free up processing cycles.