ID:144538
 
Code:
        C()
set hidden = 1
if(usr.resting == 0)
usr.resting = 1
while(usr.resting == 1)
usr.move = 0
spawn(20)
usr.hp += rand(2,10)
if(src.hp >= src.maxhp)
src.move = 1
src.hp = src.maxhp
src.resting = 0
if(usr.resting == 1)
usr.resting = 0
usr.move = 1
if(src.hp >= src.maxhp)
src.move = 1
src.hp = src.maxhp
src.resting = 0


Problem description:

I have a feeling I'm creating a loop and crashing it but I can't find out where the loop is. I set the C() verb ad the macro and it's a basic rest command. I want it to check if their hp is less than maxhp and if so, heal. And when they hit C again it stops even ig they aren't full.
Don't use if(variable == 1) unless you are checking for an exact value otherwise use if(variable) and if(!variable) instead of if(variable == 0).
In response to Xx Dark Wizard xX
that doesn't fix my crash problem.
In response to CYN
That code right there isnt even robust, I suggest you take DW's advice.
In response to XzDoG
I did.... <.<
Well, look.
You have
        C()
set hidden = 1
if(usr.resting == 0)
usr.resting = 1
while(usr.resting == 1)
usr.move = 0
spawn(20)
usr.hp += rand(2,10)
if(src.hp >= src.maxhp)
src.move = 1
src.hp = src.maxhp
src.resting = 0
if(usr.resting == 1)
usr.resting = 0
usr.move = 1
if(src.hp >= src.maxhp)
src.move = 1
src.hp = src.maxhp
src.resting = 0


Notice the first if statment. You are making it so if they have 0 resting, they go to 1. On the 3rd if statment you made it so if they have 1 resting it gos to 0. Is that what is looping?
In response to Revojake
The only time you should use ==1 is something like
mob
proc
almostdead(mob/M)
if(M.hp == 1)
for(var/obj/O in client.screen)
var/icon/I = (O.icon)
I.SwapColor("#fff","#ff0000"))
No put usr in proc. Ungh.

Lummox JR
In response to Lummox JR
I didn't use usr, and he was using a verb.
In response to Lummox JR
Yeah lummox that is a verb. I guess I should have specified that but yeah...
You're problem is simple: you're not sleeping the loop.
spawn() does not interrupt the current proc, but instead splits it. Replace that with sleep().

Also, instead of checking the value of resting again, you should use "else".

Not to mention you randomly change between src and usr. I would suggest just sticking to src in this case, because then you don't have to put "src." infront of everything.

A few things about structure:
1. There's no reason to set move to zero repeatedly (assuming you have a good movement system), so take it out of the loop.
2. When checking a boolean variable (something that will always equal 1 or 0), just use if(var) and if(!var).
3. You should have the sleep() at the end of the loop, to ensure that resting will be one. Otherwise, they might stop resting within that 2 seconds and still regain health.

I think that's the gist of it. If I missed anything or screwed anything up, just tell me. I haven't been on Byond for a bit.
In response to DarkCampainger
Fixed thanks :)
In response to DarkCampainger
DarkCampainger wrote:
3. You should have the sleep() at the end of the loop

I've waited all day for someone to say this. Congratulations.
In response to Xx Dark Wizard xX
How about just get in the habit of not using USR whenever possible ;)
In response to VcentG
VcentG wrote:
How about just get in the habit of not using USR whenever possible ;)

Well if it's a verb then it's fine there. That just wasn't clear from the post. The name looked like a proc, not a verb.

Lummox JR
In response to Lummox JR
Yeah, but still... if you get in the habit of not using usr period then you wont use it in things other than a verb also.. that's all I'm sayin.