Small question - picking a random object

For questions about using Classic.

Post » Wed Apr 02, 2008 3:35 am

Hey again,
Let's say I have a group of cloned enemies, and I want to perform an action on a random one every X seconds. how would I go about that?

Also, a question about "swarm" behaviour - if I have a basic AI routine for an enemy, and I place a bunch of them around the player (in a platform game), they all act at the same time instead of individually. I thought placing "for each object" in the events would the trick, but it didn't seem to help. any ideas?

Thanks :)
B
2
G
4
Posts: 44
Reputation: 1,138

Post » Wed Apr 02, 2008 4:17 am

To pick a random instance, use the "Pick a random object" condition under that object's conditions. Construct will select only one. For instance:



If I have a few of those sprites scattered around the layout then every 1000 MS one of them will make an eighth turn clockwise.

As far as distinguishing individual instances, you need to specify them somehow. The best way is with private variables. Give your object some kind of variable that toggles whether or not they're "active." In your condition, pick the active ones by comparing the variable:

[code:222i7zu7]
+ sprite.Value('active') Equal to "yes"
- do stuff
[/code:222i7zu7]

Hope this helps :)
Moderator
B
5
S
2
G
6
Posts: 4,348
Reputation: 10,971

Post » Wed Apr 02, 2008 5:49 am

deadeye to the rescue!
the first tip was a success, but the second one wasn't quite what I was asking.

Let's say I set the enemy to walk left when the player is to the left of him (detected by comparing X positions) and vice versa.
If I set one enemy on each side of the player, both of them will walk in one direction, instead of each one heading towards the player.
Maybe I overcomplicated things, since the game seems to hang every once in a while. maybe I'll restart the AI again.. platform AI is a pain in the ass!
:evil:
B
2
G
4
Posts: 44
Reputation: 1,138

Post » Wed Apr 02, 2008 5:53 am

You could set them to rotate towards the player, and if they have bullet movement they'll just go towards him. But that's probably not what you were looking for, and you probably knew it already :P
B
2
S
2
G
4
Posts: 110
Reputation: 1,670

Post » Wed Apr 02, 2008 6:11 am

[quote="electrii":bgk40431]the second one wasn't quite what I was asking.

Let's say I set the enemy to walk left when the player is to the left of him (detected by comparing X positions) and vice versa blah blah blah[/quote:bgk40431]

I don't know how you have your conditions set up but here's a quick 5-second cap that shows instance picking based on comparing x position:

http://www.mediafire.com/?1dnymyyt92t

There are two purple sprites to either side of one green sprite.

The condition:

[code:bgk40431]
+ purpleSprite.X Greater than greenSprite.X
- rotate purple sprite
[/code:bgk40431]

Only causes the right-hand purple to rotate. So it is possible to pick instances based on all sorts of specific conditions. If you have more than one condition for picking enemies to move, I'd check the heirarchy.

And you can always pm the .cap and I'll take a look (I know you don't like to make your stuff public ahead of time).
Moderator
B
5
S
2
G
6
Posts: 4,348
Reputation: 10,971

Post » Wed Apr 02, 2008 6:11 am

Thanks, but that wouldn't work out since the enemies use platform movement as well. ;)

I'm also having weird problems with collosions now. I want to change an animation when the enemy hits a solid, and it doesn't detect it. :cry:
B
2
G
4
Posts: 44
Reputation: 1,138

Post » Wed Apr 02, 2008 6:12 am

BUMP because we posted at exactly the same time and you might not see my post.
Moderator
B
5
S
2
G
6
Posts: 4,348
Reputation: 10,971

Post » Wed Apr 02, 2008 6:51 am

Thanks, it seems like playing with the heirarchy did the trick. :)

Can you think of any alternative way to detect when a platform object hits a solid? putting a detector on the bottom for each enemy seems redundant, and making an event that says "when <enemey> hits solid - set animation" doesn't work for some reason, until the sprite changes angle, then it's triggered. it's probably a bug.
B
2
G
4
Posts: 44
Reputation: 1,138

Post » Wed Apr 02, 2008 7:04 am

[quote="electrii":sezg2fh8]Thanks, it seems like playing with the heirarchy did the trick. :)

Can you think of any alternative way to detect when a platform object hits a solid? putting a detector on the bottom for each enemy seems redundant, and making an event that says "when <enemey> hits solid - set animation" doesn't work for some reason, until the sprite changes angle, then it's triggered. it's probably a bug.[/quote:sezg2fh8]

Nope, not a bug. It's an "undocumented feature."

The reason plaform movement doesn't trigger "On collision" events is because they never actually collide as far as your event sheet is concerned. All the platform calculations take place in between cycles (after the end of your event sheet, and before the beginning). So all the pushing out of solids and such has already happened whenever your "On collision" event rolls around. Sucks, I know. As a general rule I always use "Is overlapping" rather than "On collision."

So yes, even though it's redundant, you should make detectors for your enemies if you want them to have detailed interaction with the environment. A foot detector for contacting the floor, side detectors for walls, etc. You can make it easier on yourself by setting them up in Containers. Click here to read my description of Containers (and be sure to read Ashley's response too, it's important).

Edit:
Wait... what? It only happens after your sprite changes angles? I'm going to need more details on that, you've confused me now.
Moderator
B
5
S
2
G
6
Posts: 4,348
Reputation: 10,971

Post » Wed Apr 02, 2008 5:00 pm

Deadeye's solutions are good so far, except that you can do it without subevents, heh - 'pick random' could simply be a condition after 'every'.

As for object picking via conditions, remember two things:
- For Each works best as the first condition
- Use built-in conditions wherever possible instead of System's Compare Values (eg. Sprite's Compare X rather than comparing Sprite.X to a value - works instance by instance)

Also might help to post your .cap file, so I can tell you exactly what's going on.
Scirra Founder
B
359
S
214
G
72
Posts: 22,952
Reputation: 178,600

Next

Return to Help & Support using Construct Classic

Who is online

Users browsing this forum: No registered users and 7 guests