ID:166465
 
Hello I'm still a novice at programming but picking up fast, I made this code, but it's my first time writing one of these from scratch, can anyone lend a hand

 mob
Move()
if(src.Meditate) return
else ..()
mob
verb
Meditate()
set category="Training"
if(usr.Meditate)
usr.Meditate = 1
usr << "You Start Meditating"
usr.maxpowerlevel+=rand(0.80,0.85)
usr.stamina-=5
return
while(usr.Meditate)
usr.Meditate = 0
return



Thanks alot
I'm glad to see that you're trying from scratch but before you write something, you need to understand what it is you're trying to accomplish.

Nexus6669 wrote:
 mob
> Move()
> if(src.Meditate) return
> else ..()


From this I gather that you don't want a mob to move whilst meditating. Fair enough, good job.
> mob
> verb
> Meditate()
> set category="Training"
> if(usr.Meditate)

At this point, what branches from if(usr.Meditate) is what happens if the usr is meditating at the time. The way you handle this from this point fully contradicts this concept:
>           if(usr.Meditate)
> usr.Meditate = 1
> usr << "You Start Meditating"

If they are already meditating, why would you set Meditate to 1 at this point?
>           while(usr.Meditate)
> usr.Meditate = 0

All this does is set usr.Meditate to 0 if it is currently not 0. Instead, I imagine you'll want to execute a series of statements while they're meditating, and have them stop meditating if they should call it again while meditating. I'll illustrate an example here:
mob/verb/Meditate()
set category = "Training"
if(Meditate) // If I am meditating already....
src << "You stop meditating."
Meditate = 0
return 0
else // Otherwise, if I am not meditating....
src << "You start meditating."
Meditate = 1
while(Meditate) // While I'm meditating....
// Statements that you want to happen while meditating.
return 0


Hiead
A lot from what I can see:
 mob
Move()
if(src.Meditate) return
else ..()//you don't need else because the return above stops the rest of the code from happening.. and you may 'return..()' here
mob
verb
Meditate()
set category="Training"
if(usr.Meditate)//usr abuse, well not really since its a verb but still to be safe, use src instead of usr since:1) this is a mob verb 2) The usr will be calling it
usr.Meditate = 1//wth, you already checked above if this was true (==1), if you don't know about Boolean vars {if(var) or if(!var)}, Developer FAQ > Newbie Section > TrueFalse ... I believe you meant if(!Meditate)
usr << "You Start Meditating"
usr.maxpowerlevel+=rand(0.80,0.85)
usr.stamina-=5//What happens if stamian <0? Add safety checks... or make generalized procs!
return//Hope ya know this stops the rest of the code
while(usr.Meditate)
usr.Meditate = 0//wth do you need a while here if you'll be changing a var that will stop it? use if instead of while
return //see above return's


Note I just commented on stuff

- GhostAnime

EDIT: Meh, see Hiead's post
In response to GhostAnime
There's another mistake in there.
rand(0.80,0.85)

Will always be 1.

Way around;
var/increase=rand(80,85)
usr.powerlevel+=increase/100
In response to Mysame
I don't think that is the problem, because I've used this on a different code and it works to how I want it too, example , taking ages to get one Level on a stat, how I want it...
In response to Nexus6669
What Mysame was saying is that
rand(0.80,0.85) == rand(1,1) == 1
Translation: rand() rounds the decimal values so "rand(0.80,0.85) will ALWAYS return 1 instead of a number between 0.80 to 0.85.

rand(80,85)/100

In order to get a number between 0.80 to 0.85, we must make them rational numbers (whole numbers) and than divide the number to make it irrational (decimal)...so after it picks a # between 80-85, we divide it by 100 to give us a decimal (lets say it gave us 83. 83/100=0.83)[forgot if the terms rational/irrational are the right ones]

(eh this is a rough translation... that's my excuse if I am wrong :P)

- GhostAnime
In response to GhostAnime
mob
verb
meditate()
set category="Training"
if(usr.meditate==1)
usr.meditate=0
usr.icon_state=""
usr<<"you stop meditating..."
else
usr<<"You begin to meditate..."
var/sta=rand(10,15)
usr.meditate=1
usr.stamina-=sta
usr.maxpowerlevel += rand(0.70,0.75)



I tried this code it works fine but when I click meditate it doesn't keep going up, I have to click it to make it stop then click it again then it goes up and soo on, how do I make it so if I click meditate it goes up untill stamina reaches 0,



Thanks.
In response to Nexus6669
1) Learn Boolean shortcuts
2)Learn about while() and break (if() does not loop but for() and while() does)
3) By the looks of it, you still did not understood what Mysame and I were trying to say.
4) Read 1,2,3 and ask us questions if you don't understand any of those :S

edit: For #3, read Hiead's post >.> Apparently it's 0 rather than 1.. meh
- GhostAnime
In response to Mysame
Mysame wrote:
There's another mistake in there.
rand(0.80,0.85)

Will always be 1.


Actually, if I'm not mistaken, it will always be 0. This is because in many programming languages, when a number is rounded to an integer, it merely lobs off anything after the decimal. Therefore, rounding 1.999999999999 would return 1, and so on. Good catch on the bug however; I completely overlooked it.

Hiead
In response to Hiead
Thanks alot, I'm reading the guides now, I just don't get a few things, I read it I understand but I just need to ask a few questions.


So

 mob/var/love=0//FALSE
mob/verb/Loveme()
if(src.love)//if src.love==1 or true
src << "I hate you!"
src.love=0
else//aka if(!src.love) or FALSE [love==0 which it is right now]
src << "Well now you'll love me!"
src.love=1



In the If Function, I would write either true/false, and I would write src.love==1 and if it was vice versa it would be src.love==0, So I would use those commands, src.love==0 for false and src.love==1 for true??
In response to Nexus6669
No, what most people do for boolean (True/False) is looking for either ==1/==0

Boolean shortcut is this:
if(var) => TRUE
if(!var) => FALSE



TRUE means ANY value that is NOT a FALSE value (see below)

FALSE means a value of either: 0, null, or empty string ("" .. which is basically null).

if(var==1) and if(var==0) are the most common boolean-wanna-be checking methods in BYOND (Thank you zeta</sarcasm>) but remember, == specifically looks for that value... so if you have ==0 but at one time made that variable 'null' for the FALSE value... well, there's a problem, which is why people try to make other use that shortcut (and because it's faster+cleaner looking too). The ==0 and null example are asked a lot, because that's the problem in the Zeta code's meditate proc -_-'

- GhostAnime
In response to GhostAnime
Ok I'm learning slowly now, I made a "while" function now, so while he's meditating it will increase the following stats.

example

 mob
Move()
if(src.meditate) return
else..()

mob
verb
meditate()
set category="Training"
if(usr.meditate)
usr.meditate=0
usr.icon_state=""
usr<<"you stop meditating..."
return
else
usr<<"You begin to meditate..."
usr.meditate=1
while(usr.meditate)
usr.stamina -= rand(1,3)
usr.maxpowerlevel += rand(1,2)



But Now I have the problem is that the values on the powerlevel and the stamina go all weird and messed up, like "5.86556e+006" and stuff, It's working to how I want it and I will put an "If" stamina =0 function on there after I can get this to work..
In response to Nexus6669
DM makes those wierd numbers appear because it usually can't have numbers with so many...well numbers, so it automatically converts it to scientific notation. That's about 5.8 million in SN.

Also, you must have it return 0 at mod/Move(), because that returns it false and prevent movement. You can probably still move at just return. Lemme help you clean that Up a bit and reduce the amount of lines you have there.

mob
Move()
if(src.meditate) return 0
..() //you don't need an else statement here, becauase its likely you'll need to stop movement elsewhere

mob
verb
Meditate()
set category = "Training" //wow, cliched category name for DBZ, come on, original is better...j/k
if(!usr.meditate) //if they aren't medding
usr.meditate = 1
usr << "You start meditating"
while(usr.meditate) //if they are
usr.stamina -= rand(1,3)
usr.power_level -= rand(1,2)
if(usr.stamina <= 0)
usr.icon_state = ""
usr.stamina = 0 //reset it to 0
usr.meditate = 0
usr << "You stop meditating"


I haven't changed anything at all, just rearranged stuff to reduce the number of lines. What I did do is rearrange it so it will stop by itself. You don't have to click med anymore.
In response to Nexus6669
All you have to do is pass the number through num2text() (look it up in the reference) and it'll show it properly.
In response to Pyro_dragons
Ok my only problem now is that, my usr.maxpowerlevel+= and usr.stamina-= are screwed somehow If I run my game on 12 hosting as from now you can see what I mean..


also here is my finalized code

 mob
Move()
if(src.meditate) return 0


mob
verb
meditate()
set category="Training"
if(usr.meditate)
usr.meditate=0
usr.icon_state=""
usr<<"you stop meditating..."
return
if(usr.stamina >= 1)
usr.lifting = 1
usr<<"You begin to meditate..."
usr.meditate=1
while(usr.meditate)
usr.stamina -= rand(1,2)
usr.maxpowerlevel += rand(1,2)
if(usr.stamina<=0)
usr.stamina=0
usr.meditate=0
usr << "You snap out of Meditation, due to lack of energy"
return
if(usr.stamina<=0)
usr.meditate=0
usr << "You are too tired. Try Resting."




If you type in Zeus' Nexus on the search, you will see my game, I will be hosting it all night, I'm off to bed, hopefully you can help me out.


Thanks you...
In response to Nexus6669
You should probably have some spawn() in there so it doesn't shoot up and drain super fast.
In response to Pyro_dragons
Pyro_dragons wrote:
You should probably have some spawn() in there so it doesn't shoot up and drain super fast.

spawn() wouldn't prevent that. ;) Try sleep(), instead.

Hiead
In response to Hiead
That's what I meant lol. My bad.
In response to Pyro_dragons
Thanks alot guys, sorry that it took so long for me to understand... :)