1. Hello Guest! It's with a heavy heart that we must announce the removal of the Legacy GMC Archive. If you wish to save anything from it, now's the time! Please see this topic for more information.
    Dismiss Notice

GM:S 1.4 [SOLVED] how to make a global variable from a string name

Discussion in 'Programming' started by jobjorgos, Jan 17, 2018.

  1. jobjorgos

    jobjorgos Member

    Jun 23, 2016
    Im wondering how I can edit the value of a global variable from a string name as you can see below.

    SCRIPT: scr_do_damage_to_enemy:
    a = argument0; //the id of the enemy. it is just a number of 0 between 29 for every unique enemy
    b = 'global.enemyhp'+string(a);
    enemy_id = real(b);
    if global.weapon == 0{
          enemy_id -=1; //should remove 1HP from the enemy (not working right now too bad)
    if global.weapon == 1{
          enemy_id -=2; //should remove 2HP from the enemy (not working right now too bad)
    if global.weapon == 2{
          enemy_id -=6; //should remove 6HP from the enemy (not working right now too bad)
    if global.weapon == 3{
          enemy_id -=15; //should remove 15HP from the enemy (not working right now too bad)
    Somehow it does not affect the value of global.enemyhp0, global.enemyhp1, global.enemyhp2 etc.
    I have about 40 different enemies and 30 different weapons, that would requires 40x30=1200 cases in a script which would make it way too big and messy.
    Thats why I made this script to automate this and save time.
    Last edited: Jan 17, 2018
  2. Simon Gust

    Simon Gust Member

    Nov 15, 2016
    Unfortunately, that is not how variables work. Their names aren't a string in memory, their names are only for you and how you access them.
    The real() function also only can make strings like "12392" to a number.

    But if you already get an enemy id somehow (argument0) you can just edit it's hp variable.
    var ID = argument0;
    var damage = 0;
    switch (global.weapon)
    case 0: damage = 1; break;
    case 1: damage = 2; break;
    case 2: damage = 6; break;
    case 3: damage = 15; break;
    ID.hp -= damage;
    assuming each enemy had an hp variable.
    And also assuming the place you call the script from has collision functions that return the id of enemy instances.
    You don't have to give ids manually if it's only for combat. They all have their own automatially.
    jobjorgos likes this.
  3. Paskaler

    Paskaler Member

    Jul 4, 2016
    You shouldn't use global.enemyhp0, global.enemyhp1 and so on. You should simply make a hp instance variable inside the enemy object and then use something like the script @Simon Gust posted above.
    jobjorgos likes this.
  4. jobjorgos

    jobjorgos Member

    Jun 23, 2016
    Ah good to know that I should not use variables this way. you advise seems to be very valuable info to me because it made me to achieve to fix the problem! The switch system you advised Simon Gust worked out very well! So this is what I did afterall:

    SCRIPT: scr_do_weapon_damage
    enemy_id = argument0;
    //gonna make a switch of this also
    if global.weapon==0 damage = 1;
    if global.weapon==1 damage = 2;
    if global.weapon==2 damage = 6;
    if global.weapon==3 damage = 15;
    if global.weapon==4 damage = 18;
    if global.weapon==5 damage = 25;
    if global.weapon==6 damage = 35;
    if global.weapon==7 damage = 45;
    if global.weapon==8 damage = 55;
    if global.weapon==9 damage = 65;
    if global.weapon==10 damage = 75;
    if global.weapon==11 damage = 85;
    if global.weapon==12 damage = 95;
    if global.weapon==13 damage = 105;
    if global.weapon==14 damage = 115;
    if global.weapon==15 damage = 125;
    if global.weapon==16 damage = 135;
    if global.weapon==17 damage = 145;
    if global.weapon==18 damage = 155;
    if global.weapon==19 damage = 165;
    if global.weapon==20 damage = 175;
    switch (enemy_id)
    case 0: global.enemyhp0 -= damage; break;
    case 1: global.enemyhp1 -= damage; break;
    case 2: global.enemyhp2 -= damage; break;
    case 3: global.enemyhp3 -= damage; break;
    case 4: global.enemyhp4 -= damage; break;
    case 5: global.enemyhp5 -= damage; break;
    case 6: global.enemyhp6 -= damage; break;
    case 7: global.enemyhp7 -= damage; break;
    case 8: global.enemyhp8 -= damage; break;
    case 9: global.enemyhp9 -= damage; break;
    case 10: global.enemyhp10 -= damage; break;
    case 11: global.enemyhp11 -= damage; break;
    case 12: global.enemyhp12 -= damage; break;
    case 13: global.enemyhp13 -= damage; break;
    case 14: global.enemyhp14 -= damage; break;
    case 15: global.enemyhp15 -= damage; break;
    case 16: global.enemyhp16 -= damage; break;
    case 19: global.enemyhp19 -= damage; break;
    case 20: global.enemyhp20 -= damage; break;
    case 21: global.enemyhp21 -= damage; break;
    case 23: global.enemyhp23 -= damage; break;
    case 24: global.enemyhp24 -= damage; break;
    case 25: global.enemyhp25 -= damage; break;
    case 26: global.enemyhp26 -= damage; break;
    case 27: global.enemyhp27 -= damage; break;
    case 28: global.enemyhp28 -= damage; break;
    case 29: global.enemyhp29 -= damage; break;
    I gonna make a switch of the weapon also, I guess it would make it a little easyer and effecienter.
    Thanks alot for helping me solving this out :)

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