• 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 Passing global variables to variable definitions?

FoxyOfJungle

Kazan Games
Hi,
Is there no way to pass global variables using "Variable definitions" of an object?

I defined particle_system_index and particle_index:



I put the variable here but it doesn't seem to recognize it...



I've tried switching to all types there in the selection but it doesn't work.

When the object is in the room and tries to create the particle:



But when I try to use it normally, it works:



I could use the object's creation code, but that's too meh... Besides, I used variable definitions on other objects, it's going to be weird I only do that in this one...
Thanks for listening.
 

TsukaYuriko

☄️
Forum Staff
Moderator
global.particle_system_river is not a real, it's a variable. A real is 0, 1, 2, 3.5, etc. This variable HOLDS a real. Or rather, it WILL hold a real at run time. You know that. The IDE can't. It only sees that the entered value is not - at the time - a real.

Set it to Expression, not Real. In Variable Definitions, you're not defining the final data type of the variable, but what is a valid input. The result of this expression will determine the data type.
 

FoxyOfJungle

Kazan Games
global.particle_system_river is not a real, it's a variable. A real is 0, 1, 2, 3.5, etc. This variable HOLDS a real. Or rather, it WILL hold a real at run time. You know that. The IDE can't. It only sees that the entered value is not - at the time - a real.

Set it to Expression, not Real. In Variable Definitions, you're not defining the final data type of the variable, but what is a valid input. The result of this expression will determine the data type.
That's the problem, there's no variable option here: 😅



Unfortunately it doesn't work when I put "Expression": 🥺



Thanks!
 

Nidoking

Member
What are you doing to ensure that those global variables are defined before the instance's Variable Definitions are processed?
 

Vusur

Member
I did a small test. Just to see, if the variable definition doesn't like globals or particles (unlikly, but why not).
Runtime: 2.3.3.434
IDE: 2.3.3.570

In a script, which simulates your rm_init. If rm_init does, what it typically does, it load the globals/stuff before the game starts for real. Scripts are also loaded before everything else. Just me being lazy here.

GML:
global.system = part_system_create();
global.part = part_type_create();
part_type_shape(global.part, pt_shape_cloud);

global.stri = "fish";
The variable definition:
var_def.PNG

My test object create event:

GML:
part_type_size(tester_part, 1, 2, 0, 0);
getter = tester_string + "_additional";
stopper = 0; //just for breakpoint
Works fine on my end. All variables set by the variable definition with type Exrpession hold the expected values. No error/warning or -1.
So the globals can't be it imho.
 

FoxyOfJungle

Kazan Games
I did a small test. Just to see, if the variable definition doesn't like globals or particles (unlikly, but why not).
Runtime: 2.3.3.434
IDE: 2.3.3.570

In a script, which simulates your rm_init. If rm_init does, what it typically does, it load the globals/stuff before the game starts for real. Scripts are also loaded before everything else. Just me being lazy here.

GML:
global.system = part_system_create();
global.part = part_type_create();
part_type_shape(global.part, pt_shape_cloud);

global.stri = "fish";
The variable definition:
View attachment 41477

My test object create event:

GML:
part_type_size(tester_part, 1, 2, 0, 0);
getter = tester_string + "_additional";
stopper = 0; //just for breakpoint
Works fine on my end. All variables set by the variable definition with type Exrpession hold the expected values. No error/warning or -1.
So the globals can't be it imho.
Thanks for taking the test.
Strange, I did the same procedure on an empty project and it returns -1 yet, see:


When I did this procedure at 0:53 minutes, even putting the "variable definition" to "Real" it doesn't recognize the global variable.

🤯
 
Last edited:

FoxyOfJungle

Kazan Games
The test project here:

Made as simple as possible.
 

Vusur

Member
Your test has a flaw. It's the create event. Think about it like this:

GML:
//variable definition will set it's default value defined for this instance (when double clicked on the instance and change there)
//run rest of create event

/// @description
my_value = -1; //overwrites default value from variable definition

alarm[0] = 120;
Do //my_value = -1; or remove it and it will show 12415. I tried it with your test project :)

Edit: Or in other words:

GML:
my_value = global.some_value //because this instance has it's variable definition overwritten

///@description
my_value = -1;

alarm[0] = 120;
 

TsukaYuriko

☄️
Forum Staff
Moderator
What are this test project and the second video supposed to demonstrate?

In the project, you define a variable with a default value of 0, then set it to -1 in Create, then set an alarm to output the value and that value is -1, the same you just set it to. That's the expected result.
In the video, you define two variable with a default value of -1, then set them to -1 in Create (so no change), then set an alarm to output the value and that value is -1, the same you just set it to and the same it's always been. That's the expected result.

None of this contains anything about globals, either. You define a global in the test project but then never use it. Did you forget something?
 

FoxyOfJungle

Kazan Games
Your test has a flaw. It's the create event. Think about it like this:

GML:
//variable definition will set it's default value defined for this instance (when double clicked on the instance and change there)
//run rest of create event

/// @description
my_value = -1; //overwrites default value from variable definition

alarm[0] = 120;
Do //my_value = -1; or remove it and it will show 12415. I tried it with your test project :)

Edit: Or in other words:

GML:
my_value = global.some_value //because this instance has it's variable definition overwritten

///@description
my_value = -1;

alarm[0] = 120;
What are this test project and the second video supposed to demonstrate?

In the project, you define a variable with a default value of 0, then set it to -1 in Create, then set an alarm to output the value and that value is -1, the same you just set it to. That's the expected result.
In the video, you define two variable with a default value of -1, then set them to -1 in Create (so no change), then set an alarm to output the value and that value is -1, the same you just set it to and the same it's always been. That's the expected result.

None of this contains anything about globals, either. You define a global in the test project but then never use it. Did you forget something?
Ohhh, you're right... So I can't put the variable in Create Event, lol!
Funny that this happens differently than when using the "creation code", because when using "variable definitions", the Create Event replaces the defined variables, whereas the creation code replaces the variables in the Create Event... This is weird but it's Game Maker so I can't complain :bunny:

Thanks you so much!
 

Vusur

Member
So I can't put the variable in Create Event, lol!
Atleast not with a writing statement. Just reading is fine let_me_read = my_value;. => let_me_read == 12432 for this instance.
Or particle_set_size(my_value, 1, 2, 0, 0) if my_value is a particle.

You could think about it this way. Variable Definition defines a default value. As soon as you do my_value = "something", you are overwriting the default.
 

Nidoking

Member
It's order of operations. Variable Definitions run before any Create events, so it's trying to get the value of the global variable before you've defined it. That's all.
 
It's order of operations. Variable Definitions run before any Create events, so it's trying to get the value of the global variable before you've defined it. That's all.
Should probs read the thread, lol. It's order of operations, but not in the way you're saying. The global variable was being defined in a script, which runs before the variable definitions. However, foxy was overwriting the variable definition in a create event later and thus the original variable definition had changed by the time the object actually wanted to access it.
 
Read the whole thing, and the first mention that the global variable was defined in a script is here:



Should probs read the thread before telling people to read the thread. Uh, lol I guess.
 

TsukaYuriko

☄️
Forum Staff
Moderator
The video in that post is now private. No way for anyone who didn't watch it to know what it contained... :)

Anyway, there are multiple causes that can lead to this problem and both of them are helpful to know about for anyone reading this topic. ;)
 
Top