Understanding Functions - modifying a newly created object

0 favourites
  • 4 posts
From the Asset Store
This is a single chapter from the "Construct Starter Kit Collection". It is the Student Workbook for its Workshop.
  • 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.

    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 : https://www.dropbox.com/sh/u73o34q4a3pz ... 9VKga?dl=0

  • 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.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • 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!

  • 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:

    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:

    ...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.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)