• 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!

GML Creating Additional Variables Using Code

R

returntoit

Guest
Hello!

I'm trying to combine variables using strings and set them equal to a different variable. Example:

//In a gun / trap.
special = "slow" // or bleed, or several other effects
amount = 5
duration = 100

The explosion / bullet then gets the arguments passed to it and then passed to the enemy or enemies.
Then enemy interrupts what the special should do to it E.g. slow speed, reduce armor and grabs the amount and duration just fine.

The "potential" problem is that if the enemy was to set off two traps or get hit by two bullets at the same time with different effects, I'm not sure how it would handle it. E.g. Sets off slow and bleed traps and gets two variables both named special with different values. I've not seen this happen nor taken the time to set up a test environment and try it so maybe its a non issue, but the situation is likely to eventually occur in my game.

Even still I would like to know if there is a way to combine strings to create new variables. E.g:

special = "slow"
amount = 5
duration = 100
string(special)_amount = amount
string(special)_duration = duration

With everything hopefully coming out as:
slow_amount = 5
slow_duration = 100

I've tried various expressions and operations as well as brackets and plus signs.
If anyone has a solution as to how to do that I'd being interested in hearing it.

Otherwise I think the next best step is to look into storing my specials in an Effects array within the enemy that way I can just add effects to that array. I think with 2d arrays I could keep the association with the effect and the values without it getting mixed up. but I'm not entirely familiar with them yet.
Also if game maker has built in functionality to handle two instances setting a variable in another instance at the same time and I'm worrying for nothing, that would be good to know. but from what I understand about the step procedure, at very least something would be lost.

Any solutions, advice, references, resources or telling me to completely overhaul this system with a better one (and briefly why its better) would be appreciated.
 

Mick

Member
You can't create new variables like you are trying to, instead you can use a ds_map for this, look up how to use them in the manual, quite simple. I'm sure you can approach the problem in multiple other ways but ds_maps and other data structures are good to know about.
 

Xer0botXer0

Senpai
2D arrays are quite simple, or will be!

Variables:

Info:
Variables must be declared before use. You can't set var_x = var_y when you haven't declared var_y beforehand.
Variables are declared with a specific data type, You've got integer, string, Boolean and more complex types. Therefore you cannot do this var_msg_string = var_score_int when the first variable is a string, and the second an integer.
How ever you can do this var_msg_string = string(var_score_int);

Variable scope: variable scope is where you specify what can access a variable.
Instance var: declared in a create event of an object therefore is unique to each instance. can be accessed by any other instance using obj_name.variable_name
local var: declared using the var function and and lasts until the code block has finished running. cannot be accessed by other instances. if it can I wouldn't recommend it.
global var: declared in a create event and can be accessed from any object. using global.var_name
built in vars are the variables like x,y,speed,hspeed, and so on.

syntax:
variable_name expression value(data_type)
Health = 100;

Take note that the above information may apply to arrays as well.

When to move over to a 1D array ? First we need to look at what value a 1D array holds, and then the syntax.

The value of a 1D array and multidimensional arrays is that they can be looped through, looping through arrays allows you to do filtering, sorting and a lot more. And simple compared to having multiple variables.
You have to initialize the max size of an array in gml. Else you'll get an out of bounds error later on.
Syntax:
array_name [array_pos] expression value(data_type)
Code:
arr_Apple[0] = "Apple"; //name
arr_Apple[1] = 15; //HP from apple
arr_Apple[2] = spr_apple_green; //the sprite the apple will use
So as you can see there's not much difference in this case compared to using variables, but this is pretty much how an array is going to look.

Now as you can see we've named the array to arr_Apple, so we know that the information in this 1D array is going to be about an apple right.
An apple, which means this array is only capable of holding information for one item. how ever you could rename it to arr_Fruits and then say well every 3 (so 0-2,3-5,6-8) positions hold information for a different fruit.
How ever that's when you'd rather use a 2D array, you've got ideas for multiple items and you want to store them together.

Syntax:
array_name[array_pos(1st dim),array_pos(2nd dim)] expression value(data_type)

Now instead of using every three values like we did in a 1D array, we simple use the first dimension within the 2D array to specify the item index(which item will the 2nd dimension indexes associate to).
Code:
arr_Fruit[0,0] = "Apple";//name (string)
arr_Fruit[0,1] = 15; //HP value (int)
arr_Fruit[0,2] = spr_apple_green; //sprite (int)

arr_Fruit[1,0] = "Banana"; //name (string)
arr_Fruit[1,1] = 10; //HP value (int)
arr_Fruit[1,2] = spr_banana_1; //sprite (int)

arr_Fruit[2,0] = "Mango"; //name (string)
arr_Fruit[2,1] = 25; //HP value (int)
arr_Fruit[2,2] = spr_Mango_1; //sprite (int)
so now you've got multiple fruit in this 2d array, so if you want to query, I've just eaten a mango, how much hp does it give me ?
Code:
for (i = 0; i < array_height_2d(arr_Fruit); i ++)
{
if arr_Fruit[i,0] == "Mango"
{
health += arr_Fruit[i,1];
break;
}
}
So this is where 2D arrays are great, now obviously when you write code for a game it's going to look different, this is just to show the use.
You can loop through the array, we use array_height_2d because say you're continuously adding new entries into the array, you' dthen have to update all the code looping through it, in this case it always has the max size of the first dimension in the array.

You then use an if statement and using the value of 'i' as the index for your 2D array you loop through the array whilst using the second dimension's value to compare with.
You then increment health by the second dimensions value associated with the first dimension that we've established is equal to the mango which (for example sake) is what the player has eaten.
We've also got a break in there because there's no need in that case to carry on looping as we've achieved the purpose of the for loop.

Now let's look at your situation, you want to differentiate between traps that the player interacts with, a 2D array would work for this.
Notice how in the example 2D array we've created different fruit by iterating on the first first dimension, you can do the same for traps and their properties, I call the second dimension values of the array the trap in minds' properties.
Code:
arr_trap[0,0] = "Bleed"; //Effect name
arr_trap[0,1] = 50; //HP drain
arr_trap[0,2] = 5; //Drain period

arr_trap[1,0] = "cut"; //Effect name
arr_trap[1,1] = 15; //HP drain
arr_trap[1,2] = 1; //Drain period
So you create the different traps or effect slots with the first dimension, then populate that trap or effects properties with the second dimension
In fact you're talking about getting shot by two different bullets each with a different effect.

So what you would want to do is give each trap or object that's going to have a damaging effect to the player a variable var_ref_trap and equal it to the first dimension with this 2D array.
So now when a player interacts how ever you've coded this with a trap or bullet or such, the player then uses "with other" or how ever you are referencing the interacting object, you'd use the local scope accessor type instance_ref.ref_to_variable and now you'd have a reference to this 2D array you've created, you can then access the properties of a specific effect using the value you've retrieved.

Im not sure if your bullets have multiple effects or not but from what I can tell this should be able to give you some insight on how to go about using 2D arrays.
 
Top