How do I spawn objects according to choosen global variable

Get help using Construct 2

Post » Tue May 16, 2017 3:19 pm

Can't seem to figure out how i can create as many objects as one global variable shows.

Example:
On Start of Layout:
Function Call: "newrow"

On Function "newrow":
System = Set Cube to: Choose(1,2,3,4,5,6,7)
Function Call "cubespawn"

On Function "cubespawn":
Pick a random GridSystem instance: Here i want to create the objects according to what was chosen for the global variable eg: if it choose to set the variable to 4, i want 4 objects being created at once


Please do tell, if there is any easier way for doing this.
Im kinda stuck from here on, been trying a lot of stuff but not really gotten my result i wanted.
Hope you're able to help me.
B
11
S
3
Posts: 31
Reputation: 814

Post » Tue May 16, 2017 7:44 pm

@Chansen
Try passing Cube as paramater (0) from Function "newrow":

On Function "newrow":
System = Set Cube to :Choose(1,2,3,4,5,6,7)
Function Call "cubespawn"(Cube)
B
20
S
8
G
3
Posts: 458
Reputation: 4,055

Post » Tue May 16, 2017 9:25 pm

mekonbekon wrote:@Chansen
Try passing Cube as paramater (0) from Function "newrow":

On Function "newrow":
System = Set Cube to :Choose(1,2,3,4,5,6,7)
Function Call "cubespawn"(Cube)


Thank you for your reply, im not quite familier with the f.param yet, i don't understand the way it works.
Could you make an more indept exampel of what you mean?

Also how do i create the objects i randomly choose On Function "newrow"?
B
11
S
3
Posts: 31
Reputation: 814

Post » Tue May 16, 2017 11:09 pm

Give the spawners a instance boolean 'occupied'

On Function "cubespawn"

______sub System > pick all (spawners)
___________Action set boolean 'occupied' for Spawers = false

_____ System > Repeat (global var Cube times)
_________sub > Spawner > is boolean instance variable set ('occupied')
_________System > pick random instance (spawner)
_________ _______action .. spawn an object
________________.... set boolean 'occupied' for Spawers = true
B
33
S
18
G
28
Posts: 2,493
Reputation: 20,950

Post » Wed May 17, 2017 9:03 am

@Chansen

You can read about how to set parameters in the functions section of the manual: https://www.scirra.com/manual/149/function

If you're going to be using functions a lot it's well worth familiarising yourself with parameters, they're very useful.

Here's a capx of your code that includes a parameter on the "cubespawn" function call:

https://www.dropbox.com/s/9rvwjo10rbvs2 ... .capx?dl=0

In event 3 I use an action to pass the global variable Cube as parameter 0 on Function Call "cubespawn".

You can add a parameter field to the Call function dialog by clicking the "Add parameter" highlight text. You can pass numbers, variables and strings as parameters.

In event 4, On the "cubespawn" function being called, it uses the passed parameter 0 value of Cube in a repeat loop. This loop creates a sprite for each iteration, using the repeat loopindex to step up the sprites X position.


I've also included another way to spawn a row of objects that may be of use, depending upon how flexible you need the system to be.
You could also integrate "newrow" and "cubespawn", if there is a one to one relationship between them.

Hope that helps! :)
B
20
S
8
G
3
Posts: 458
Reputation: 4,055


Return to How do I....?

Who is online

Users browsing this forum: Euduardo and 25 guests