• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

SOLVED Assistance with code help

flyinian

Member
I am trying to add randomization to my combat system and experimenting with different code styles.

When I use "var enemy" or "totalcounttarget" directly to adjust the value of "Count 1", it doesn't work.

If I use the "Count 1" directly, it works but, I lose the randomization for my combat system.

I'm not sure what i'm doing/not doing is causing my code to break.

GML:
var enemy = choose(EnemyList[? "Count 1"]);//,EnemyList[? "Count 2"],EnemyList[? "Count 3"]]); // I have more than one enemy to choose from, but for testing purposes I only have the one enabled.
    
if (EnemyList[? "totalcount"] != EnemyList[? "totalcountcheck"])  // meaning if the player attacks the enemy and the enemy lost troops.
{
    
EnemyList[? "totalcountdifference"] = EnemyList[? "totalcountcheck"] - EnemyList[? "totalcount"];  //This works as intended. This calculates my damage to be done to "Count 1".

EnemyList[? "totalcounttarget"] = enemy; // This holds the target for the damage to be done.

if(enemy > 0) // if "Count 1" is greater than 0, do the following code. If there is any enemy troops to be destroyed.
{
//code in question
       //enemy -= EnemyList[? "totalcountdifference"];  // This doesn't work for effecting "Count 1".
    
      // EnemyList[? "Count 1"] -= EnemyList[? "totalcountcheck"] - EnemyList[? "totalcount"]; // This one works for effecting "Count 1" but, I lose the random selection.

     //EnemyList[? "totalcounttarget"] -= EnemyList[? "totalcountcheck"] - EnemyList[? "totalcount"]; // This doesn't work for effecting "Count 1" but, it does effect "totalcounttarget" value.


};

};
Any help is appreciated.

Thank you.
 

Binsk

Member
This is a matter of storing a variable by reference or by value.

When you have a variable defined it must be stored somewhere in your computer's memory. If you then say, "put this variable in a map" the computer can do one of two things:
  1. Make a copy of the variable in RAM and store this copy in the map (this is storing by value)
  2. Store the actual location of the variable in the map (this is storing by reference)
In case number 1, storing the value back into a variable (such as enemy) and changing it will not update the value in the map because you are modifying a copy which just has the same value. In case number 2, editing the value of enemy would also change the value in the map because they are literally the same value in memory.

Now, with GameMaker, almost everything is stored by value with the exception of some special array cases. This means whenever you store a value in a map, list, or anything, and want to edit it later you need to store the edited version back into the structure to keep the changes.

On to your map, remember that each value has a key and a value. "Count 1" is the key and whatever is stored in enemy is the value. If you want to choose one randomly you must choose a random key (as you did in your comments for your first line). However, because things are stored by value you will need to save your modified value back into the map which requires you know which key you initially pulled things from! Thus you need to edit your code to remember the key and store enemy back into the map with that same key once done editing.

For example:
Code:
var enemy_key = choose("Count 1", "Count 2", "Count 3"); // Choose a key but don't yet pull the value
var enemy = EnemyList[? enemy_key]; // Okay, pull the value

// [...] All your other code here

if (enemy > 0){
   enemy -= EnemyList[? "totalcountdifference"]; // We edit enemy, but the version in the map isn't yet updated!
   EnemyList[? enemy_key] = enemy; // Store the modified version back into the map! It is now updated!
}
That's all there is to it. I went on a spiel up above because you will come across this kind of thing fairly often.
 

flyinian

Member
This is a matter of storing a variable by reference or by value.

When you have a variable defined it must be stored somewhere in your computer's memory. If you then say, "put this variable in a map" the computer can do one of two things:
  1. Make a copy of the variable in RAM and store this copy in the map (this is storing by value)
  2. Store the actual location of the variable in the map (this is storing by reference)
In case number 1, storing the value back into a variable (such as enemy) and changing it will not update the value in the map because you are modifying a copy which just has the same value. In case number 2, editing the value of enemy would also change the value in the map because they are literally the same value in memory.

Now, with GameMaker, almost everything is stored by value with the exception of some special array cases. This means whenever you store a value in a map, list, or anything, and want to edit it later you need to store the edited version back into the structure to keep the changes.

On to your map, remember that each value has a key and a value. "Count 1" is the key and whatever is stored in enemy is the value. If you want to choose one randomly you must choose a random key (as you did in your comments for your first line). However, because things are stored by value you will need to save your modified value back into the map which requires you know which key you initially pulled things from! Thus you need to edit your code to remember the key and store enemy back into the map with that same key once done editing.

For example:
Code:
var enemy_key = choose("Count 1", "Count 2", "Count 3"); // Choose a key but don't yet pull the value
var enemy = EnemyList[? enemy_key]; // Okay, pull the value

// [...] All your other code here

if (enemy > 0){
   enemy -= EnemyList[? "totalcountdifference"]; // We edit enemy, but the version in the map isn't yet updated!
   EnemyList[? enemy_key] = enemy; // Store the modified version back into the map! It is now updated!
}


That's all there is to it. I went on a spiel up above because you will come across this kind of thing fairly often.
Thank you, I got it working. I can finally progress into a new issue.
 
Top