ID:1330581
 
(See the best response by Kitsueki.)
Code:
mob
verb
FreezeAMob()
var/mob/M = usr.enemy
M.Immobile=1
sleep(200)
M.Immobile=0


Problem description:

I was wondering if this code could cause problems of someone being 'stuck' forever? I'm trying to figure out if say I cause someone to be immobile and then log out before the 20 second delay. Will they still be immobile due to me and be stuck in that state? Should I fix this by making it a proc instead of a verb? Or perhaps using spawn() instead of sleep()?
If they log out before the sleep statement has had time to wait around, then Immobile will still be set to true upon logging back in.

A fairly simple fix is to simply make Immobile a tmp variable so that it isn't saved, preventing the players from getting locked up.
Another tip is to try to avoid using sleep whenever possible...

mob/verb/FreezeAMob()
var/mob/M = usr.enemy
M.Immobile = 1
spawn(200)
if(M) M.Immobile = 0
else //handle for null M


If you were to off-load FreezeAMob to a global proc, it would technically belong to the world, and as long as the world exists, it can handle a spawn request.

proc/FreezeAMob(mob/user)
var/mob/M = user.enemy
M.Immobile = 1
spawn(200)
if(M) M.Immobile = 0
else //handle null case (M logged out since we called spawn


The latter example is safer because if or else WILL be called unless the world dies in that period of 20 seconds.
Btw, you should also check if M is non-null after setting it from user.enemy just in case user.enemy is null.
Would spawn even work in this situation?

                while(count!=64)
flick("Attack",usr)
if(M)
M.health-=rand(usr.tai/64,usr.tai/16)
count++
sleep(1)
In response to Ganing
Yes.
I believe spawn acts sleep, except spawn() doesn't wait for any further code to be executed which will cause a delay. It simply ignores it. I may be wrong though..
Best response
Here's a good explanation about spawn()
In response to Ganing
Ganing wrote:
Would spawn even work in this situation?

>               while(count!=64)
> flick("Attack",usr)
> if(M)
> M.health-=rand(usr.tai/64,usr.tai/16)
> count++
> sleep(1)
>


That is an example where while/sleep should be used.