Picking from 2 objects at random to spawn

  • Hello,

    I'm pretty new to learning Construct 2 - well, very new - and am attempting to make my first game with it.

    The game is a simple top-down shooter , and is a kind of mix of Asteroids and Galaga. Nifty little thing.

    Anyway, in the game, UFOs are to fly by every 10-15 seconds. There are two versions, one that will fly from Left to Right, and one that will fly from Right to Left.

    Now, I've tried a couple approaches to make this work, including setting a variable at random, and then choosing which UFO to spawn based on that variable's setting. But it's not really getting the desired result - and I have a feeling it's not very elegantly done, either, and is probably wasting events. I'm still on the Free version (for now - just need the $$ avail to buy a license), so Events are at a premium, and I don't know how many this game will ultimately require.

    I'm guessing it's probably a very easy thing to do and, in my newbie's naivete, I just haven't figured it out. But, if someone could point me in the right direction, that would be great, and very much appreciated.

    Thank you!

  • If you are just wanting it to pick a between a couple choices you can use:

    choose(a, b)

    This will select one of the two..

    So could also be numbers like;

    choose(1, 2)

    for example.

    You can add as many choices as you want.

    choose(a, b, c, d, 2, 4, 15, pizza)

    The 'random' function is used for picking a random number between two numbers..

    You should use choose instead i think/

  • You can read about both these expressions here..

    https://www.scirra.com/manual/126/system-expressions

  • Ah!

    I hadn't come across anything about the Choose command.

    That's very similar to the last game editor I used... that should work fine. I'll go read up on it.

    Thanks!

    If you are just wanting it to pick a between a couple choices you can use:

    choose(a, b)

    This will select one of the two..

    So could also be numbers like;

    choose(1, 2)

    for example.

    You can add as many choices as you want.

    choose(a, b, c, d, 2, 4, 15, pizza)

    The 'random' function is used for picking a random number between two numbers..

    You should use choose instead i think/

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • So, I tried the "Choose" approach, but am not able to find a way to actually do it.

    In the System options, I have it set to every random(10,15) seconds, to create an object. The problem is, the "create object" option brings up the window that only lets you choose a single object. I can't actually enter Choose(x, y) anywhere...

    Is there some other method to creating objects where I can circumvent that whole process? Again, I've been looking, but not seeing any other option.

    Thanks!

  • You could do something like this:

    system every random (10,15) seconds - call function "create" with parameters 0 = choose (1,2,3)

    on function "create"

    -- if parameter = 1

    create object 1

    -- if param = 2

    create object 2

    -- if param = 3

    create object 3

  • Hi there, LittleStain,

    Thanks for that tip. I've tried getting the Function thing to work, as well as watching a couple videos explaining it. I thought I had it figured out, but I can't get it to work.

    At best, two UFOs spawn at once, each coming from opposite directions... then a few seconds later (much shorter than 10-15 seconds), a third flies by... and then that's it. Nothing else happens.

    I think I'm in a bit over my head here at this point. I'd thought it would be simple to simply tell Construct "every 10-15 seconds, create one of two indicated objects at random". But of course, my luck, it has to be a lot more involved/complicated than that.

  • I hope this makes it more clear for you..

    https://dl.dropboxusercontent.com/u/1750576/chooseexample.capx

  • A ha. Well that explains a lot... You're not even using Functions. I was trying to work it out using the function suggestion.

    Okay, that makes a lot more sense, now that I'm reading it. In my new-ness, I didn't even think of handling it that way. That's even simpler than how I'd been trying to do it.

    I was basically trying to adapt the method I'd used when working with GMS to this. The overall "flow" is similar, but the setup is different. I guess I need to try and "un-learn" anything GMS... it'll probably help a lot.

    Awesome, thank you for taking the time to help me out with that!

  • no worries <img src="{SMILIES_PATH}/icon_e_wink.gif" alt=";)" title="Wink">

    There is probably smarter ways than mine but this will get you by for now.

    If you want to read more about functions, here is a link to the manual page about it..

    https://www.scirra.com/manual/149/function

  • function example

    capx> http://s000.tinyupload.com/index.php?fi ... 9114024645

    ..sorry no sound <img src="{SMILIES_PATH}/icon_e_biggrin.gif" alt=":D" title="Very Happy">

  • Okay, sweet! Got it working.

    What's better, that same method will likely work for how I want to handle random items dropping from enemies when they're destroyed. Nice. I'm back on track.

    Thanks again for all the feedback/help, from everyone.

  • function example

    capx> ..sorry no sound

    Bah, no sound is fine. I'll be playing a Silent film version of my project for a while, so it fits the theme :p.

    Thanks

  • function example

    capx> ..sorry no sound

    I can't open that file. Says it was made with a newer version. I'm guessing you have a licensed version, and that the free edition is some versions behind.

    Well, I have it downloaded, so as soon as I can get it licensed (hopefully soon), I'll be able to check it out, and it'll be a good reference.

  • https://www.scirra.com/construct2/releases/r205.2

    example new link (Latest Stable r200)> http://s000.tinyupload.com/index.php?fi ... 9285259204

    ( extract files and open caproj)

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