[Solved]How do I For Each Family Member

Get help using Construct 2

Post » Tue Sep 01, 2015 4:17 am

Hey guys,
First, let me set the scene. I am making a 2D RPG. There is a player and there are monsters. Each Monster is stored in a Family called Monsters.

So my issue is... When a Player is below a Monster and they walk up to said monster, the Player sprite is on top of the Monster (based on Z-order) which is what I want, but, if the player is above and walks down to the monster, the Player sprite once again is on top instead of below the monster sprite (based on Z-order). I created a quick fix by using "Every .5 seconds > Compare Monster.Y to Player.Y and Move them accordingly. Unfortunately this changes every Monsters Z-order. For now this is fine because the player can only be in one spot at one time but, down the line, I know I am going to have issues with this and I would like to fix it now.

I am considering using "For Each" with the Family Monsters Object. Will "For Each" apply to each value inside the Family or will it apply changes to the entire Family at once. Do you have any suggestions for how to handle this?

If this has been answered, I apologize. Searching for "For" and "Each" is considered too common by this website, haha.
Last edited by Tekniko on Tue Sep 01, 2015 5:42 pm, edited 1 time in total.
B
56
S
15
G
13
Posts: 826
Reputation: 17,645

Post » Tue Sep 01, 2015 4:46 am

I think you won't need to use "For" or "Each" for this. You could create a variable to record the monster's UID. I just tested it and it works for families too. So you can apply your events only on that monster by selecting its UID.
B
46
S
23
G
31
Posts: 693
Reputation: 20,637

Post » Tue Sep 01, 2015 5:38 am

@Tekniko

I would check which monsters are overlapping the player, and only move those... or maybe an invisible sprite two or three times bigger than the player called "MosterDetector" and use that to select only the monsters near the player.

Then if the the moster's Y is greater than player's Y then move the monster in front of player.
if the monster's Y is less than player's Y then move the monster behind the player.

that way you don't have to do any looping - just pick monsters overlapping, check the Y coordinates and move the z-order accordingly. Using that method, if your player walked through a whole group of monsters he would pass through them correctly (any higher up the screen would be behind him, and any farther down the screen would be in front).
B
97
S
34
G
20
Posts: 438
Reputation: 17,790

Post » Tue Sep 01, 2015 6:04 am

The problem is, I don't know how to refer to each individual monster. The way I have it set now, they either all are above the Player or all of them are below.
B
56
S
15
G
13
Posts: 826
Reputation: 17,645

Post » Tue Sep 01, 2015 1:24 pm

@Tekniko - what I was saying is use "Is Overlapping Another Object" to select only monsters near your player. The only thing I would keep an eye on is generating too many collision checks. You don't have to refer to each monster individually (you can loop through picked monsters if you want), but you can process them in groups: all monsters near player get z-order updated, all monsters outside the area you are checking get ignored. So, what you do is: pick monsters near player, then in a sub event pick monsters with Y less than play and set their z-order behind player, else set z-order in front of player. (the "else" clause would only select the near by monsters that don't have Y less than player)

Since it sounds like you are talking about a relatively small number of monsters at any one time, you could also use "is on-screen" to select only visible monsters. Or compare X coordinates: pick monsters where abs(player.x - monster.x) < 200 (abs is the absolute value function - that would select monsters within 200 pixels to the left or right of player).

So, that is three ways to only select monsters near the player and omit other monsters on the layout...
B
97
S
34
G
20
Posts: 438
Reputation: 17,790

Post » Tue Sep 01, 2015 4:29 pm

As soon as you said, "Is overlapping" I saw the solution in my head. Sometimes I just draw a blank on how to fix a problem. Thank you!
B
56
S
15
G
13
Posts: 826
Reputation: 17,645


Return to How do I....?

Who is online

Users browsing this forum: Baidu [Spider], Darknessed, FabianB and 42 guests