GMS 2 faster xp adding? [solved]

Discussion in 'Programming' started by Bladebss, Aug 12, 2019.

  1. Bladebss

    Bladebss Member

    Apr 8, 2018
    so i have some code that adds xp to the enemy xp in a grid, one at a time, until it is equal to the xp to add. there is a for loop that runs on xp in that grid to see if the level up criteria is met and then adjusts the stats of the enemy in accordance with this. I have it this way so that if the enemy earns more than one levels worth of xp, it does the level up calculations the moment it reaches the required xp and then continues to add the xp, after the level up. the next levels xp is based of of the amount of xp the enemy had at the moment of level up, so it needs to be the right amount.

    I tried it without this adding 1 xp at a time and it just looped to the end result before the level up criteria activated, the enemy only goes up one level and the amount needed for the next level is massive as its based off the total xp to add not the amount needed to level up.

    so this adding 1 xp at a time works.. but its super slow.
    the xp added is done at the beginning of a battle and the xp is to make the enemy the right level for that battle.
    so at the beginning of the battle the enemy starts at level 1 and slowly raised up until they reach the correct level. but this seems really weird to the player as they watch the enemy slowly climb levels at the start of battle.

    is there a way I can speed up the adding of 1 xp at a time or change the way its done, so that at the start of the battle the enemy appears at the right level from the beginning.
    I did think I could just have the battle not start until the xp and the xp to add, are the same, but at higher levels this makes the time too long before the battle starts. honestly its madness!

    "enemy_inventory,13,0" is the xp stat for the enemy
    "global.enemyxp" is the amount of xp the enemy needs, to be the level required at the start of battle
    "enemy_inventory,19,w" is level up, and is either true or false, based on if the conditions are met
    "enemy_inventory,28,w" is the amount of xp the enemy needs to go up another level, and is doubled on level up being true

    this is all in step event..

    if(ds_grid_get(enemy_inventory,13,0)<global.enemyxp){ds_grid_set(enemy_inventory,13,0,ds_grid_get(enemy_inventory,13,0)+1);}}// if xp to add is more than enemy xp add 1 to it every step. 
    for(w=0; w< ds_grid_height(enemy_inventory);w++){
    if (ds_grid_get(enemy_inventory,13,w) > ds_grid_get(enemy_inventory,28,w)) {ds_grid_set(enemy_inventory,19,w,true) ds_grid_set(enemy_inventory,11,w,ds_grid_get(enemy_inventory,11,w)+1)}}}//if the xp is greater than next level xp, level up is true, add one to level stat
    if (ds_grid_get(enemy_inventory,19,0)=true){ds_grid_set (enemy_inventory,14,0,ds_grid_get(enemy_inventory,13,0) *2); // if level up is true, double the current xp and set next level stat
                    ds_grid_set (enemy_inventory,24,0,ds_grid_get(enemy_inventory,24,0) + (round(ds_grid_get(enemy_inventory,21,0)/ 3)));//increase attack by 3rd base attack
           ds_grid_set (enemy_inventory,25,0,ds_grid_get(enemy_inventory,25,0) + (round(ds_grid_get(enemy_inventory,22,0)/3)));//increase defence by 3rd base defence
           ds_grid_set (enemy_inventory,27,0,ds_grid_get(enemy_inventory,27,0) + (irandom_range(1,5)));  // increase health by 1 to 5    
           ds_grid_set (enemy_inventory,28,0,ds_grid_get(enemy_inventory,14,0));// xp needed is double current xp
                       ds_grid_set(enemy_inventory,19,0,false);//level up is done
  2. rytan451

    rytan451 Member

    Jun 21, 2016
    Calculate the needed xp, then set the enemy's xp to that xp. Don't increase it one at a time.

    enemy_inventory[# 13, 0] = global.enemyxp
    This code snippet changes the enemy's xp (in grid slot 13, 0 as you requested) so that it's global.enemyxp.

    By the way, why are you storing xp in a grid data structure?
  3. Bladebss

    Bladebss Member

    Apr 8, 2018
    When i first did it, i just set the enemy xp to the desired amount but, the for loop cycles all the way to the end in one step, so if more than one level of xp is earned the enemy still only goes up one level.

    The for loop doubles the xp at level up and makes that value the xp required for the next level, that would be double global.enemyxp so only one level and needs double that to pregress to the next. I wanted it to level up and then add the remining xp to the next levels progress. I read in another thread a sugesstion to some one else with a similar problem was to have the xp added one at atime untill it was equal to the xp toadd.

    I have it in a grid because it seemed like a good way to store all the stats and atributes for the enemy. I mean i think it could be an array instead but i didnt think it would matter and would allow me more flexibility if i wanted to expand the grid, say in a duo battle with more than one enemy. Do you see a problem with it being in a grid? Is there a better way? Im kinda just working with what i know lol
  4. Bladebss

    Bladebss Member

    Apr 8, 2018
    ok, I think I found a solution, so for any one reading this with a similar problem, here's what I did..

    I realised the reason it was going so slowly was because it was adding one xp per tick, so if im adding 400 xp that's 6 seconds at least..
    so I decided I needed to address the original problem of the doubling of xp to determine the required xp for the next level.

    so I used the level rather than xp to formulate this, now im no maths genius lol and I could not work out a formula to turn the level into the expediential growth pattern of the xp requirement, doubling each time. BUT I found an easier one! so it does "the level pluss one" x "the level pluss one" x "the level pluss one". this way the xp for the next level was based off of the level you are not the xp you have at level up, and no matter what level the game can calculate the amount of xp you need to go up a level.

    so I replaced the code top line of code with this one.
    and changed the first line in the for loop to this one

    if (ds_grid_get(enemy_inventory,13,w) >   (ds_grid_get(enemy_inventory,11,w)+1)* (ds_grid_get(enemy_inventory,11,w)+1)* (ds_grid_get(enemy_inventory,11,w)+1)) {ds_grid_set(enemy_inventory,19,w,true) ds_grid_set(enemy_inventory,11,w,ds_grid_get(enemy_inventory,11,w)+1)}}}
    now the code runs in under a second and only takes one tick per level so.. at level 60 it will still take one second, and a further half a second at level 100, but this is much improved and I think I can change the way the rest of the stats are influenced and calculated in a similar way to this, so I can simply have all the stuff calculated off the level and base stats, and every thing will take one tick.

    I can also use the level and this lvl+1*lvl+1*lvl+1 thing to give the amount of xp from each enemy you earn and easily balance how many enemy's of a given level you need to kill to go up a level, at any given level.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice