ID:138795
 
Code:
mob/proc    Rest()
if(usr.hp == usr.maxhp)
usr<<"You are fully rested!"
usr.icon='mob.dmi'
usr.icon_state="player"
usr.cant_move=0
usr.resting=0
else
usr.icon='mobstates.dmi'
usr.icon_state="zzz"
usr.cant_move=1
usr.resting=1
sleep(30)
usr.hp+=1
usr.Rest()

Wake_Up()
if(usr.resting==1)
usr.icon='mob.dmi'
usr.icon_state="player"
usr.cant_move=0
usr.resting=0



obj/Rest
icon='hud.dmi'
icon_state="rest_up"
layer=MOB_LAYER + 20
New(client/C)
screen_loc="4,1"
C.screen+=src
Click()
if(usr.resting==0)
usr.Rest()
else
usr.Wake_Up()
MouseDown()
icon_state="rest_down"
MouseUp()
icon_state="rest_up"


Problem description: When the mob is resting (resting=1) and i want him to wake up() (by clicking on obj/Rest) the icon and icon states switch back, but at intervals of 3 seconds (sleep(30)) the mob keeps switching back to icon state "zzz" and wont "wake up" until usr.hp == usr.maxhp. I was wondering if there is a simpler way to make a Rest proc where the mob would rest when need and wont when unneeded. And also, when calling the same proc, if the mob is resting, he will wake up.



Baltraven

Just a hint
if(usr.resting==1) // is BAD

if(usr.resting)//good unless you have resting = 2 or 3 etc

if(!user.resting) //notice the ! this checks for no value.


The reason it keeps going to "rest" is because your aren't checking if the user is still resting or not in the proc.

P.s use SRC instead of USER in procs.
In response to A.T.H.K
so in the Rest() proc i'd check if src is resting or not. You want me to use if(!src.resting). Just a quick question about the !, does !src.resting mean resting = 1 or 0?
In response to Baltraven
! means 0 or no value
In response to A.T.H.K
Thanks. I got it to work. I tried to check whether or not src was resting within the proc, but what happend was he would rest for 1 hp and then stop. SO instead, i had the check when i called the proc. SO the check is now within the rest button instead of the proc. Either way, ur tip helped me, thanks! =D
In response to A.T.H.K
Actually problem still exists. I returned it to the former piece of code that i posted. It seems that since Rest() is a repetitive proc cuz it ends in Rest() i need some kind of check within the proc as well as the call from the obj.

/mob/procRest()
if(src.hp == src.maxhp)
src<<"You stopped resting!"
src.icon='mob.dmi'
src.icon_state="player"
src.cant_move=0
src.resting=0
else
src.icon='mobstates.dmi'
src.icon_state="zzz"
src.cant_move=1
src.resting=1
sleep(30)
src.hp+=1
src<<"You gained 1 lifepoint while resting."
src.Rest()


/obj/Rest
icon='hud.dmi'
icon_state="rest_up"
layer=MOB_LAYER + 20
New(client/C)
screen_loc="4,1"
C.screen+=src
Click()
if(usr.resting)
usr.Wake_Up()
else
usr.Rest()
MouseDown()
icon_state="rest_down"
MouseUp()
icon_state="rest_up"


I need some kind of a check within the proc itself, as well as when i call the proc ( or so it seems). can someone please clarify what i am doing wrong. The problem is the same as the first post.
In response to A.T.H.K
A.T.H.K wrote:
The reason it keeps going to "rest" is because you aren't checking if the user is still resting or not in the proc.

/mob/procRest()
if(src.resting)
if(src.hp == src.maxhp)
src<<"You stopped resting!"
src.icon='mob.dmi'
src.icon_state="player"
src.cant_move=0
src.resting=0
else
src.icon='mobstates.dmi'
src.icon_state="zzz"
src.cant_move=1
src.resting=1
sleep(30)
src.hp+=1
src<<"You gained 1 lifepoint while resting."
src.Rest()
In response to Baltraven
You're both going about this the wrong way. This would be best accomplished with a while() loop, like so:

mob/proc/Rest()

//We set the variables.

icon = bla
icon_state = bla
cantmove = 1
resting = 1


//This will loop until the condition is no longer true.

while(hp < maxhp)
hp++
src << heal message


//In case it goes over(not an issue with +1, but you may
//want to change it later).

if(hp < maxhp)
hp = maxhp


//Then we set things back to normal.

src << end message
icon = bla
icon_state = bla
cantmove = 0
resting = 0


This sets the appropriate variables at the beginning, runs the while() loop until health is greater than or equal to max health, sets health to max health if it's over, and then resets the appropriate variables.
In response to Robertbanks2
This will not help because of several things. The more i click the rest obj, the more Rest procedures will run, meaning instead of regenerating 1 hp per 3 seconds, i can click the button as fast as possible and have it keep regenerating. Also, ur code only enables the mob to return to normal states when hp = max hp. i want it so that if i run the proc while the mob is resting, it will only then return to normal states. Basically, what i want is a piece of code that will:

1. Rest my mob when i click the obj
2. "Unrest" my mob when the mob is "resting", when i click the obj.
3. Continue to rest until button is clicked, or health is fully regenerated.
In response to Baltraven
Baltraven wrote:
what i want is a piece of code that will:

I have given you ideas on how to produce this .... Not many people like just GIVING out code.

mob
var
normal_icon
normal_icon_state
hp
cant_move
resting
maxhp
proc
Rest()
if(src.resting)
if(src.hp == src.maxhp)
src<<"You stopped resting!"
src.icon=src.normal_icon
src.icon_state=src.normal_icon_state
src.cant_move=0
src.resting=0
else
src.normal_icon = src.icon
src.normal_icon_state = src.icon_state
src.icon='mobstates.dmi'
src.icon_state="zzz"
src.cant_move=1
sleep(30)
src.hp+=1
src<<"You gained 1 lifepoint while resting."
src.Rest()
WakeUp()
if(src.resting)
src.resting = 0
src.cant_move=0
src.icon=src.normal_icon
src.icon_state=src.normal_icon_state

obj
Rest
icon='hud.dmi'
icon_state="rest_up"
layer=MOB_LAYER + 20
New(client/C)
screen_loc="4,1"
C.screen+=src
Click()
if(usr.resting)
usr.WakeUp()
else
usr.resting = 1
usr.Rest()
MouseDown()
icon_state="rest_down"
MouseUp()
icon_state="rest_up"


[EDIT] sorry forgot something with that
In response to Baltraven
1. It does that.
2. Change the conditions checked in the while() statement to something like while(health < maxhealth && resting), then have clicking the button while resting set resting to 0.
3. Do what I just said in 2.

I gave you what you needed to make something functional for yourself, not a piece of code to copy paste into your game like a noob.

The dev forums are not a code dispenser, don't come here and complain when people don't hand you fully functional code that does exactly what you want it to. Anyone here who is actually interested in helping you will teach you what you need to know to handle the situation on your own, then make you do it for yourself.
In response to Robertbanks2
Robertbanks2 wrote:
I gave you what you needed to make something functional for yourself, not a piece of code to copy paste into your game like a noob.

TBH and I know its wrong but I like to just add to the already existing code it will keep them in a comfort zone.

Anyway its been posted nearly 4 times, if you don't get it maybe its time to read - http://www.byond.com/docs/ref/
In response to A.T.H.K
Thank you for your help. What seems to be the problem is that when i call the proc from the obj, i had to set resting = 1 before calling rest(). That was the only thing i changed in my code and it works now. Thanks A.T.H.K!
In response to A.T.H.K
It's not wrong, it's the easiest way to solve a problem. Perhaps the easiest isnt the best way, but it is the fastest, and when it comes to internet and coding "noob"s like me the quicker the response, the better.
In response to Baltraven
It is wrong, because then you just copy paste it and don't learn anything. These forums are for learning, not begging people to fix your broken code for you.
In response to A.T.H.K
A.T.H.K wrote:
TBH and I know its wrong but I like to just add to the already existing code it will keep them in a comfort zone.

You really shouldn't, it doesn't help anyone in the long run. especially when the code you're adding on to isn't very good to begin with.

Bettering yourself at anything requires that you remove yourself from your comfort zone, otherwise you'll just keep doing the same things and never get anywhere. If someone doesn't understand, then we explain it further, with the final logical step being to write out a full explanation with a working example, but that should be avoided if at all possible.
In response to Robertbanks2
if i copy/pasted, it would give me more errors than i started with. Perhaps the wording of my posts seems rather demanding, but it's because i have become stressed out over such a small mistake.