TL;DR : when created in a function, objects need a wait 0 second event before they can be modified. Is there any better way to access an object that has just been created in a function?
[EDIT] : Solved, see below.
I would like to ask you something I’ve run into multiple times and makes me scratch my head. Hopefully you can help me better understand how functions work.
I noticed that when creating an object inside a function, you can’t access this object directly in the event which called the function.
This will only set to scale 0.2 previously existing sprites.
However, you can access that object by inserting a “wait 0 seconds” event just after the function.
This will set all object to scale 0.2 including the newly created sprite.
When the object is already created, modifying it in any way takes effect immediately.
For example you can set an existing object instance variable in a function, then in the same event that called the function access this object based on this newly set instance variable without waiting 0 seconds.
The object is immediately destroyed.
It goes as far as making a function which set an object’s instance variable, then creates a new identical object with the same instance variable set to the same value –
Then in the event which called the function, deleting all objects with that given value, will only destroy the old object, not the new one.
Only the old objects are deleted. The newly create object is not deleted, even though its number value is correctly and immediately set to 5.
Please note that everything behaves exactly the same in C2 and C3.
So I have to questions:
1) Is it normal? How do you explain that behavior?
2) What is the most correct way to access an object which was newly created in a function
You can find the Capx with the different tests here :