ID:1995953
 
(See the best response by Pokemonred200.)
Code:
mob/proc/levelup()
ini
if(xp>=maxxp)
xp-=maxxp
maxxp*=2
level+=1
usr<<"\blue Level Up"
sleep(5)
goto ini


Problem description:
Its hard to maintain the fps when this proc starts, then i need a way to check the xp without an ifinite loop
Instead of having this chunk of code call itself every half a second, you should be calling it only when the character gets experience.

So, for instance, if you have a "Death" code that checks if character A killed character B, then you should call "levelup" in that function:
mob/proc/levelup()
if(xp >= maxxp)
xp -= maxxp
maxxp *= 2
level += 1
usr<<"\blue Level Up"

mob/verb/attack(mob/target)
target.hp -= 1
if(target.hp <= 0)
xp += 1
levelup()
Another thing; don't use goto, and 'usr' doesn't belong in procs; you're gonna wanna replace usr << "\blue Level up" with something like src << "<span style='color=#00f'>Level Up</span>" (you'd use src for correctness and security reasons (as usr would cause a crash when it's not used in a verb), and the <span> is just applying CSS, which is more or less a preference of mine)

Like Goose said, you would want to call levelup() in a Death() proc, like such:

mob/proc/Death(mob/killer)
if(istype(killer,/mob))
killer.levelup()
src << "<span style='color:#f00'>You have been slain!</span>"

mob/proc/levelup()
if(exp >= maxExp)
exp -= maxExp
maxExp *= 2
++level // ++level does the same thing as level += 1 but runs faster
src << "<span style='color:#00f'>You have leveled up!</span>"

but if i get xp to lvl up twice, the code wont work for the two lvls
In response to Danielkage
Best response
Danielkage wrote:
but if i get xp to lvl up twice, the code wont work for the two lvls

replace if(xp >= maxxp) with while(xp >= maxxp).
but if i get xp to lvl up twice, the code wont work for the two lvls

Ah, then what you are looking for, is a while loop. As pointed out by Pokemonred200:
mob/proc/levelup()
while(xp >= maxxp) // This is like an 'if', but runs multiple times!
xp -= maxxp
maxxp *= 2
level += 1
usr<<"\blue Level Up"

mob/verb/attack(mob/target)
target.hp -= 1
if(target.hp <= 0)
xp += 1
levelup()
In response to Pokemonred200
Pokemonred200 wrote:
Another thing; don't use goto, and 'usr' doesn't belong in procs; you're gonna wanna replace usr << "\blue Level up" with something like src << "<span style='color=#00f'>Level Up</span>" (you'd use src for correctness and security reasons (as usr would cause a crash when it's not used in a verb), and the <span> is just applying CSS, which is more or less a preference of mine)

Like Goose said, you would want to call levelup() in a Death() proc, like such:

> mob/proc/Death(mob/killer)
> if(istype(killer,/mob))
> killer.levelup()
> src << "<span style='color:#f00'>You have been slain!</span>"
>
> mob/proc/levelup()
> if(exp >= maxExp)
> exp -= maxExp
> maxExp *= 2
> ++level // ++level does the same thing as level += 1 but runs faster
> src << "<span style='color:#00f'>You have leveled up!</span>"
>


Now its glitching with my verb to give xp(train simulator)
    verb/Give_XP(mob/m in world)
set category="Owner"
src.xp+=input("How much xp?","(Admin)Giving XP",src.xp) as num
usr.levelup()


Admin.dm:60:error: Bad input type: usr.levelup
Admin.dm:60:error: input type (usr.levelup) must be atomic (area, turf, obj, or mob).


You have the usr.levelup() line indented too far.
In response to Danielkage
You're probably looking for something like:

verb/Give_XP(mob/M as mob in world)
set category = "Owner"
M.xp += input(src,"How much experience are you giving [M]?","Experience",M.xp) as num
M.levelup()


Before, your indentation was off, so input() may have seen it as extra info for what you were going to provide. Also, mob/M in world would look for every object in world (which includes the tiles on the map), whereas mob/M as mob in world would only look for mobs. And finally, since you were using src and usr in Give_XP, you're effectively giving experience to yourself, rather than the player (mob/M) that you selected.
that was the tabs in the lines hahaha thanks all my problems solved
mob/verb
GetFreeXP()
usr.AddXP(27)

mob/proc
AddXP(amount=5)
xp+=amount
LevelUpCheck()

LevelUpCheck()
if(xp>=maxxp)
level+=1
//do the rest of your level up stuff


Here is a simple example. Instead of increasing the xp variable directly use the AddXP() proc to increase xp by the amount you want and it will then check if they leveled up for you.