A very common use of dialogues is when speaking to an NPC. There are many ways of achieving this, but here you have a simple one to follow step by step:
- Install Dialogic 1.1 from the asset lib
- Enable Dialogic and restart the project
- Create a new timeline called
timeline-1
Let's create a character and add basic movement to it:
- Make the KinematicBody2D
- Rename it to
Player
- Add a Sprite to it
- Add a collision shape
- Add a script:
extends KinematicBody2D
export (int) var speed = 200
var velocity = Vector2()
func get_input():
velocity = Vector2()
if Input.is_action_pressed("ui_right"):
velocity.x += 1
if Input.is_action_pressed("ui_left"):
velocity.x -= 1
if Input.is_action_pressed("ui_down"):
velocity.y += 1
if Input.is_action_pressed("ui_up"):
velocity.y -= 1
velocity = velocity.normalized() * speed
func _physics_process(delta):
get_input()
velocity = move_and_slide(velocity)
- Create an Area2D node
- Rename it to
NPC
- Add a collision shape with the range for your dialog to be able to trigger
- Add a Sprite
- Add an indicator for when the Player can talk with the NPC
- Add a script to the Area2D node
extends Area2D
var active = false
func _ready():
connect("body_entered", self, '_on_NPC_body_entered')
connect("body_exited", self, '_on_NPC_body_exited')
func _process(delta):
$QuestionMark.visible = active
func _input(event):
if get_node_or_null('DialogNode') == null:
if event.is_action_pressed("ui_accept") and active:
get_tree().paused = true
var dialog = Dialogic.start('timeline-1')
dialog.pause_mode = Node.PAUSE_MODE_PROCESS
dialog.connect('timeline_end', self, 'unpause')
add_child(dialog)
func unpause(timeline_name):
get_tree().paused = false
func _on_NPC_body_entered(body):
if body.name == 'Player':
active = true
func _on_NPC_body_exited(body):
if body.name == 'Player':
active = false
And that's it!