Windows Variable set when instance created being reset?

Hi everyone

I have an issue with a variable being set when an instance is created, but not retaining that value after that instance has been created. The code I have used is:

var insty = instance_create_layer(1,1,"objectLayer",oTree)
with (insty) { daysOld = 6 }

With the object the variable is being passed to, I have created the variable under the "variable definitions" tab and set it as 0.

This creation code should override the "0" value and make it "6" from what I understand and have been reading on forums and the manual, yet it isn't working.

Any ideas?

Thanks
 

TsukaYuriko

☄️
Forum Staff
Moderator
It does override it. The question here is when you're expecting it to be overridden, and whether something else then overrides it back.

When you create an instance, its Create event will instantly and invariably be called. After the Create event finishes executing, whatever code you wrote after the instance's creation will run. So if you expect the update to happen before the Create event, you'll be disappointed.

Also, if anything else overrides the value after this override, it will be overridden again.


Therefore, I'm left with the question: How and when are you verifying that the value was overridden?
 
Hi. Thanks for he reply.

This is an order of what is happening:

1. I create the instance
2. Instance created, sets the variable to its default of "0" from the variable definitions tab (or the create event - I've tried both)
3. Now, the with code should set the variable to "6"
4. I verify what value is held, by seeing which sprite image is shown (frame 0 or frame 6). Its always frame 0 - the default.

The only way I have been able to make frame 6 show, is by setting the variable to 6 in the create event of the instance, and that works as usual. It really is as though the value isn't being carried over to the instance after it is created.

I've tried different versions of the code: such as,

var insty = instance_create_layer(1,1,"objectLayer",oTree)
with (insty) { daysOld = 6 }



var insty = instance_create_layer(1,1,"objectLayer",oTree)
insty.daysOld = 6



with (instance_create_layer(1,1,"objectLayer",oTree))
{
daysOld = 6
}

I think they are all valid, but nothing works.

I have also searched through the code via find/replace for any other code that might have changed the value of the variable "daysOld" and there is no other code that could account for it.
 
I just worked it out. For the record in case someone has a similar issue. It really was a problem with my logic and the flow of the code.

Nothing wrong with the code I wrote above - but the way I used the variable after the instance received it was the problem. (Basically, the way I was checking the value was built into the create event, so it happened before the new value was set.

Silly me. :)
 
Top