Question regarding "Else" and "on Created"

  • Hello,

    Tonight I was trying to fix a bug and I could not find out why until I stripped down my whole project to a tiny series of events. I identified my problem and I'm wondering how Else works in the example below

    https://imgur.com/a/3GiQ0tq

    (I don't know why but my images won't upload so I had to put it on imgur)

    So Titles " On created":

    1. if Test = 1 and "Test = 2 condition" in Else is disabled, Sun and Rainbow get destroyed

    2. if Test = 1 and "Test = 2 condition" in Else is enabled, only Rainbow gets destroyed

    I thought Else condition was ignored if the first part of a condition was true, but it seems not to be the case "on created", could you enlighten me? :)

    Thanks

  • My guess, you have put the sprite rainbow or sun on the layout in the editor. So there is a least 1 object of one of them on the layout at the start.

    If you now run your example a sprite will created and 'on created will be called' with test=0 and 'on start of layout will be called' simultaneously and another sprite will be created. Because of 'on created' is called at first with test=0 the second else will be executed and all rainbow sprites will be destroyed even the one that are created in the function. After that 'on created' will be called a second time with test = 1 or 2.

    I hope that makes a little sense and it's not totally confusing. It's hard for me to explain this in english.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Asmodean is probably right here, try sticking a breakpoint on the first action of "on start of layout" and the first sub condition of "tiles on created" to see when and how often those are being triggered.

  • Hello both of you!

    First thanks for taking the time to reply to me!

    I am not sure I understand, but all objects are actually present at the start of the layout. I only delete the Titles and recreate them based on the choose action result.

    I tried to put a breakpoint before posting and tried to understand but I got more confused. Also it's not possible in "On Created" action apparently to put a breakpoint from what I can see.

    I'm a bit confused by the result and why the else condition is actually entered, I guess to the fact "On Created" is asynchronous?

    I have attached the test project here in case you want to check it out: dropbox.com/s/1iv0bvonih85oma/test.c3p

    Thanks!

  • Asmodean is correct. On create is called for objects even if they are already placed. In fact they are called before On start of layout. If you want to keep your On create code, as opposed to just doing it differently, put On create inside a Group that is disabled, and then On start of layout, enable the group so that when your function gets called, it will work as expected.

  • When you make a Layout, you have all objects that layout will use sitting in the layout.

    A common thing people do in On Start of Layout Event is to Destroy all those objects, then they create them as needed. This is important to have these objects in the layout as it tells the layouts which objects to load into memory - if they are not in the layout, and if you do a Create Object action, it could cause your game to lag or pause while that object is loading into memory.

    The drawback of having the object already in the layout and using an OnCreated Event is that, the original object sitting in your layout actually gets created - therefore triggers the event before the game even starts.

    Personally I only use OnCreated to do cosmetic things, or things only pertaining to the object I'm creating.

    I would recommend what blackhornet suggests or make global boolean to shut off the first time you are in OnCreated so it won't do the action on the very first OnCreate.

  • Thanks a lot all of you, I understand now! I did not know the "On Created" event was actually triggered before the "start of Layout".

    It all makes sense now, thanks for the tips :)

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