Small question - picking a random object

This forum is currently in read-only mode.
From the Asset Store
Hand-painted tiles, objects, animated objects, and background to build a colorful Mayan civilization environment.
  • 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

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

    <img src="http://i30.tinypic.com/2v166wy.jpg">

    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:

    + sprite.Value('active') Equal to "yes"
    [ul]
    	[li]do stuff[/li]
    [/ul][/code:222i7zu7]
    
    Hope this helps
  • 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!

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • 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

  • 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

    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:

    + purpleSprite.X Greater than greenSprite.X
    [ul]
    	[li]rotate purple sprite[/li]
    [/ul][/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).
  • 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.

  • BUMP because we posted at exactly the same time and you might not see my post.

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

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

    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.

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

  • I couldn't really figure out containers. I assigned them and got the yellow box and everything, but it doesn't seem to register. I don't think it assigned them correctly, since I didn't really understand what you meant by:

    + objectBody condition
    [ul]
    	[li]objectSprite: Set position to objectBody[/li]
    [/ul][/code:3o0z24j7]
    
    Did you mean "always - set position"? because if I do that the containers seem to rapidly switch between sprites.
    
    As for your question, construct does seem to detect when a platform object is on collosion with the ground, but only when it changes direction.
    
    Edit: I found a way to work around it - when a platform object is "on the ground", it's Y velocity is set at 100. no need for detectors now, I think!
  • I couldn't really figure out containers. I assigned them and got the yellow box and everything, but it doesn't seem to register. I don't think it assigned them correctly, since I didn't really understand what you meant by:

    > + objectBody condition
     - objectSprite: Set position to objectBody
    [/code:38bwfd1i]
    
    Did you mean "always - set position"? because if I do that the containers seem to rapidly switch between sprites.
    

    I meant something like this:

    + objectBody.Value('status') Equal to "alive"
    [ul]
    	[li]objectSprite: Set position to objectBody[/li]
    [/ul][/code:38bwfd1i]
    
    The condition doesn't really matter as long as it's something that references objectBody.  It could be "objectBody.Y Greater than 0" or "objectBody is visible" or something like that.  If you want it to have an Always like effect, use a condition that will be consistent each cycle.
    
    And yeah, you can do simple stuff to detect whether your sprite is on the ground.  I use y position to detect whether my jack-in-the-boxes were jumping or standing still.  I suppose you could do something similar for x position to detect walls, but it might just be easier to use detectors.
  • I'm guessing "body" is the active object and "sprite" is the skin?

    maybe I should take a look at your cap when i'm home. containers look useful.

  • Yeah, I use a separate collision sprite (just a simple rectangle) and position the animated sprite on top of it. The rectangle is the one that gets the platform movement and does all the work. The sprite is just there to look pretty.

  • Awesome, I figured it out. I think it solved my freezing solution, since a solid box collosion is much more stable than an animated sprite with pointy edges

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