Referencing object created after using Create Object (by name)

0 favourites
From the Asset Store
With this template you will learn how to use the GooglePlay Games native plugin
  • Could someone explain to me how using "Wait 0.0" to force an action into the next tick is supposed to help (work)?

    I've encountered another issue that is probably the same issue as above... I am populating a list from JSON, and while it populates, gets created and appears on screen just fine, the list items are not "available" to the system actions.

    I populate the "roomList" list from a "roomList.json" file early on.

    Then I am iterating over another JSON file, "strands.json," which contains details for cable strands containing a reference to a "locationA" that is in the roomList.

    Inside that second loop, I am attempting to create an instance of "roomList" for each item and pre-selecting the appropriate "room" in that "roomList" from the "locationA" field in the "strands.json" file.

    In a sub-event of the block that "creates roomList object" I am trying to make the selection... The logic to compare the list is not the issue as if I hard-code "roomList.selectItem(5)" I still get nothing selected.

    If I attempt to run a for-loop over the list (for 0 to 10: Log "roomList.ItemTextAt(loopindex))" I will get zero 11 times.

    If, however, I add a Wait 0.0 after the "create roomList object" block, I WILL get a selected item. (and my 0-10 loop will log each item in the list) but no other parts of my script will run after that.

    It is my understanding that adding a "Wait 0.0" forces the next action to execute on the next tick, but I really can't understand the flow and why it seems to "break" the loop that I put it in.

    I suppose I have 2 objectives: One is to really understand what is happening so I can, generally speaking, properly script in Construct. The other, of course, is to achieve my objective of creating, populating and pre-selecting an item from a list. It seems like I am jumping through way too many hoops to do something so simple - I've gotta be going about things the wrong way.

    Thank you!

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Sure. It has to do with when new objects can be picked. When you create new instances they can’t be picked in a general way till the next top level event (top level means an event to the far left). Basically the new instances aren’t added to the object list till the next top level event. Pick by uid is the one condition that can pick new objects regardless.

    More info here.

    construct.net/en/forum/construct-classic/help-support-using-construct-38/picking-problem-37830

    Anyways the wait 0 is a bit of a hack to work around this. It delays till the end of the event sheet before running. Which is often fine in a lot of cases.

  • rojohound, Thank you...

    I understand the concept of needing to wait until the next top-level event before the object that was created is "available." Well, I sort of do, lol. I can understand it conceptually, although I suppose I don't really understand what is actually happening under the hood.

    I've garnered that it's something like, since the events are happening asynchronously, whatever is happening in "this thread" doesn't know what's happening in "that thread" until both are finished and we hit the next tick.

    I keep running into scenarios where I THINK what I've scripted is going to run sequentially (for example, a For-Loop sub-event running within a For-Loop super-event) but the result I observe more often than not seems to be the outer loop and inner loop running concurrently.

    The Wait 0.0 is one of those things that falls into that category - As I've read, adding a "Wait" will put off the next action until after the Wait, but it will not pause the whole script (as in the case of, say, Adruino Programming). If I have in my script:

    Do Thing 1

    Wait 1.0

    Do Thing 2

    Do Thing 3

    What I would expect logically is that it will do exactly that... Thing 1, Wait 1.0sec, then do Thing 2 followed by Thing 3.

    If understand what I've read, however, what will really happen is:

    Thing 1, Thing 3 and then do Thing 2 when the Wait 1.0 time is up (which may or may not be before Thing 3 depending on how long the Wait Time is vs how long it takes to get to Thing 3).

    What I've experienced (or at least it seems) is that I end up with Thing 1, Wait Time, Thing 2 and then it's anyone's guess whether or not Thing 3 will ever happen.

    I'm sure that is not actually what is happening, but it certainly seems that way.

  • In C3 events are executed from top to bottom. Their order on the event sheet and the way they are nested is pretty important! It determines the order in which the actions are run, and the scope of picked instances.

    See this screenshot with my comments in it. The red numbers show the order in which the actions will run.

  • dop2000 - Thank you very much for the detailed example!

    I think I'm getting this.

    I program in PHP, JS as well as microcontrollers (variants of C, I suppose) on a pretty regular basis so a lot of my "flow logic" comes from those experiences. I have been scripting on Construct using the same logic, until I run into snags.

    I believe I have an understanding of the "nesting" of sub-events: They're like the Construct equivalent of putting an IF Statement (or any statement) inside of another IF statement?

    Example:

    //This is the first top-level event
    IF (the button is pressed){
    
    	Create SOMETHING; //this action will happen immediately.
    
    	//This is a sub-event
    	IF (a second button is pressed){
    
    		Create SOMETHING ELSE; //only if the other button is already being held down.
    	
    	} //end sub-event
    
    } //end FIRST top-level event
    
    //SECOND Top-Level Event:
    Change SOMETHING ELSE into ONE MORE THING;
    
    

    IF NO WAIT actions are involved, I would expect the sub-event inside the first top-level event will take place before the second-top-level event is executed:

    "SOMETHING" would be created, followed by "SOMETHING ELSE" (so long as the condition that made "SOMETHING" happen is still being met) and finally "SOMETHING ELSE" would be changed into "ONE MORE THING." - BY the time we get to the second top-level event, I would expect that SOMETHING and SOMETHING ELSE have been created.

    Again, without any WAIT actions, would this be correct, or would "Change SOMETHING ELSE into ONE MORE THING" still take place before the sub-event "Create SOMETHING ELSE" and thus fail?

    If this would work, then I think I can say I am getting it.

    Thank you, again!

  • Again, without any WAIT actions, would this be correct, or would "Change SOMETHING ELSE into ONE MORE THING" still take place before the sub-event "Create SOMETHING ELSE" and thus fail?

    I Construct, both SOMETHING and SOMETHING ELSE will be created before the second top-level event. (if there are no waits)

    Yeah, Construct logic is a bit different from other languages, it takes time to get used to it. One of the most important concepts in Construct is picking. Once you understand how it works, it all starts to make sense!

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