Update variable in step event without initialize it in the create event?

FoxyOfJungle

Kazan Games
It seems a little strange, but let's say I don't want to initialize a variable in the create event and just use the Draw event, is there any way to make that variable change without being started?

I managed to do something like this:

GML:
if !variable_instance_exists(object_index, "variable") var variable = 0; else var variable += 1;

draw_text(50, 50, variable);

The problem with this code is that it can only be used once in the draw event for the same object.
Say I have a button and I want to modify its opacity, but I don't want to initialize the variable in the create event, how would I do that?

My goal is to use only one event, the Draw Event (yes, I know it's not appropriate, but I'm doing some tests...)
Thanks!
 

FoxyOfJungle

Kazan Games
Just delete var
I added this var while I was creating the topic, after I had done the test, I thought it wouldn't make a difference (just the fact that it doesn't work). Anyway, even removing it, it still uses the same variable (it was to be expected). But I want to use them individually, let's say it's inside a script and I want to use the same script several times in the draw event, so I want them to be independent.


What I have:

GML:
if !variable_instance_exists(object_index, "alpha")
{
    alpha = 0;
}
else
{
    if (point_distance(150, 50, mouse_x, mouse_y) <  50)
    {
        alpha += 0.01;
    }
}

draw_text(150, 50, "alpha:" +string(alpha));




if !variable_instance_exists(object_index, "alpha")
{
    alpha = 0;
}
else
{
    if (point_distance(150, 150, mouse_x, mouse_y) <  50)
    {
        alpha += 0.01;
    }
}

draw_text(150, 150, "alpha:" +string(alpha));

 

chamaeleon

Member
I think they are the same thing, it didn't make a difference in the result, both return the instance ID:
The effect is the same if you have one instance. As soon as you have two or more, you'd get presumably unintended side-effects due to going through the object instead of a specific instance for the instance variable.
But I want to use them individually, let's say it's inside a script and I want to use the same script several times in the draw event, so I want them to be independent.
Scripts are not things you have data associated with. You have global, instance and local variables. Scripts/functions is just code, using those three types of variables, and the execution of code is done in the context of one instance at time, obviously, for the purpose of instance variables.

Without knowing more of what you really want to achieve, my first reaction is to implement a create event (despite what you said about not having one), create an array or ds_list, for instance in the create event with as many entries as are needed for the purpose of keep track of different variables, and pass in the required index in your script, so the script knows with value to work with.
 

FoxyOfJungle

Kazan Games
@chamaeleon
That is the question, I am making a program and I would like to avoid using the create event because I would have to keep creating variables just to define the alpha of a button and this messes things up a bit... if I for example have 20 buttons, I will have to create 20 variables. But apparently I don't have much choice... Maybe an array with 20 entries 🤔
I think I will have to use the create event anyway, thanks.
 

chamaeleon

Member
@chamaeleon
That is the question, I am making a program and I would like to avoid using the create event because I would have to keep creating variables just to define the alpha of a button and this messes things up a bit... if I for example have 20 buttons, I will have to create 20 variables. But apparently I don't have much choice... Maybe an array with 20 entries 🤔
I think I will have to use the create event anyway, thanks.
Are the buttons controlled by one instance or one instance per button? If one instance, use an array (which means a single variable which happens to hold an array) of size 20 to hold 20 individual values that is created in the create event, if one instance per button, create one variable per instance by simply declaring it in the create event.
 

FoxyOfJungle

Kazan Games
@chamaeleon
Basically, I use one instance to draw the entire GUI, no more objects, if I create an "object", it will be using structs.

If one instance, use an array (which means a single variable which happens to hold an array) of size 20 to hold 20 individual values that is created in the create event
Yeah, that's it.
Thanks.
 
Top