[Solved] How do I avoid repetition?

Hello. I have a simple event to make the player face who he is talking to, but I am sure there must be a cleaner way of doing this than pasting the same events for each NPC. The only difference between each block is a variable check. Each NPC is a separate object - not separate animations within one object. This is unavoidable because NPCs may vary in size and have various animations each.

My question: how do I make a single event that will flip my character for any NPC he talks to?

I tried grouping them to see how that would behave. The player would flip when in collision with either NPC in the group, but he was only using the X position of the first NPC.

I think the answer will help me way beyond this small problem. Iā€™m getting on well with GDevelop, but more dynamic stuff is a little out of reach and that means my events are repetitive and verbose. They work, but I know they could be better. Thank you.

Have you tried putting all the NPCs into an object group?

2 Likes

Hi!
According to the limited knowledge I have about that, I would at the beginning of the game, in the scene editor, give each NPC an object variable ā€œnameā€ that record their own name.

In scene editor I would also group all NPCs in a group called NPC.

Then for checking if frodo is on right or left that the NPC heā€™s talking with, Iā€™d do like that :

That means the event will try each instance of objects in the group NPC. For each of them it will check if thatā€™s the one frodo is talking to.

(if you have several instances of a same NPC object, maybe youā€™ll need to add a condition like ā€œdistance between frodo and NPC< somethingā€ , so only the instance heā€™s talking to would work)

Please someone who know better confirm itā€™s the best way to do itā€¦!?

I think thereā€™s no other way to dynamically access an object (I donā€™t really know why).

2 Likes

Thanks Bluzima for your time, but that doesnā€™t seem to work for me. The player faces the correct way for the first npc (trevor), but it breaks for brenda: none of the actions for that event work when I talk to her.

Did you added the brenda object to the NPC object list, give her the instance variable name and make sure the case is correct?

2 Likes

I havenā€™t used ā€˜repeat for each instance beforeā€™. That did it!

Thank you @Bluzima for going to the trouble of creating the events and solving my problem - Iā€™m sorry I didnā€™t realise it right away! Thanks also @MrMen and @beanmatt for nudging me along.

1 Like

Good to know it worked!

1 Like