# Picking from 2 objects at random to spawn

Get help using Construct 2

### » Sat May 23, 2015 7:29 pm

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!
B
7
S
1
Posts: 14
Reputation: 455

### » Sat May 23, 2015 8:20 pm

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/
Last edited by UberDark on Sat May 23, 2015 8:57 pm, edited 5 times in total.
B
54
S
7
G
8
Posts: 150
Reputation: 5,817

### » Sat May 23, 2015 8:25 pm

You can read about both these expressions here..
https://www.scirra.com/manual/126/system-expressions
Last edited by UberDark on Sun May 24, 2015 12:07 am, edited 1 time in total.
B
54
S
7
G
8
Posts: 150
Reputation: 5,817

### » Sat May 23, 2015 9:21 pm

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!

UberDark wrote: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/
B
7
S
1
Posts: 14
Reputation: 455

### » Sat May 23, 2015 9:43 pm

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!
B
7
S
1
Posts: 14
Reputation: 455

### » Sat May 23, 2015 9:52 pm

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
I told my dentist I had trouble with my teeth and asked her to fix it without looking in my mouth..
B
54
S
16
G
8
Posts: 6,160
Reputation: 19,775

### » Sat May 23, 2015 10:35 pm

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.
B
7
S
1
Posts: 14
Reputation: 455

### » Sat May 23, 2015 10:44 pm

@MiToVo
I hope this makes it more clear for you..
https://dl.dropboxusercontent.com/u/1750576/chooseexample.capx
B
54
S
7
G
8
Posts: 150
Reputation: 5,817

### » Sat May 23, 2015 11:45 pm

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!
B
7
S
1
Posts: 14
Reputation: 455

### » Sun May 24, 2015 12:01 am

no worries

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
B
54
S
7
G
8
Posts: 150
Reputation: 5,817

Next