GML Creating New Variables Outside the Create Event?

K

KieranSherman

Guest
Hey everyone,

I think I know the answer to this, and I think that answer is no :p but is there a way to create a new variable within an object outside of its create event?

I'm trying to make my code as easy as possible, and basically what I'm doing is creating an object (called oTile) which in its create event, creates a variable (called attributeMapObject), and initializes it with the create call to another object (called oAttributeMap).

Create Event of oTile
Code:
attributeMapObject = createAttributeMap();
createAttributeMap
Code:
var instance = instance_create_depth(0, 0, 0, oAttributeMap);

return instance;


Ideally I wouldn't have to do this, and I could get away with doing something like this:

Create Event of oTile
Code:
createAttributeMap(self);
createAttributeMap
Code:
/// @param object
var object = argument[0];
var instance = instance_create_depth(0, 0, 0, oAttributeMap);

//creating the new variable without it being previously defined
object.attributeMapObject = instance;


I'm mainly trying to do this because of naming. I have a bunch of functions that are responsible for manipulating maps, and they will work with any object passed into the function, as long as the object has a variable named attributeMapObject and that variable is assigned to an oAttributeMap instance. I know I could do what I'm trying to do with inheritance and parenting and whatnot, but I was just curious if there's any other way to do it.

Thank you!
 
Typically you would define the variable in the create event, but give it an "invalid" value. I like to use the keyword "noone" which equals -4. Then before you try to use it, make sure it isn't equal to your invalid value.

Although I might be off base... little hard to follow what your actual question is.
 
K

KieranSherman

Guest
Typically you would define the variable in the create event, but give it an "invalid" value. I like to use the keyword "noone" which equals -4. Then before you try to use it, make sure it isn't equal to your invalid value.

Although I might be off base... little hard to follow what your actual question is.
Sorry haha, what I'm really wondeirng is just if there is a way to create a variable within an object, without already defining it within the object's create event. So in this case, I wouldn't even want to assign a null value, because I wouldn't want the variable to exist until I called a script which would create it
 
H

Harest

Guest
I was going to say you can definitely do that as long as you're not trying to read the variable before it's set, but apparently i'm missing something cf. @Pixelated_Pope ^^.
 
As was going to say you can definitely do that as long as you're not trying to read the variable before it's set, but apparently i'm missing something
As long as you don't want to use YYC, you are absolutely right. It's not a good practice, though, and it's a practice that won't carry over to any other "big boy" programming language; variables must be declared.
 

toxigames

Member
Instead of variables you can use a ds_grid. This way you'll be able to dynamically add as many entries to the grid as you'd like and be free to use them basically like variables.
 
H

Harest

Guest
As long as you don't want to use YYC, you are absolutely right. It's not a good practice, though, and it's a practice that won't carry over to any other "big boy" programming language; variables must be declared.
Could you elaborate on why the YoYo Compiler couldn't be used ? Technically, we're already declaring variables that doesn't exist in the create event of an object when we declare temp variables right ? So nothing could technically stop you from removing the "var" keyword. The variables would be attached to the object and it'd still work since you're just using these variables here after declaring them in the current event (step event for instance).

But as you said, that's indeed not a good practice and many languages work with classes with declared set of variables and functions (including setters & getters). Plus in the example above, it doesn't have much utility. I'm just interested on the technical aspect here as i don't understand what would block you to do so currently.
 
Could you elaborate on why the YoYo Compiler couldn't be used ?
Okay... I'll be honest... I was 100% sure that if you tried to use a new variable in the step event without declaring it in the create event first, YYC would throw a compile error. I've seen this error a dozen times in the last couple of days. But now that I TRY to get it to show up... it won't. I know the error message exists, I just don't know why I can't get it to show up when I'm trying to prove it...

¯\_(ツ)_/¯

So I guess you can do whatever you want. The variable doesn't need to exist in the create event or when you create it, and you can initialize that variable at creation time. I'm still not understanding the original question, to be honest.
 

TheouAegis

Member
Go ahead and do whatever you want. Program however you want. Then test it. If it works, good. If it doesn't, you'll know for next time. People have told me not to do lots of things; I still did them because they worked for me, but I'm aware they won't always work -- I live in the now.
 
Top