SIM : Get a single random NPC to go to one of several "destination" objects

Hello, I’m new to Gdev AND I realise that even without needing code, it’s far from easy to understand how to do what I would consider simple things.

Big fan of management sims, I’m trying to give commands to npcs. There are a lot of tutorials around a main character you move around, that interact with enemies, objects, etc, but I haven’t found much in terms of giving behaviour to NPCs.

Let me show you the absolutely ugly sketch I drew to try things:

I have here three characters. Two destination spots on the right.

What am I looking to do:

  • Select one random character IF a destination is unoccupied
  • Get that random character (and only that character) to move its angular bottom to one of the two destination points.
  • Meanwhile, since the other spot is free, another character goes to that one.
  • After a few seconds, the characters leave their new spot (where doesn’t really matter at this point), leaving the destination points free again for new ‘customers’.

It’s pretty basic, imagine a tavern, free seats, unique characters come at random, sit, order, leave, etc.

My journey on GD:

  • First of all, I SUCK at it.
  • One major problem I found, amongst others, is that as the event sheet repeats its commands every frame (as I understand it…?), then even If I managed to select a random chara to go to the destination with a variable I called ‘Empty: true’, the event will run again, finding another random, resulting in what is actually a funny run towards the destination point.
  • I don’t know how to do : Take a random guy on the left, in that starting box and only this guy you picked will go towards an empty destination point and even as he’s on its way, so hasn’t collided yet, nobody else will attempt to steal its spot.

I almost gave up entirely several times in the last few… days now. But as I was trying to find solutions on forums and tutorials, I often saw how supportive this community is so… here I am.

Many thanks in advance for any and all help.

Happy new year everyone, by the way!

Maxime

1 Like

Do you know about trigger once condition? Sounds like it would be useful for you

Hello Bernie and thank you for replying. I know of Trigger Once (while true) but when I use it above my event, one character will move one frame toward the target and then stop. I probably put the command at the wrong spot and or link to the wrong command.

Other than the trigger once, I feel that what’s wrong is that even if I pick a single chara, using random pick, in the action ‘all charas’ move.

Is there a way to put in the action : ‘move that randomly chosen chara’ ? And not all of them?

Thank you very much for your help.
Maxime

Hi Maxime, I haven’t tried “Pick a random object” events but you may want to give one of the Charas a variable instead, to hold its destination. (With Trigger once, as that seems to select only one character)
Then create another event where if a Charas’ variable is not “” (not empty), make it move toward the destination object.

1 Like

As reina said above, try giving char a variable then only char with variable will move and not all of them.

@reina, @BernieStone

Thank you so much for your help.

I did use Trigger Once, but as a subevent of checking if Destination1 was empty.
after spending the better part of the day on this, I finally managed to :

  • Move one random chara to destination1
  • Not move any others while destination1 is NotEmpty
  • Making the occupying chara leave (who knows where, probably a better place)
  • Make another random chara take his spot.

Realised how important sub events are compared to ‘just’ events. The trigger once would not allow other chara to move after the first one left Destination1 with a trigger one at the top. The most important root information is if Destination1 is Empty or not.


Wish I could share the petty animated result! lol

I don’t know if I have it in me to struggle this much every time I want to do what should be elementary.

In any case, I really appreciate the support, thank you both very much.

Maxime

I love GD. Give it a chance. There are extentions and object behaviors that help simplify things. It also helps to learn the basics through tutorials, videos and studying the examples or templates.

I personally like to break events into groups. I would have a section to choose a destination, a 2nd group do the moving and then maybe a 3rd to move away.

As you’ve learned variables are key to track the states.

Conditions work like filters. They pick the matching objects and also trigger any actions. Without any conditions, an action like delete would delete all of the instances or objects with the object or group name. With a condition, it would only delete the ones that match the condition.

There are a lot of different ways to move objects. You can assign force immediately (like in your events) or permently. You can use extensions or object behaviors.

Object behaviors like tween and path finding are very helpful. Tweens will move an object or other settings and let you know when it’s done.

The path finding behavior will calculate a path and move the object to it. Again, telling you when it’s done through a condition. It can be paired with the pathfinder obstacle behavior that gets assigned to obstacle objects. It would then choose the best path around the obstacles .

There are many other ways to move objects.

https://wiki.gdevelop.io/gdevelop5/tutorials/how-to-move-objects/

2 Likes

Hello Keith and thank you for your welcoming message.

I’m happy to at least be able to understand most of what you talked about, I’ve been through the basic and intermediate tutorials but on the first pass, it’s quite hard to register everything and understand how to use it once you’re in front on your blank page.

I’m a graphic designer and illustrator by trade but as a fervent lego lover, I’ve always been eager to build my own ‘structures’ for others to interact with. Never considered it before finding GD, as I don’t think I’d have the time, patience or mind to learn an actual coding language.

I’ve been very impressed with the help given in almost no time to wait, on top of it.

I haven’t looked into extensions, much, saw the youtube selection of Helper but like the tutorials, I found that most information taught regard a main character with or without physics attached to its object. Not much (nothing I found) on management sims, except perhaps the city builder basic mechanics with the tiles example.

One great tutorial could be on npc needs increasing over time (Say hunger, fun, work (I know, crazy variable)) with bars that fill up and when it’s full, you get an angry message. If there is a place where the npc can fulfil its need, then the bar reset. Probably not incredibly hard to do when you know what you’re doing, invaluable for someone like me!

In any case, thank you again Keith for your message and the different informations you gave me. I’ll definitely try to organise my event list better to have a better understanding of what i’m doing (and where i’m failing, ARGH).

Have a great day.
Maxime

3 Likes

You’re welcome. It’s a smaller community but there are good people here. I love GD for the simplicity. You can do a lot with relativly few lines. I also love how it’s not broken up into separate files. I never liked the apps that tell you that you need a bunch of files with different suffixes.

You can definitely add more boolean variables to create more states as well as number variables to track energy or whatever. It would then choose a destination based on the variable or the states.

2 Likes

Sounds like a great way to approach this indeed! Thanks for this great tip once more.

1 Like