Targetting specific instances of objects

0 favourites
From the Asset Store
Welcome! I-Spy (Hidden objects) is an educational puzzle that is more than just seek-and-find activities.
  • Hello All,

    I am working on an idea and wanted to get some input, I want to have a grid of objects (Think blocks or bricks) and I want to have AI controlled sprites automatically attack certain blocks. Now The twist here is that when they are attacking one block they should not hit the others, the shots should pass right through. So here is my rough idea, and I am open to anyone else's thoughts on the best way to approach it:

    1. Create a grid of blocks made of instances of a sprite.

    2. Use an array to track each blocks location and current state.

    3. Have enemy sprites that randomly come and shoot/attack a particular block.

    4. While that block is being attacked, the surrounding blocks should not block the shots targeting the target block.

    5. After 4-5 hits, the block being hit is destroyed

    6. At this point a new block is picked and the enemies start attacking it instead.

    7. Once a block is destroyed its destroyed state is stored in the array so that it does not get picked again to be attacked.

    8. Each block will have multiple states (Whole, multiple levels of damaged, and destroyed). These will be tracked in the Array by assigning a number to each state. For example 6 = whole, 5 = slightly damaged, 1 = destroyed.

    9. I may also track how many blocks are left so that I can have an event trigger when all blocks are destroyed.

    Something like a block breaker/breakout game except that only a specific brick can be hit at any one time and all other bricks are basically visible but not solid until they are picked as the target...

    Anybody have any thoughts on a good way of implementing the logic for this scenario? I haven't had to use arrays much in construct yet, but am familiar with them outside of construct from my developer days. Any thoughts on the best way to do this would be great.

    Thanks,

    James

  • You don't need an array for this. Use a Sprite for the blocks as you said and give them an instance variable for the state. You can then make the grid of blocks by placing them in the editor or with events.

    global number grid_top=10
    global number grid_left=100
    
    start of layout
    for "row" from 0 to 9:
    for "col" from 0 to 9:
    --- create block at (loopindex("col")*block.width+ grid_left, loopindex("row")*block.height+ grid_top)

    Then you can do the targeting by saving the uid of a random block to a global variable. Then with events only check for bullets hitting that instance.

    global number target=-1
    
    target = -1
    pick a random block instance
    --- set target to block.uid
    
    every 1.0 seconds
    pick block with uid target
    --- "have the enemy shoot at block"
    
    pick block with uid target
    block is overlapping bullet
    --- destroy bullet
    --- subtract 1 from block state
    
    block state<=0
    --- destroy block
    --- set target to -1
  • Well without using an array you could:

    Pick a random block IID for the target - floor(random(block.count)).

    Shoot at it.

    On collision with block, check that the block IID = target.

    Subtract health.

    Health variable = state = frame/animation.

    On destroy - pick new target.

    *edit* ^^ what he said

  • Nice, thanks! I will test it out and see how it goes! Thanks again for all the input!

  • Hello R0J0hound I am struggling a bit picking the block with the UID assigned to the target variable... any clarification on this... in code I would do something like shoot at block where block.UID == target

    I am trying to use the picked block in multiple events, but straining to see how to assign current target as the value assigned to my target value. For shooting at it I need to set the angle of the bullet toward the X, Y of the target, but I first need to identify it via the UID stored in the target variable... I am sure there is just something I am not seeing in my events list that resolves this...

  • NVM, I think I got it, system pick nth instance... set it to my target variable...

  • R0J0hound Using the method you described, it goes block by block in order when picking the next block to destroy, so I altered it a bit and instead of pick block.UID, it did a pick random instance of the block then assign block.UID to the target variable. This gives the effect of picking a random block each time as the target, but for some reason, whenever they are fired at, it stops after destroying 9 blocks. My assumption is that this is tied somehow to the initial logic creating the blocks as it is the only place I can find the number 9... could be pure coincidence, but after destroying 9 blocks, it stops spawning more enemies to shoot at the blocks... Here is a image of my events.

    I can provide a capx, but everything is here in the sheet since it is all event driven...

    <img src="https://dl.dropboxusercontent.com/u/10285716/BlockKillerEvents.png" border="0" />

  • You need to use the "pick by uid" condition of "block" instead of the system condition to pick an object instance.

  • Thanks R0J0hound that makes sense. How would you assign the random to it? What I have currently now is:

    Block > Pick Instance with UID Random(Block.UID)

    ---- System > Set Target to block.UID

    For some reason though, this still does them all in order. I have tried other parameters to the Pick Instance with UID like Random(0,122), etc... but it does not work. It just sits there and nothing happens. Any better expression to plug into when grabbing the UID to really get a random (but existing) UID from the 100 blocks I Have on the screen?

    Thanks!

  • Thanks R0J0hound that makes sense. How would you assign the random to it? What I have currently now is:

    Block > Pick Instance with UID Random(Block.UID)

    ---- System > Set Target to block.UID

    For some reason though, this still does them all in order. I have tried other parameters to the Pick Instance with UID like Random(0,122), etc... but it does not work. It just sits there and nothing happens. Any better expression to plug into when grabbing the UID to really get a random (but existing) UID from the 100 blocks I Have on the screen?

    Thanks!

    The idea would be to pick a random instance of block, assign it's UID to target variable, and use that to refer to the target block in the future.

    So do something along these lines:

    pick random block

    .. target = block.uid

    pick block by uid target

    .. shoot

  • vee41

    The pick random block piece is the one causing the issues. Using "System > Pick Random instance" works, but for some reason it ignores some of the blocks. Using System > Pick nth instance for some reason picks them in order and still skips blocks. Using "Block > Pick Instance with UID Random(Block.UID)" also works but again does them all in order even though I am specifying random... But at least it does get all the blocks now and I am not left with 4 or 5 that it just ignores all together.

    The goal is to clear them all, and do them in a random order. So random block is picked, all enemies target it, once it is destroyed a new random block is picked. Here is the current and 14th or so iteration of the logic, I have scrapped generating the grid of block on the fly because it caused random oddness here and there. Currently I Have the state and the animation frame tied together so that as the block is damaged the state changes which updates the animation frame. Note that I have also added an event that sets the state and animation frame of the block as soon as it is picked as the target so that I can tell which one is being targeted before it gets hit. This is just to allow me to make sure that the block being targeted is the same for all enemies at any particular time. I also have a shot variable to control the number of shots an enemy can take so that the blocks do not get cleared out to quickly.

    <img src="http://dl.dropboxusercontent.com/u/10285716/BlockKillerEvents2.png" border="0" />

  • The randomization event "Pick instance with UID Random(Block.UID)" does not really make any sense. You should use 'pick random instance' event like you did before, and everything else should work.

    If you use "Pick instance with UID" event, you'll need to enter an UID of exiting block to make it work. Right now, you are randoming a number from 0 to whatever UID first picked block has.

  • Doesn't make sense to me currently either. I got it working with "Pick Random Instance" but it skipped blocks for some reason, at the end there would be 4-6 blocks that it just totally ignores. With "Pick Instance with UID" it works, but it does them all in order by UID.

    Using R0J0hound advice I am trying to make it work with "Pick Instance with UID" on the block object. The Block object doesn't have a pick Random instance like the system object does...

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Here's a capx since I admit there is some ambiguity with what I typed.

    https://dl.dropboxusercontent.com/u/5426011/examples17/target_specific_instance.capxr124

  • Thanks R0J0hound that cleared it up. Oddly I had that setup but it wasn't working. I have decided there must be some oddity with my events or with my UID's... But I scrapped it and started from scratch and it works fine just like your sample. Odd. Thanks again for all the assistance on this one!

    I am using this logic to create an interactive Under Construction animation for my site. At the load it will do a couple random number generations which will be responsible for picking a set of blocks, a set of enemy types. It will then create the screen and the enemies chosen will destroy the blocks to reveal the company logo and under construction message.

    I figured this would be a nice entertaining method that will also allow me to use some of my game characters/assets to be used outside of the game to make the site more personal for those who end up playing my games.

    Once the site is finished I will find another way to incorporate it just to keep the entertainment value.

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