Non-Global variable values in objects can't be changed???

C

CosmicDrifter

Guest
Please tell me I'm wrong about this...

Just for the sake of this question, let's say I create an object called, TimerOne; create an instance layer for it, and then place it in room0. To even take it one step further, I declare a variable:

TimerOne = obj_TimerOne;

Now in the create event of TimerOne, I declare a variable:

tspeed = 0;

Now, with that said, somewhere else in my game, I want modify the value of that variable. Now I would think that all I need to do is:

TimerOne.tspeed = 0.32;

and that would be it. However, that is not the case as far as I can tell. When I try to modify a user-defined variable in an object I created, the value doesn't change. Am I missing something here or is this the way it is? I just assumed objects in GS2 were similar to Classes in C# and C++… where you can set its properties to be read and written to.

Thanks for taking the time to read this. :)
 

TsukaYuriko

☄️
Forum Staff
Moderator
There's nothing that should stop you from doing this. If this isn't working the way you'd expect it to, show us your full code and provide full context for the location of each piece of code so we can see what's wrong. "somewhere else" doesn't tell us much.
 
R

relic1882

Guest
The objects have to be in the same room together and be active at the same time IIRC.
 

FrostyCat

Redemption Seeker
When you declare variables in the Create event, it only gets to run when you create an instance of it. There is no static scope in GML. Setting TimerOne.tspeed only sets the tspeed in one specific instance (if there is one at all, you'd get an error otherwise), not all future instances of obj_TimerOne.

The terminology in GML is not exactly the same as conventional OOP, and I think that's where your confusion is coming from. A general equivalence would be this:
  • "Object" in GML = "Class" in conventional OOP
  • "Instance" in GML = "Object" in conventional OOP
There are other technical differences (e.g. GML uses type-unsafe numeric handles while C++/C# are strong-typed), but the above is a workable starting point.

If you want a singular value that is available everywhere, set it up as a global variable. Your C++/C# voice will whine telling you it should be encapsulated or that globals are bad, but you don't always do as the Romans do when you aren't in Rome.
 
C

CosmicDrifter

Guest
Okay, here's a test that I did to verify this issue. Keep in mind that I already made the object and put it in room0, therefore the instance of this object exists in the eyes of GM2. Here's the code I wrote for it.
GML:
TimerOne = obj_TimerOne;
TimerOne.tspeed = 0.35;

tc = TimerOne.tspeed;

xx = tc;
To do the test, I put a break on the line that reads "xx = tc", and ran it in debug mode. When I checked the value of tc, it equaled 0, not 0.35.

FrostyCat, if I understand you correctly, what you're saying is that the only way I can do this is to make tspeed a global variable? As far as not being in Rome, it's a good thing I'm not, considering what's going in the world with this horrible virus going around. With that said, I hope everyone is safe and sound and that we all get through this without getting sick or even worst. Stay healthy out there.
 
Last edited by a moderator:

TsukaYuriko

☄️
Forum Staff
Moderator
You're not setting TimerOne's tspeed to a value, but setting tc to TimerOne.tspeed and then adding 0.5 to tc.

You're trying to refer to a variable by reference, which you can't.

Were you to reform this code into TimerOne.tspeed += 0.5;, that would have the desired effect, as you would then be actually modifying this variable rather than an entirely separate one.
 
C

CosmicDrifter

Guest
You're not setting TimerOne's tspeed to a value, but setting tc to TimerOne.tspeed and then adding 0.5 to tc.

You're trying to refer to a variable by reference, which you can't.

Were you to reform this code into TimerOne.tspeed += 0.5;, that would have the desired effect, as you would then be actually modifying this variable rather than an entirely separate one.
Ahhhh! I understand. I will give that a try in the morning. Thanks for your help. :)
 

Nidoking

Member
I'm having trouble following the thread with all the edits, but I think there's a fundamental misunderstanding going on here. What's the purpose of the TimerOne variable in the first place? Is it meant to store a type, or an instance? In C++ terms, are you trying to store a pointer to an object, or are you trying to store the name of a class so you can use reflection?

Either way, if the TimerOne variable exists within the obj_TimerOne instance you've created, as you seem to be implying here, then you can't access it from outside that instance without referring to the instance in the same way you're trying to do. You're trying to open a locked box using the key that's in the box. The TimerOne variable then serves no purpose. You might find more luck either using TimerOne = instance_find(obj_TimerOne, 0) to get the actual id of the instance, or just using instance_find(obj_TimerOne, 0) whenever you want to refer to the instance.
 
C

CosmicDrifter

Guest
I'm having trouble following the thread with all the edits, but I think there's a fundamental misunderstanding going on here. What's the purpose of the TimerOne variable in the first place?
The purpose of the TimerOne variable is to make my code easier to work with. Instead of having to type, "obj_TimerOne.whatever" every time I need to access it, I use the TimerOne variable. Not only does it save me time in writing my code, my code looks nicer and easier to read when I do it that way. I'm not a fan of writing "spaghetti code". From what I have seen in other people's code, this is also a common practice, so I don't get your confusion.

So after playing around with this and trying out the suggestions made on this thread, I have come to the conclusion that the only object variables that their values can be changed are GM2's Built-in-Variables, such as, x, y, speed and the rest.. which in my opinion is BS! I've been really enjoying working with this engine since I bought it, but some of its limitations are bothersome. I guess the only solution that I can think of would be to use global variables, which again, I'm not a big fan of.. but I'm glad the developers have implemented them in their engine.

Thanks everyone for responding. It was greatly appreciated. Great community here! :)
 

Nidoking

Member
Well, your conclusion is wrong. I define variables in objects and change them from other objects all the time. The problem isn't in what you're trying to do, but how you're doing it.

Is there some reason you don't just change the name of your obj_TimerOne to TimerOne, if all you're concerned with is how many characters you have to type?
 
C

CosmicDrifter

Guest
Well, your conclusion is wrong. I define variables in objects and change them from other objects all the time. The problem isn't in what you're trying to do, but how you're doing it.

Is there some reason you don't just change the name of your obj_TimerOne to TimerOne, if all you're concerned with is how many characters you have to type?
The name isn't the issue here. The issue is changing the value of one my own variables I created in the object.

If I were to do a simple test... which I have done already... keep in mind that the obj_TimerOne object already exists and I created a variable in that object called, tspeed. The code reads as follows:

GML:
obj_TimerOne.tspeed = 0.35; // I want to change tspeed's default value of 0 to this.

// Now I want to get the value of tspeed and maybe compare it to some other variables within my game.

var tc = obj_TimerOne.tspeed; // Now if this was a Built-in-Variable, I would have got a valid value.

// But instead, I get an error telling me the variable does not exist.
So from what I'm experiencing right now, the only object variables that are recognized are the Built-in-Variables. None of my own variables even show up in the drop down "I forgot what you call that" list of options you can apply to your code.

Now if you can explain to me what I'm doing wrong here, it would be greatly appreciated. :)
 

TailBit

Member
A instance of the object need to exist.. That's pretty much all that is missing.

Oh, there are also the order of operations that will give a error, because the create event to the timer will happen after this test object's create event..

So put the timer object into the room before the test object..

Or change the rooms creation order
 
Last edited:
C

CosmicDrifter

Guest
A instance of the object need to exist.. That's pretty much all that is missing.
I just said it exists, didn't I? It exists in room0 with all of my other objects. The only difference is the name of the instance layer it exists on.
 

TailBit

Member
Ah, sorry, my edit overlapped your post..

But yeah, it sounds like it could be the creation order that is the problem then. (see my prev. post)
 

Nidoking

Member
Now if you can explain to me what I'm doing wrong here, it would be greatly appreciated. :)
I'm attempting to do that, but you haven't provided enough information for me to figure that out. I see you have posted some code. However, there's not enough context. Which object and which event is it defined in?
 
C

CosmicDrifter

Guest
Ah, sorry, my edit overlapped your post..

But yeah, it sounds like it could be the creation order that is the problem then. (see my prev. post)
Saw it. Thanks. :)

I don't think the creation order is the issue, but I want to check to make sure first. Gonna do another test.
 
I'm not sure if this is the problem but it depends where you're referencing the tc variable from, declaring it as var tc means it's a local variable so can only be referenced during the short time it exists locally, if you don't include the "var" does it work then?
 
C

CosmicDrifter

Guest
Ah, sorry, my edit overlapped your post..

But yeah, it sounds like it could be the creation order that is the problem then. (see my prev. post)
Wow! That did work. I had to move that object towards the top of the Resource list. Okay, I take back what I said about GM2's limitations.. although I do think their Random Generator sucks, but that's another post. :p

Thanks TailBit. You've been a big help and now I think I can get rid of some of my global variables I created as a workaround.

Thanks to every one else that took the time to respond, too. Greatly appreciated. Y'all have a great day and stay healthy. :)
 
Top