How to spawn a random object from a set

0 favourites
  • 9 posts
From the Asset Store
This is a single chapter from the "Construct Starter Kit Collection". It is the Student Workbook for its Workshop.
  • How do I spawn a random object from a set of multiple objects? For example...

    When right arrow key is pressed, spawn a yellow OR red OR blue OR green car at position (0,0).

    Thank you in advance for any help you can offer.

  • Yann would probsbly suggest useing "Choose" I'd go for an array to map an integer to the "Name" of the instance to spawn.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • And why not simply a random(0,car.AnimationFrameCount) ?

    Example capx

  • Ill give you a bit more information to see which is the best route to take. The goal is to have a continuous level which generates the next room (object) by taking a random one from a list. Each room will have its own different collision and the rooms will be spawned on the location of another object.

    Thank you for your help so far, just wondering what you think suits the above goal the best?

  • Weishaupt > not choose, more what kyat said (:

    Sheep > still need more information, what kind of gameplay, how would you use these rooms, Even without the randomness, how do you create these room? We have to see the big picture to give you an usable answer.

  • It will be a sideways scrolling platformer game. The level will be never ending. The level will be randomly generated from a set of preset level sections rather than the game actually generating random content.

    Example...

    Player starts in section 'StartOfLevel'. This section is 1000px wide. At some point the game needs to decide what the next section of the level will be, to attach to the end of the 'StartOfLevel' section. These add-on sections will be smaller (maybe 300px wide) and are pre-designed.

    The purpose of the add-on sections is to give the level variation as the level progresses.

    If you need any more information then just ask me :)

    Thank you

  • I see, if it wasn't a continuous scrolling thingy, the answer would be easy just a got to layout "room"&random(roomcount) but here it's something else.

    Hmmm I know

    You need to use two dummy sprite which would be used as a start and end point

    You need to use the same objectType for ALL the elements of your room.

    Just use differents animation Frame with an animation speed set to 0

    And set the initial frame (in property panel) to the corresponding frame for the object you want to use

    This unique sprite will have an instance variable named 'room' of type number which will hold the number of the room.

    You will have the startDummy positionned at the connexion point between two room at the far left, and the endDummy positionned at the connexion point between two rooms at the far right

    Each Dummy object will also have an instance variable named 'room'

    The idea is that at start of frame you'll create an array with all the positions relative to startDummy (this dummy will drive all the room) and all the corresponding animation (to have the right image showing) if you use different angles you'll also have to store them.

    In practice that should look like :

    System: on start of layout
      System: foreach startDummy
        Sprite: room = startDummy.room
        System: foreach sprite
          -> Array: Set value at (startDummy.room,loopindex+1,0) to sprite.X-startDummy.X
          -> Array: Set value at (startDummy.room,loopindex+1,1) to sprite.Y-startDummy.Y
          -> Array: Set value at (startDummy.room,loopindex+1,2) to sprite.animationFrame
          // Storing the number of object+1
          -> Array: Set value at (startDummy.room,0,3) to loopindex+1
        endDummy: room = startDummy.room
          -> Array: Set value at (startDummy.room,0,0) to endDummy.X-startDummy.X
          -> Array: Set value at (startDummy.room,0,1) to endDummy.Y-startDummy.Y
      // Cleaning up  
      -> startDummy: destroy
      -> endDummy: destroy
      -> sprite: destroy
    

    Basically the Array will then contain a map of each room with the startDummy as an origin. And in the first index, you'll have the X and Y position where you have to attach the next room.

    Now to spawn rooms, you'll have to keep in check the position of the next room to spawn in two variable (for X and Y) (maybe if it's all at the same height level you can avoid storing/using Y)

    And You'll have to loop through all the values of a room stored in the array, create Sprite, position it from the current new room to spawn, and set the proper animation frame.

    Let see

    Global Variable nextX = 0  //X position of the nextRoom
    Global Variable nextY = 0 //Y position of the nextRoom
    // next room is near the border of the window
    // spawn another room
    // (I assume you always scroll to player and I add 100 as a security margin)
    System: nextX < Player.X+WindowWidth/2+100
      Local Variable randRoom=0  // which room?
      // random(5) will return a random number from 0 to 4
      -> Set randRoom to random(5)
      // create the room
      System: for "" from 1 to Array(randRoom,0,3)
        -> System: Create Sprite on layer "rooms" at nextX+Array(randRoom,loopindex,0),nextY+Array(randRoom,loopindex,1)
        -> Sprite: set animation frame to Array(randRoom,loopindex,2)
      // store the end point of the new room
      -> System: set nextX to Array(randroom,0,0)
      -> System: set nextY to Array(randroom,0,1)

    And that should basically be it

    Now there's still 2 issues :

    • The first one is zIndex dunno how the script will behave... maybe pretty well, else you might have to use layer and also store layer position to create in the right one.
    • The second one is collisions, you might create invisible collisions and store their position to the startDummy the same way... But in another Array.

    Tell me if it worked, I just wrote it as it came :D

  • Thank you very much for your help. I will give this a go when I get off work and post my findings!

    <img src="smileys/smiley1.gif" border="0" align="middle" />

  • Hey SheepGo I am doing a smiler thing to you a constant side scroller with randomly created maps. Did yours end up working?

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