How do I spawn objects according to choosen global variable

0 favourites
  • 5 posts
From the Asset Store
Globals 2.0
$3.99 USD
Globals 2.0 stores and group variables. You can also load and save data (variables) from/to JSON files.
  • Can't seem to figure out how i can create as many objects as one global variable shows.

    [quote:31dwjc8w]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.

  • 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)

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • 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"?

  • 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

  • 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! <img src="{SMILIES_PATH}/icon_e_smile.gif" alt=":)" title="Smile">

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