Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

Loop until $variable is something

I have a client server model, and basically the client sends a request to the server saying "I want variable a now". The server sends variable a back to the client.

This comes in in the form $requested_variable, let's say.

The problem I have is there will be a brief wait until $requested_variable is set to the new variable.


Imagine I have this function:

def get_var(a)
# send request to server, blahdeblah, it comes back eventually as $requested_variable
return $requested_variable


Since $requested_variable isn't set right until it comes back from the server, I'm kind of stuck here as $requested_variable will just be nil.



What I tried was this:

When the variable comes back, set $gotanewvariable = true

Then,

def get_var(a)
# send request to server, comes back as $requested_var
while $gotanewvariable != true
# do nothing
end
$gotanewvariable = false
return $requested_variable
end




This didn't work. The game just hung and nothing happened ($requested_variable never came through).



Anyone got any ideas how I should handle this situation?
 
I cheated and just dumped the code that checks socket messages in the while loop. It's dirty but it works.

I don't have time to learn threads right now and understand they're mindboggling but I will in the future! Honest :eek:hdear:
 
No, you shouldn't just return an empty value, but you should add a timeout. If I understand what you want correctly, what you should do is

while !($requested_variable = #getfromserver)
if(too much time has gone by)
return nil
end
end

return $reuqested_variable

I am not sure how you communicate with your server, but you may have to adjust the conditional statement for the while loop. Like if the server gives you -1 if nothing is there yet, you'd adjust it to be
while ($requested_variable = #getfromserver) == -1
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top