Create an instance of an object using variables

0 favourites
  • 5 posts
From the Asset Store
Hand-painted tiles, objects, animated objects, and background to build a colorful Mayan civilization environment.
  • I am creating a simple spaceship combat game where the player creates the spaceship. The spaceship is created in a 'workshop' layout before the player uses their ship to defeat other ships in the level layout. The focus of the game is to experiment with different combinations of blocks to create ships with varying abilities.

    I have so far only added two blocks into the game:

    1. A ship_anchor block (grey), which is the centre of the ship and determines what the ship rotates around. Only one can be placed.

    2. A rifle block (red), which automatically fires bullets at enemies (green) which fly towards the ship.

    There are currently only 2 'spaces' to place these two blocks in the workshop layout, but I plan on having a 6x4 grid in the finished game.

    However, I have encountered a problem which would force me to have create hundreds of events to get around, which I know slows down a game from a previous game I have made.

    I want to be able to use a variable to tell the game what block to create in a space in the level layout of the game. At the moment I can only see one way of choosing which object to be created when a block is placed in a space in the workshop layout. This is to have a variable that stores the value of the block placed in the space, and have that block created in that space when going to the level layout.

    The problem with this is that I have to create an event for every space being occupied by every block. For the amount of blocks I have planned to put in the game, that would be about (6x4)x12, which is equal to 288 events! I have created a game with a similar amount of events and variable before, and frame-rate averaged at about 55 when running. Frame-rate isn't my main concern however. I am more concerned that it will take me many hours to create every scenario for every block being placed in every space.

    Is there any way for me to use a variable to create a specific block in a specific location? For example, if space1 is equal to 3, create block 3 in space 1 in the level layout?

    Any help with this would be very much appreciated!

  • It would be far simpler to use an array instead of a variable for every block.

    In your case, I'd imagine you'd create an array the same size as the workshop grid, with each cell in the array representing an individual block. So if block option 1 is placed at the 3x2 (zero-based) location in the editing grid, then set a value of 1 at the array(3,2) location. Then, when you load the combat stage, just have a loop run through each XY cell in the array, and create the block corresponding to the value in the current cell.

    Also, if possible, try to have a single sprite object for all the different types of blocks, and use animations or frames to set the specific type of block. That way, when you're looping through each XY of the array, you can just create the one sprite object, and automatically assign it's animation/frame based on the value in the loop's current XY location.

  • It would be far simpler to use an array instead of a variable for every block.

    In your case, I'd imagine you'd create an array the same size as the workshop grid, with each cell in the array representing an individual block. So if block option 1 is placed at the 3x2 (zero-based) location in the editing grid, then set a value of 1 at the array(3,2) location. Then, when you load the combat stage, just have a loop run through each XY cell in the array, and create the block corresponding to the value in the current cell.

    I see, that does seem like a much simpler way of doing it. I'll look up a tutorial on how to use loops now.

    Also, if possible, try to have a single sprite object for all the different types of blocks, and use animations or frames to set the specific type of block. That way, when you're looping through each XY of the array, you can just create the one sprite object, and automatically assign it's animation/frame based on the value in the loop's current XY location.

    I never thought about using animations to determine the properties of an instance before. Very clever!

    The only problem I can think of now is that I don't know how to anchor each block to the one next to it. Actually, I've just properly figured out what you mean! If I have the grid of the blocks always created when the combat layout is played, then I can use the visibility and animations of them to determine whether they collide with enemies or fire bullets etc.

    Thank you very much, your answer has been very helpful!

  • I'll look up a tutorial on how to use loops now.

    Just to be clear, the specific loop I referred to in my previous post is a "For Each Element" loop condition within an Array, though knowing how to use all types of loops can be incredibly useful.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Anchor pieces with Pin behavior

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