Targetting specific instances of objects

Get help using Construct 2

Post » Fri Apr 12, 2013 9:55 pm

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
B
49
S
11
G
10
Posts: 1,833
Reputation: 14,428

Post » Fri Apr 12, 2013 10:51 pm

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.
[code]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)[/code]

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.
[code]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[/code]
B
79
S
24
G
54
Posts: 4,746
Reputation: 40,755

Post » Fri Apr 12, 2013 10:53 pm

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 ramones2013-04-12 22:55:57
B
54
S
28
G
18
Posts: 1,520
Reputation: 25,020

Post » Fri Apr 12, 2013 11:03 pm

Nice, thanks! I will test it out and see how it goes! Thanks again for all the input!
B
49
S
11
G
10
Posts: 1,833
Reputation: 14,428

Post » Sat Apr 13, 2013 1:00 am

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...
B
49
S
11
G
10
Posts: 1,833
Reputation: 14,428

Post » Sat Apr 13, 2013 1:11 am

NVM, I think I got it, system pick nth instance... set it to my target variable...
B
49
S
11
G
10
Posts: 1,833
Reputation: 14,428

Post » Sat Apr 13, 2013 2:10 am

@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...

B
49
S
11
G
10
Posts: 1,833
Reputation: 14,428

Post » Sat Apr 13, 2013 11:09 am

You need to use the "pick by uid" condition of "block" instead of the system condition to pick an object instance.
B
79
S
24
G
54
Posts: 4,746
Reputation: 40,755

Post » Mon Apr 15, 2013 8:00 am

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!BluePhaze2013-04-15 08:00:33
B
49
S
11
G
10
Posts: 1,833
Reputation: 14,428

Post » Mon Apr 15, 2013 8:18 am

[QUOTE=BluePhaze] 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![/QUOTE]

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

B
24
S
8
G
7
Posts: 756
Reputation: 7,192

Next

Return to How do I....?

Who is online

Users browsing this forum: DiabloOvermind, emoticon, odiusfly and 16 guests