How do I use Else & Variables correctly?

  • I was wondering if anyone could suggest what I am doing wrong here?

    I am trying to use Else correctly with variables and not succeeding! In this example, I am trying to produce 5 objects at coordinates held in variables and using Else to determine which to use and using 'repeat' to run through a function 5 times to produce the 5 objects.

    The function appears to be triggering the object creation more than once in the same repeat, so while the function is repeated five times, many more than five objects are created - often different quantities each time.

    I am guessing I am doing something wrong with the way I'm using Else, as it appears the variable condition is being triggered more than once. I had anticipated that the function would end once the first condition was met, but that doesn't seem to be the case and it seems to be triggering again once another condition is met.

    If anyone could shed some light onto what I am doing wrong here, I'd be most grateful - and if there's a better way to approach this, I'm all ears :)

    I've attached a link to the c3p file - example file

    Thanks!

    Kate

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Hi, you need to place the conditions following the else into the same event block as the else.

    Also, if you're calling from a function you don't need the trigger once conditions.

  • I had a go at simplifying your code:

    dropbox.com/s/9r1s1hlwtcphf8p/spawnObs.c3p

    ...I've included comments in the code to explain the changes. The X positions that the objects spawn at comply with your current logic, although given the way you structured your events I'm not sure if this outcome is as you intended:

    In your event 4 you set g_posX to either 190 or 550; this means the first event in function makeobjs will not fire as g_posX is never 370. As a result your subsequent events are equivalent to setting g_posX using a single choose(190, 370, 370, 550) action i.e. 370 will be chosen twice as often as the other two positions. I've modified the actions to reflect this, but if that isn't what you wanted let me know what you need and I'll have another pop at it.

  • Thank you so much for that - it's much appreciated! I think that's going to work out perfectly and thank you so much for tidying up the event sheet and giving me some tips for better optimization of code - it's helped me get a better understanding of how it all works!

    Kate

  • No problem, happy to help :-)

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