Skip to content

NPC dialogue

czw-qwert edited this page Oct 6, 2026 · 6 revisions

Create DialogueComponent.java to manage which one should be talked and it tell system what will be display in dialogue box. Create DisplayDialogue.java to show which one should be talked and it tell system how to display information in dialogue box. Create DialogueProximityComonent.java to manage when should start or continue talking with player and it tell system when to display information in dialogue box.

  private void nextDialogue() {
    if (dialogue.length == 0) {
      return;
    }
    if (talking == false) {
      talking = true;
      currentLine = 0;
    }
    if (currentLine < dialogue.length) {
      String text = dialogue[currentLine];
      entity.getEvents().trigger("showDialogue", text);
      currentLine++;
    } else {
      endDialogue();
    }
  }

It is used to manage which one should be talked. For example, String[] dialogue = {"hello", "i am npc", "good luck"}; At frist, dialogue is "helllo". Then ,it is "i am npc". Finally ,it is "good luck".

  public void endDialogue() {
    talking = false;
    currentLine = 0;
    entity.getEvents().trigger("hideDialogue");
  }

It is just to finish dialogue.

  private void createDialogueUI() {

    rootTable = new Table();
    rootTable.setFillParent(true);
    rootTable.bottom();

Create a rootTalbe which is filled in screen. The rootTable is transparent. It is used to put some thing in it and they are putted in the bottom part of the rootTable.

    dialogueTable = new Table();

It is real table which is used for dialogue box

    // black background
    Pixmap pixmap = new Pixmap(1, 1, Pixmap.Format.RGBA8888);
    pixmap.setColor(0f, 0f, 0f, 0.80f);
    pixmap.fill();

    backgroundTexture = new Texture(pixmap);
    TextureRegionDrawable background = new TextureRegionDrawable(new TextureRegion(backgroundTexture));
    dialogueTable.setBackground(background);

It creates a black background. It create a 1x1 pixel with RGBA8888. Format.pixmap.setColor(0f, 0f, 0f, 0.80f) means that it is black and the alpha of it is 80%.

    speakerLabel = new Label(speakerName, skin);

    dialogueLabel = new Label("", skin);

    continueLabel = new Label("[E] Next", skin);

    dialogueLabel.setWrap(true);

    speakerLabel.setAlignment(Align.left);
    dialogueLabel.setAlignment(Align.left);
    continueLabel.setAlignment(Align.right);

    dialogueTable.add(speakerLabel).left().expandX().padBottom(10f);
    dialogueTable.row();

    dialogueTable.add(dialogueLabel).width(650f).left().padBottom(15f);
    dialogueTable.row();

    dialogueTable.add(continueLabel).right().expandX();

    dialogueTable.pad(25f, 30f, 20f, 30f);
    rootTable.add(dialogueTable).width(750f).padBottom(40f);
    rootTable.setVisible(false);
    stage.addActor(rootTable);
  }

speakerLabel, dialogueLabel and continueLabel are used for speakerrname, information of dialogue and "[E] Next". They are used to compose dialogue box.

  @Override
  protected void draw(SpriteBatch batch) {}

It is necessary even it will not used in DiaplayDialogue.java because it extends UIComponent. Without it, system will report an error when try to run the game.

    Vector2 npcPosition = entity.getCenterPosition();

    Vector2 playerPosition = player.getCenterPosition();

    float distance = npcPosition.dst(playerPosition);

    if (distance <= dialogueDistance) {
      if (!playerInRange) {
        playerInRange = true;
        nextDialogue();
      }
      if (Gdx.input.isKeyJustPressed(Input.Keys.E)) {
        nextDialogue();
      }
    } else {
      if (playerInRange) {
        playerInRange = false;
        endDialogue();
      }
    }

get the distance between npc and player. If the distance is lower than the number we set, npc will start dialogue with npc. If the distance is bigger that the number, it will end the dialogue. When doing dialogue, press keyE will continue doing dialogue and show next information of dialogue.

Bug fix: Unclear words in dialogue box

Reason:

    speakerLabel = new Label(speakerName, skin);

    dialogueLabel = new Label("", skin);

    continueLabel = new Label("[E] Next", skin);

Reason for bug is the colour of words is not not explicitly defined as white. The colour of words used skin in UIComponent.java and it may cause the colour of words to change to black. The background of dialogue is black, so the words in dialogue box is not clear.

Fix:

    Label.LabelStyle textColor = new Label.LabelStyle(skin.get(Label.LabelStyle.class));

    textColor.fontColor = Color.WHITE;

    speakerLabel = new Label(speakerName, textColor);
    dialogueLabel = new Label("", textColor);
    continueLabel = new Label("[E] Next", textColor);

To fix this bug,we can set the colour of word as white. The colour of words will not be affected by the change of skin in UIComponent.java

Table of Contents

Home

Start here

Enemies and combat

Attacks and effects

Winning

Testing

Game systems

Player
Items, loot and shops
Maps
NPCs and UI

Game engine

Entities and components
Services and tools
Input
Rendering and world

Links

Javadoc

Clone this wiki locally