Skip to content

coppolaemilio/npc-talking-tutorial

Repository files navigation

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:

npc-talking-tutorial

Project setup

  1. Install Dialogic 1.1 from the asset lib
  2. Enable Dialogic and restart the project
  3. Create a new timeline called timeline-1

Character movement.

Let's create a character and add basic movement to it:

  1. Make the KinematicBody2D
  2. Rename it to Player
  3. Add a Sprite to it
  4. Add a collision shape
  5. 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)

NPC

  1. Create an Area2D node
  2. Rename it to NPC
  3. Add a collision shape with the range for your dialog to be able to trigger
  4. Add a Sprite
  5. Add an indicator for when the Player can talk with the NPC
  6. 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!

About

No description or website provided.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published