Note that there are some explanatory texts on larger screens.

plurals
  1. POExperience points jumping and leveling for RP game
    primarykey
    data
    text
    <p>Sorry for the "half vague" title, but I couldn't come up with a better title.</p> <p>Anyways, I'm building sort of an RP game with PHP and right now I'm trying to figure out how to advance the users level and keep the "overflow" of experience points and possibly advance their level again.</p> <p>So let's say my character is level 1, I need 400 exp to reach level 2. Currently what I have is, when I reach 400 exp or over, it advances my level by one and resets my exp. But what I would like to do is if I gain 450 exp it advances my level and resets my exp to the overflow which is 50 in this case. This isn't hard, the tricky part is if I gain so much exp that I will level up more than once!</p> <p>Currently I have this code,</p> <pre><code> public function advLvl($user) { $sql = $this-&gt;db-&gt;prepare("UPDATE `characters` SET `level` = `level` + 1, `experience` = '0' WHERE `u_name` = :user"); $sql-&gt;execute(array(":user" =&gt; $user)); } public function checkLvl($user, $currLvl) { $sql = $this-&gt;db-&gt;prepare("SELECT `experience` FROM `characters` WHERE `u_name` = :user"); $sql-&gt;execute(array(":user" =&gt; $user)); $exp = $sql-&gt;fetchColumn(); if($exp &gt;= $this-&gt;expNextLevel($currLvl)) { $this-&gt;advLvl($user); } } public function expNextLevel($currLvl) { if($currLvl == 1) { return 400; }else { $gain = 400 + $this-&gt;defaultMobGain($currLvl); $expNextLevel = 120 + ($gain * $currLvl); return $expNextLevel; } } </code></pre> <p>And on every request I do this,</p> <pre><code> $alt-&gt;checkLvl($_SESSION["username"], $char_info["level"]); </code></pre> <p>Which, increases my level, everything works, the only problem is that if I gain so much exp that I will advance more than once then it only increments per refresh. So let's say I am level 1 and I gain 1320 exp, that is enough to get me up to level 3 (level 1 = 400, level 2 = 920). But in order to keep up with the changes I have to refresh the page twice.</p> <p>So I assume you have to build some kind of a recursive function. Something like, while my current exp is greater than or equal to the amount of exp till the next level, advance my level. But, it's not as simple as that, that while loops is going to be executed hundreds of times, I only need it to execute twice (in this case). How would I go about doing this?</p> <p>Hopefully it makes sense and you understand what I'm trying to accomplish, if you need more info then just let me know.</p> <p>Thanks in advance.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload