(SOLVED)Can't Assign variables to variables in the "Variable Definition" page of an object.

flyinian

Member
I am trying to assign a variable to a variable in the variable definition on the object's page.

EX:

GML:
//Here is the variables in question.
//I am assigning  _object an object. I am then assigning _SpriteX & _SpriteY the variable of the _object's X  & Y value which is determined through another object.
//I have these variables located in the "Variable Definition" page of the object and not in the create event.
//The x and y placement don't work right.
//If this is possible to do, what data type would this be considered when used in the "Variable Definition" page of the object?
//I am also using the parenting  and inheritance system with this object.
//Assigning variables to variables works fine If I don't use the variable definition page and go with the create event.


///---Used Object's Variable---///
_Object = obj_object;                                                        //This is the object to be used.

_SpriteX = _Object.x;                                                        //Sprite's X cordinate.
_SpriteY = _Object.y;                                                        //Sprite's Y cordinate.
 

Lukan

Gay Wizard Freak
If you're assigning obj_object to the _Object variable in the Variable Definitions page you need to make sure there is only one instance of it in the room with the object that's running.
Variable Definitions can only point to Object IDs and not Instance IDs.
 

flyinian

Member
If you're assigning obj_object to the _Object variable in the Variable Definitions page you need to make sure there is only one instance of it in the room with the object that's running.
Variable Definitions can only point to Object IDs and not Instance IDs.
So, The "variable Definition" page of the object cannot point to instance IDs?

Does this mean I can't use "instance_create_layer" if I am using "Variable Definition"?

What I am trying to do is,
i place obj_object1 into the room. This object spawns in the menu for that room(obj_object2).
That obj_object2 creates all of the needed buttons for the navigation within that room.

Since I am going to need a lot of buttons, I am creating a parent that has all of the needed code and variables. I am placing all of the variables into the "Variable Definitions" on the object's page.
 

Nidoking

Member
Use the Create event. Variable Definitions definitions have to have static scope (i.e. they can't refer to things in instances, they have to exist in a manner that's inherent to the game itself).
 

Lukan

Gay Wizard Freak
Variables should all be defined in variable definitions, but you cannot always assign the correct values there.
You can still create the variables in variable definitions, and assign them in the create event, or you can just use the create even for both.

The variable definition ui is basically there to help you keep track of all your vars in an object, it's not required use, just suggested.
It helps avoid conflicts and non assigned variable errors.

I create UI in a similar manner to this, you'd need to assign this variable at the time the instance is created to properly fetch its instance id.
 

flyinian

Member
Use the Create event. Variable Definitions definitions have to have static scope (i.e. they can't refer to things in instances, they have to exist in a manner that's inherent to the game itself).
Variables should all be defined in variable definitions, but you cannot always assign the correct values there.
You can still create the variables in variable definitions, and assign them in the create event, or you can just use the create even for both.

The variable definition ui is basically there to help you keep track of all your vars in an object, it's not required use, just suggested.
It helps avoid conflicts and non assigned variable errors.

I create UI in a similar manner to this, you'd need to assign this variable at the time the instance is created to properly fetch its instance id.
This has gotten rather complicated for me.

If I were to place the object into the room and use code within it's self to place its coordinates and such, would it work? Instead of having an object create it in the room and set its x and Y positions?

Would it just be easier to create all variables in the create event and ignore the "Variable Definition" page of the object. If so, what would be the best way to manage many objects with all similar codes.

ex: creating navigation buttons for the game.

I was copy and pasting code into each object and updating each individual object with updates, this is time consuming and would be hard to make additions, subtractions or changes to code.

Thanks for the help.
 

kburkhart84

Firehammer Games
I personally never have used the variable definition dialog, as you can create variables in any code on the object as long as it happens before there is an attempt to use that variable.

If I were to place the object into the room and use code within it's self to place its coordinates and such, would it work? Instead of having an object create it in the room and set its x and Y positions?
It would work...but if you have multiple instances then they would all go to the same place. Its more common to use the room editor to place positions but code in the variables that are going to be the same either in the create event or the variable definition dialog.
Would it just be easier to create all variables in the create event and ignore the "Variable Definition" page of the object. If so, what would be the best way to manage many objects with all similar codes.
The way I personally do this is with Instance creation code. Look it up in the manual. It lets you change variables for each individual instance. So, your buttons would have code to execute whatever script is stored in some variable when the button is pressed, and that code wouldn't change so you could keep it in the clicked event just fine. Then, your instance creation event would set that variable to whatever script you are calling. Note that when putting scripts in variable for later execution, you want to NOT put the parenthesis so it stores the script into the variable instead of actually calling the script and then returning the result.
This could also be used for the text display of the buttons. Have the draw code draw the string in some variable, and have the instance creation code set that string to whatever it needs to be for each button.

Another way some people do it is with parent/child objects, making different version inherit code from the parents. In the case of buttons, I don't think its necessary because those can be done with a simple couple of variables, but for other things, it can be a good alternative to instance creation code.
 

samspade

Member
Objects don't have variables. Instances do. So saying object_id.variable is wrong. It will work, but not the way you expect. Essentially, GM will find an instance of that object, which could even be the instance calling the code, and use that instance's variables instead.

If you place an instance of an object in the room editor, its x and y position are set by where you put it. If you place it by code using a function then they are set by the function.

If you are going to place instances of objects in the room editor, I thing object variables in the Variable Definition section are great and I use them all the time. If you are only going to create through code, then I wouldn't use them as (I don't think) they have much purpose outside of placing in the room editor. Think of object variables as essentially exposing an instance variable to you to directly edit for that instance, in the room editor. (They also run before the create event which can nice as well.)

If you are using objects as buttons and intend to place them using the room editor, then I think that using inheritance, object variables, and placing directly in the room editor is the simplest way to go.

It might be worth it to brush up on how objects and instances work:

Full Playlist

Inro video:

 

hippyman

Member
Use the Create event. Variable Definitions definitions have to have static scope (i.e. they can't refer to things in instances, they have to exist in a manner that's inherent to the game itself).
Object variables are just normal instance variables. They're very useful for initializing multiple instances with different variable configurations within the room editor but they don't have to be whatever the heck you said.

OP is misunderstanding the difference between objects and instances and you're piling on more incorrect information.
 

Nidoking

Member
Object variables are just normal instance variables. They're very useful for initializing multiple instances with different variable configurations within the room editor but they don't have to be whatever the heck you said.

OP is misunderstanding the difference between objects and instances and you're piling on more incorrect information.
Okay, then. If I'm wrong, then let's say I have an instance in the current room with a variable h holding the value 4. Now, tell me how I define a variable in the Variable Definitions list that refers to that instance's h in some way. If you want to correct people, you actually have to be right yourself.
 

hippyman

Member
Okay, then. If I'm wrong, then let's say I have an instance in the current room with a variable h holding the value 4. Now, tell me how I define a variable in the Variable Definitions list that refers to that instance's h in some way. If you want to correct people, you actually have to be right yourself.
You set the variable to be an expression. Then you place your instances in the room editor. You assign the instance id to the variable. You access all the instances variables.
 

hippyman

Member
Huh. Wow. It does work. Well, it still seems like a supremely bad idea, but okay.
You're ridiculous.

@flyinian While I do recommend checking out some of the starter tutorials in the yoyo tech blogs to get a better understanding on how things work, your answer is provided above. If you'd like a more in-depth explanation I'd be glad to help.
 

flyinian

Member
I guess one of the biggest questions that I have right now would be what can't you do with the Variable Definition page of the object?

I made a new project and started testing with the parenting and using the variable definition page, I still can't get it to work at all. It works fine if I use the create event.
 

samspade

Member
The variable definition section could in theory be used for all variables, but you shouldn't. It would quickly become clunky and unusable.

It's primary use is as a means of 'exposing' the variable ease of access when using the room editor (and to some extent with inheritance as it allows you to overwrite inherited variables without overwriting the create event). Essentially, it is a much, much nicer version of creation code, which was always a pain and I've never needed to use once since these got introduced.

If you don't need it for that purpose, I would use it. In other words, default to using the create event. Initialize all your variables there. Only use object variables when you want them to make your life a little easier.
 

flyinian

Member
The variable definition section could in theory be used for all variables, but you shouldn't. It would quickly become clunky and unusable.

It's primary use is as a means of 'exposing' the variable ease of access when using the room editor (and to some extent with inheritance as it allows you to overwrite inherited variables without overwriting the create event). Essentially, it is a much, much nicer version of creation code, which was always a pain and I've never needed to use once since these got introduced.

If you don't need it for that purpose, I would use it. In other words, default to using the create event. Initialize all your variables there. Only use object variables when you want them to make your life a little easier.

I was trying to use this method to speed things up by using the parenting and variable definition. I'll be taking another look at it tomorrow and if I can't make any progress I may just scrap it and go with individual objects with individual code/variables.

I mean once I get all of the buttons where I want them and with all the code I possibly want I wont be changing them ever again... right? it's good coding practice.

Thank you for the help.
 

Nidoking

Member
I use Variable Definitions for two purposes. The inheritance thing is the main one - If I have a parent object, I set all of the variables it will rely on as Variable Definitions with default values, and override those definitions in the child objects. Sometimes, I don't actually need to change anything in the child object other than defining those variables. If I used the Create event to define all of the variables, I'd have to inherit/override every time. The other way is for variables that will depend on the contents of a room, like having a switch that's tied to a door. I can put a door instance in a room, then put in the switch and set a variable with the instance ID of the door using a Variable Definition. The switch can now generically open and close whatever door instance is in that variable, and I can set each one individually.

The thing to keep in mind is that any variable definition, whether a Variable Definition or a statement in an event, evaluates its argument one time and assigns that value to the variable. You can't set a variable to track the value of another variable as it changes. You'll either have to use that other variable directly, or you'll have to keep checking the value for changes and update each time. In the example in your OP, your _Object variable would store a constant object ID, which is fine (as long as it's a singleton object - the instance ID would likely be a better choice), but the x and y values of that instance might change, and your _SpriteX and _SpriteY variables would keep their original values. If you just refer to _Object.x and _Object.y directly, then you'll always get the current values.
 

flyinian

Member
I use Variable Definitions for two purposes. The inheritance thing is the main one - If I have a parent object, I set all of the variables it will rely on as Variable Definitions with default values, and override those definitions in the child objects. Sometimes, I don't actually need to change anything in the child object other than defining those variables. If I used the Create event to define all of the variables, I'd have to inherit/override every time. The other way is for variables that will depend on the contents of a room, like having a switch that's tied to a door. I can put a door instance in a room, then put in the switch and set a variable with the instance ID of the door using a Variable Definition. The switch can now generically open and close whatever door instance is in that variable, and I can set each one individually.

The thing to keep in mind is that any variable definition, whether a Variable Definition or a statement in an event, evaluates its argument one time and assigns that value to the variable. You can't set a variable to track the value of another variable as it changes. You'll either have to use that other variable directly, or you'll have to keep checking the value for changes and update each time. In the example in your OP, your _Object variable would store a constant object ID, which is fine (as long as it's a singleton object - the instance ID would likely be a better choice), but the x and y values of that instance might change, and your _SpriteX and _SpriteY variables would keep their original values. If you just refer to _Object.x and _Object.y directly, then you'll always get the current values.
Thank you.
 
Top