How to create NPCs in BGE

I’m beginning to create an RPG in BGE (specifically UPBGE) and I want to have NPCs scattered around. I have no idea where to start in creating them, other than creating the animations. Here are some of the things I’d like to do:

  • Program a system in which you can click on an npc to talk to it (I’m sure you could do this with a Mouse sensor, a message actuator, and a Text object, but I’m not sure how to make a Text object show the message I want it to (obviously, it would be very inefficient to make text objects for every single message)
  • Program a system in which if you click an npc when you’re a certain distance away, you will run after it to be able to talk to them (I’d imagine you could do this with a Near sensor, Mouse sensor, and Steering actuator)
  • Program a system in which an npc will walk around randomly when not being interacted (I don’t have any idea how to implement this, other than guessing that states would be involved)

I’ve just started learning Python, so I hardly have any knowledge on how to use it. I would be able to implement these features using logic bricks (preferably). Any tips on implementing these would be greatly appreciated.

1 Like

yeah all of that sounds pretty easily doable

I would have a few states, and use the steering actuator and a near sensor
when you get near to a waypoint -> move the waypoint to the next point in a loop.


import bge
cont = bge.logic.getCurrentController()
own = cont.owner

collision = cont.sensors['Collision']
if collision.positive:
    if 'orig' not in collision.hitObject:
       orig = collision.hitObject.children[0]

    if collision.hitObject['Waypoint'].children:
        collision.hitObject.worldPosition = Collision.hitObject['Waypoint'].children[0]
        collision.hitObject['WayPoint'] = Collision.hitObject['Waypoint'].children[0] 
        collision.hitObject.worldPosition = collision.hitObject['orig'].worldPosition
        collision.hitObject['Waypoint'] = collision.hitObject['orig']

so the npc bumps into a waypoint -> this moves the waypoint to the next ‘marker’

on the first waypoint we parent the ‘Marker’ to the waypoint collider

next we parent the next ‘Marker’ to the first marker

and so on, until the last marker has no children -> swap back to first marker

this way

Waypoint child is -> marker 1
marker 1’s child is marker 2
marker 2’s child is marker 3 and so on.

edit: “wayPoint” object is the steering actuator target for the NPC

1 Like

Would he need NavMesh too or no?

also for talking NPC’s make a dialogue scene, add a camera, a plane facing the camera and the text doing the same. Go to your main scene, select your camera, add a always sensor and DO NOT click anything. Add a scene actuator with Overlay Scene and DO NOT type anything in it.

put the text in the far of your left in the corner of plane, make plane black, text white, rename text “dialogue” and give it a interger property called “sentence”. no quotations.

Back at Dialogue scene add Always sensor and activate the 3 dots to text. Add a Keyboard sensor with whatever button or Spacebar and name it “space” no quotations. Connect both sensors to a python controller with game logic simple script.

here’s script

``import bge

def main()

cont = bge.logic.getCurrentController
own = cont.owner

sentenceDialogue = "Did you get it?"

space = cont.sensors ["space"]
remove = cont.actuators ["remove"] 

# this will loop the conversation. The 5 is the number of sentences you have.
if space.positive and own ["sentence"] < 5:
	own ["sentence"] += 1
number = own ["sentence"]

if number == 2:
	sentenceDialogue = "Good, where is it?"
if number == 3:
	sentenceDialogue = "How did you get it?"
if number == 4:
	sentenceDialogue = "yada yada"
if number == 5:
	sentenceDialogue = "Blah Blah"
if number == 6:
# remove scene
own.text = sentenceDialogue


Your right about the near sensors it’s more if you want the entire world to stop or not.
So you can have the playable character frozen or just put the eyes back in first person.

if the waypoints are close enough together you can get away with ‘seek’ however you can also use a navmesh and have waypoints a bit further apart.

there are also lots of python based pathfinding solutions

1 Like

Your plan looks pretty good. Now you can focus on the single points. Give the point more details (split them into smaller requirements). Continue until you can manage them.

I suggest you start very very simple. For example, to implement your first point you do not need a full fledged character. You just need

  • something you can click on
  • something that shows text

Ensure it meets your requirements.

Then you create two of them (to see if you can mange more than one NPC). Try if your implementation still works. This step can be difficult as it introduce more complexity. It shows you the user experience, which enables you to verify your requirements (e.g. is the text where you expect it to be, how is the timing, how ist the general feeling).
You get an idea how much you need to do to add more NPCs.

When this works with two it usually works with three too.

Hi, maybe You want to have a Look on this: :grinning:

Thanks a lot, I’ll be sure to check it out!

Quick question though; Is the first part (where you set up the character) necessary to be able to add the dialogue? I. E. any logic bricks that I need to implement, as I want to add my own character model.

You need to append these booth Objects from the Demo. It will automaticely import some aditional Stuff, like Scripts, GUI Scene etc.
My Char has two Objects. One for Physics what contains all the movements, and one to display, what carries the QnD Stuff. For QnD, You only need this, as i’ve shown in the Video. It should be easy to keep them apart. You can try to copy this to Your Char, oder maybe to an Empty and Parent it, or make it by Hand.

The Cam Tracker is only necessarry for my 3rd Person Setup. See that the Ray Sensor is in the right direction. You also don’t need the Inverntory Script from the Demo if You have your own. You need to understand a little how it works to use it efficiently.