ID:679613
 
(See the best response by Stephen001.)
Hey guys,
I am making a game where a bunch of monsters spawn and you can kill them, get upgrades, and weapons. But I ran into some trouble when I needed to be running three infinite loops at once and I was having trouble.
mob
Login()
alert("Welcome! The world has been over run by cute little aliens!","Invasion")
alert("It's up to you to stop them! Good luck!","Invasion","Begin")
usr.game=1
..()
usr.AmmoRegeneration() //This proc and the following two are the ones that I need to be running at the same time.
usr.MobCheck() //This one checks to see if you are on the same space as a monster.
Game() //This spawns the monsters in the four corners of the map every 1 - 3 seconds.

mob
proc
MobCheck()
for(var/mob/NPC/monster/M in oview(0))
usr.HP-=1
usr.Dead()
del M
MobCheck()
AmmoRegeneration()
sleep(usr.ammoregeneration)
if(usr.ammo<usr.maxammo)
usr.ammo+=1
AmmoRegeneration()

Unfortunately, the only one that runs is the Ammo Regeneration one. It makes sense that it would never get to the other two but I'm really stumped on how to get them all running. Please help.

P.S. The last procedure was too lengthy to put in.
You're recalling MobCheck() and AmmoREgeneration) all the time, it won't stop being called that way, add a sleep(0.5), or it'll lag a lot.w
Okay thanks, but I still dont know how to run them all at the same time. I can't put all of them into one procedure because they have different sleep times.
Best response
Essentially your issue is that AmmoRegeneration() never completes, as you say. There are three solutions for this, some better than others:

First is to spawn() off the initial call:

        spawn() usr.AmmoRegeneration()


This means AmmoRegeneration will happen after the Login() finishes (or some suitable sleep() in Login() or it's called procedures), and the sleep in AmmoRegeneration() will allow other things to run also. So they kind of appear to run "at the same time".

A problem still exists though, as AmmoRegeneration() never actually completes. Each time to call a procedure, BYOND allocates a bit of memory for it. When the procedure completes, BYOND frees up that memory. In your case, it allocated a bit of AmmoRegeneration(), which then calls AmmoRegeneration() etc etc etc until the end of time, and you have an infinite loop that keeps allocating memory. So eventually BYOND throws errors about this.

This can be solved by:

        AmmoRegeneration()
sleep(usr.ammoregeneration)
if(usr.ammo<usr.maxammo)
usr.ammo+=1
spawn() AmmoRegeneration()


If you spawn here, the second AmmoRegeneration() call happens after the current one, and the current one's memory is freed up. This has the benefit also that you don't need to do the code I've shown in my first example, as the initial call will complete pretty quickly.

But, we can do one better:

        AmmoRegeneration()
spawn() while(TRUE)
sleep(usr.ammoregeneration)
if(usr.ammo<usr.maxammo)
usr.ammo+=1


We actually only make one AmmoRegeneration() call here, and that spawns off a little anonymous block of code that loops infinitely, in a safe way. The benefit to this (aside from less function calls) is that you can add code to avoid calling AmmoRegeneration() again and creating two loops by accident.
I tried what you said and it worked perfectly. I can't thank you enough. Sorry, it took me a while to get back on the computer and check for responses.
Thanks again!

One thing that happened though was that this procedure caused an infinite loop.
mob
proc
MobCheck()
for(var/mob/NPC/monster/M in oview(0))
usr.HP-=1
usr.Dead()
del M
MobCheck()

I'm not really sure how to prevent the infinite loop when I kind of need it to be one. Any ideas?
Add a little delay. Like sleep(5).
I made a delay of sleep(5) for 0.5 seconds and about a minute into the game it crashed because of an infinite loop still. What's going on and how should I go about fixing this?
Same principle as I demonstrated before should apply to that procedure also. Could you vote on my first answer if you felt it was useful please? Just allows other people in the future to see that's what you used.
Thanks I fixed it.
And sure.
Much obliged, glad I could help.
Sorry to bother you guys again but the sleep(5) in the infinite loop still makes the game crash sometimes. I don't want it to run any slower or the game won't work. Any ideas?
In response to Conno1234
Conno1234 wrote:
Sorry to bother you guys again but the sleep(5) in the infinite loop still makes the game crash sometimes. I don't want it to run any slower or the game won't work. Any ideas?

Are you calling MobCheck() more than once? If so, there would be multiple instances of this infinite loop procedure occurring, thus bogging down your game to an eventual halt.
Screw, ironically, nailed it.