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

One variable to = multiple objects?

K

Krenzathal77

Guest
Just wondering if there is a way to maybe assign one variable to multiple objects.

I have two different characters in my game and I was wondering if that instead of writing out separate bits of code for the enemy to deal with each character object for the same action I could just put them both in one variable so that either character could set it off if it happened to be them.

I tried this in the create event:

players = obj_player1 or obj_player2

But it didn't seem to work. I don't mind having separate bits of code for each but just in case there was a more efficient way.

Thanks for you time.
 

Tthecreator

Your Creator!

jo-thijs

Member
Just wondering if there is a way to maybe assign one variable to multiple objects.

I have two different characters in my game and I was wondering if that instead of writing out separate bits of code for the enemy to deal with each character object for the same action I could just put them both in one variable so that either character could set it off if it happened to be them.

I tried this in the create event:

players = obj_player1 or obj_player2

But it didn't seem to work. I don't mind having separate bits of code for each but just in case there was a more efficient way.

Thanks for you time.
I'll first explain what your piece of code there does.
When you use obj_player1, GameMaker replaces that by a number, which is called the object index.
In GameMaker studio, this object index equals the amount of preceding objects in the asset tree of your project.
The first object in your project has object index 0.
The second one has object index 1.
The third one has object index 2.

If obj_player1 is the first object in your project, then this:
Code:
obj_player1.x += 4;
would be equivalent to:
Code:
0.x += 4;
If obj_player2 is the first object in your project after obj_player1, then this:
Code:
obj_player2.x += 4;
would be equivalent to:
Code:
(obj_player1 + 1).x += 4;
Now that you know that when you write an object, you're actually writing numbers, we can take a look at what "or" does.
This:
Code:
a or b
will calculate the value of a.
If a is true, then b is not calculated and the whole expression returns 1.
Otherwise, b is also calculated and if b is true, the whole expression still returns 1.
If neither a or b are true, then the whole expression returns 0.

The expressions a and b return numbers however and not necessarily true or false.
In GameMaker a number is considered to be true if and only if it is larger than 0 after rounding the number.

So, if you'd write this:
Code:
show_message((-1) or 6.5);
you would get a message saying 1.

Now we cn return yo what your piece of code does.
You store in the variable player whether obj_player1 or obj_player2 is true.
An object_index is true if and only if the object isn't the first in your project.
Because obj_player1 and obj_player2 can't both be the first objects of your project, player will be the value 1.
Interpreting this as an object index again, the variable player will be the second object in your project.

Now, as for a solution to your problem.
By now, Tthecreator has already provided 1 solution: to use arrays to store every object index and to write your own scipts to deal with arrays.
While this is the most general solution to this problem, it isn't the most elegant (easiest) one in your case.
GameMaker has a parent system.

You can create an object obj_player.
You can then set the parent of obj_player1 and obj_player2 to obj_player.
If you now use obj_player somewhere, GameMaker's functions will execute it for both obj_player, obj_player1 and obj_player2.
 
Top