How do I make sure the enemy sprite X value is comparing the relevant instance, using "For Each"?

  • I am building a state machine for an enemy sprite with multiple instances.

    One of these states has the enemy facing the player when they are alert to them, so I have to compare the player (Main_Box.x) with the X value of the enemy instance to see if I need to mirror the enemy or not.

    Will the compare value subevent take into account which enemy I am dealing with automatically, or do I have to specify it.

    I have attached a screenshot of the event sheet I am building.

    Thanks!

    P.S. does anyone know if the line of sight behavior flips when you mirror the sprite, I read somewhere that it was added, but I can't be sure.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • You could find it out yourself by testing and playing around? That's what I would do if the logic was already created. Because you are doing a for each goblin every tick then how it will probably appear in game is that it is applying to all goblins that have an aware state. It'll be so fast that it will seem to pick all of the aware goblins.

  • Thanks, yes playing around will help :) - I've already tested multiple enemies but had to completely start again due to errors, I wondered if maybe that was the cause and I thought I would ask.

    I'm more curious about the underlying mechanics of the event sheet, does referencing an instance.x value only use the x value for the instance involved in that chain of events, or is it any old instance.x value from any of the other copies?

  • It's to do with picking, if you haven't picked anything then it will apply it to the instance with the lowest UID. In this example you pick twice, for each goblin so all of them, then goblin state = aware, so any goblin that is aware, then the condition is applied only to those picked. It narrows it down as you are imagining, when you run that conditions of goblin.x > player.x it is choosing and applying it to the goblins that were picked i.e. all goblins that are 'aware'. Also if you were to run the action on those meeting the condition it will again pick the same goblin instances that were picked, those that are in 'aware' state.

  • Oh and the other question, yes mirroring the object will mirror the LOS so it is facing the other way for detection.

  • It's to do with picking, if you haven't picked anything then it will apply it to the instance with the lowest UID. In this example you pick twice, for each goblin so all of them, then goblin state = aware, so any goblin that is aware, then the condition is applied only to those picked. It narrows it down as you are imagining, when you run that conditions of goblin.x > player.x it is choosing and applying it to the goblins that were picked i.e. all goblins that are 'aware'. Also if you were to run the action on those meeting the condition it will again pick the same goblin instances that were picked, those that are in 'aware' state.

    So I just tested my new code, and only one goblin is turning, despite both being "alert" in the instance variables. Does this mean it's only picking one instance, shouldn't the "For Each" at the top of this event flow be covering this?

    Thanks :)

    p.s. > Oh and the other question, yes mirroring the object will mirror the LOS so it is facing the other way for detection.

    Thank you so much, some old forum posts confused me on this when it was not a feature at the time.

  • So I disabled all the for each loops and now the enemies are mirroring independently, how is this the case when it should be that the for each loop means '"for each" (enemy) do this action'

    When I do use "for each" loops, the enemies do not flip independently, only one flips, the others are ignored.

    This is the opposite of what I expected, what am I missing here in terms of principles?

  • I should've added more info to this really as I have some experience with this kind of logic. I made a theme park style game where the guests would constantly be changing state and the things they were doing in game. A for each loop isnt the best thing to use when instances are dynamically changing all the time, a for each loop is better used to run through all instances once. To pick an instance that's in a state just simply say when goblin state = x do a thing, i.e. with the for each disabled. This way it just picks the instance when it changes to that particular state which is probably what you want for gameplay such as this. When you say goblin state = x, in a sense you are still picking all goblins that meet the criteria.

  • I should've added more info to this really as I have some experience with this kind of logic. I made a theme park style game where the guests would constantly be changing state and the things they were doing in game. A for each loop isnt the best thing to use when instances are dynamically changing all the time, a for each loop is better used to run through all instances once. To pick an instance that's in a state just simply say when goblin state = x do a thing, i.e. with the for each disabled. This way it just picks the instance when it changes to that particular state which is probably what you want for gameplay such as this. When you say goblin state = x, in a sense you are still picking all goblins that meet the criteria.

    Oh wow, thank you, that makes sense. I really appreciate the explanation :)

    So when should one use 'for each', when using multiple instances of the same object?

  • Most 'for each' style logic will work with simple conditions, you rarely need to use this loop. A good use of it is for each (ordered) where you can run through instances of an object in an order of something such as top to bottom on screen, so that can be useful if you need to go through every instance 'in a specific order'.

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