Understanding Functions - modifying a newly created object

Discussion and feedback on Construct 2

Post » Mon Jun 19, 2017 8:44 am

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.

Hi everyone,

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.
Image
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.
Image
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.
Image
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.
Image
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 : https://www.dropbox.com/sh/u73o34q4a3pz ... 9VKga?dl=0
Last edited by Rable on Mon Jun 19, 2017 10:22 am, edited 1 time in total.
B
33
S
6
G
1
Posts: 355
Reputation: 2,850

Post » Mon Jun 19, 2017 8:58 am

I think It works like this. Created or destroyed items does not really exist until the next Top level event. (Not function) I encountered the same problem before.

It's very weird behaviour I agree. You can work around this by using a variable as well.

-> On click
Create object "Sprite".
Set "Sprite" boolean variable True.

(Next top level event)
Bolean = True
Do what you intended..
Set bolean False.

Had problem with this sorting Z-order after an object was created. Very annoying.

https://www.scirra.com/forum/viewtopic.php?f=152&t=178366
Here's a thread about this issue.
Follow my progress on Twitter
or in this thread Archer Devlog
B
42
S
18
G
19
Posts: 1,061
Reputation: 14,056

Post » Mon Jun 19, 2017 9:19 am

Thanks for the reply. Very interesting. It means that it doesn't apply only to functions but to all created objects which should be accessed not directly below the "create" action.

So wait 0 or put the modification in the next top event are the only solutions. It explains many things and is definitely something important to know.
Thanks again! :)
B
33
S
6
G
1
Posts: 355
Reputation: 2,850

Post » Mon Jun 19, 2017 9:42 am

Oh wait...
It's a bit tricky, but based on @R0j0hound 's comment in the link you provided, there is a workaround based on the UID of the object, as "pick by UID" is an exception.

For anyone interested, this should be learned:

R0J0hound wrote:There are three lists internally:
Instance list
New list
Sol list

By default the sol list will be the instance list, which you then filter. When you create an object it's added to the new list and the sol is set to it. The pick all sets the sol to the instance list and since the new list isn't added yet those objects aren't picked. Then at the next "top level" event the new list is merged into the instance list.

You can search the forum for "top level" for some more discussions/explanations of this. In one of the change logs had a fix where before "pick all" would pick the new ones too. This was fixed to prevent cases of infinite loops.

Alternatively the pick by uid can pick objects from the new list. It's the exception to the rule.


So I tried this:
Image
...and it works! I can refer to the newly created object without needing a top event or a wait 0 seconds event.

For further explanations by Ashley, refer to this post.

[EDIT] It may be even simpler by using "function.returnvalue" instead of a global variable if there is only one object created in the function that you need to refer to.
B
33
S
6
G
1
Posts: 355
Reputation: 2,850


Return to Construct 2 General

Who is online

Users browsing this forum: No registered users and 12 guests