Skip to content

Repository files navigation

DialogSystem for Noita

An example mod that uses this library can be downloaded here: https://github.com/TheHorscht/DialogSystem_example/releases

example.mp4

Installation

Copy the files into your mod, so the structure looks like this: mods/yourmod/lib/DialogSystem/dialog_system.lua

Then in your mod's init.lua, initialize the library like this:

dofile_once("mods/yourmod/lib/DialogSystem/init.lua")("mods/yourmod/lib/DialogSystem")

Passing in the path to to the folder containing dialog_system.lua.

Global config

You can also pass in a table with configuration options as the second parameter to the init function:

dofile_once("mods/yourmod/lib/DialogSystem/init.lua")("mods/yourmod/lib/DialogSystem", {
disable_controls=true,
images= {
-- To be used with {@img something} in dialog textsomething="mods/yourmod/files/dialog_images/something.png",
something_else="mods/yourmod/files/dialog_images/something_else.png",
},
sounds= {
-- Can then be specified as typing_sound = "new_sound"new_sound= { bank="mods/yourmod/files/dialog_sounds.bank", event="cool_new_sound" },
new_sound_two= { bank="mods/yourmod/files/dialog_sounds.bank", event="cool_new_sound2" },
},
})

Quick start

Create a new entity with a LuaComponent as follows:

<LuaComponentscript_interacting="mods/yourmod/files/npc/interact.lua"script_source_file="mods/yourmod/files/npc/interact.lua"execute_every_n_frame="1"enable_coroutines="1"
>
</LuaComponent>
-- mods/yourmod/files/npc/interact.lualocaldialog_system=dofile_once("mods/yourmod/lib/DialogSystem/dialog_system.lua")
-- Make NPC stop walking while player is closelocalentity_id=GetUpdatedEntityID()
localx, y=EntityGetTransform(entity_id)
localplayer=EntityGetInRadiusWithTag(x, y, 15, "player_unit")[1]
localcharacter_platforming_component=EntityGetFirstComponentIncludingDisabled(entity_id, "CharacterPlatformingComponent")
ifplayerthenComponentSetValue2(character_platforming_component, "run_velocity", 0)
elseComponentSetValue2(character_platforming_component, "run_velocity", 30)
endfunctioninteracting(entity_who_interacted, entity_interacted, interactable_name)
dialog_system.open_dialog({
name="Monkey",
portrait="mods/yourmod/files/npc/portraits/monkey.png",
typing_sound="one",
text="Hello, I am monkey! Would you like to buy a banana?",
options= {
{
text="Oh yes please! (500 gold)",
func=function(dialog)
spawn_banana_at_player_position() -- You implement this yourself :)reduce_player_gold(500) -- You implement this yourself :)dialog.show({
-- Options that are not specified will use the previous messages optionstext="Thanks for the purchase buddy.",
})
end
},
{
text="Nah I'm not hungry.",
-- If no func is specified, the option will close the dialog
},
}
})
end

Documentation

-- Dofile-ing dialog_system.lua returns the dialog systemlocaldialog_system=dofile_once("mods/yourmod/lib/DialogSystem/dialog_system.lua")

It can either be configured locally by setting the following properties on it, or globally when initializing the library (see here). If nothing is configures, it will fall back to use the defaults.

-- To add new images/icons to be used in text, this one will be usable as {@img fish}dialog_system.images.fish="mods/yourmod/files/dialog_images/fish.png"-- To add new typing sounds, to use it specify typing_sound = "tick" after registering it like thisdialog_system.sounds.tick= { bank="mods/yourmod/files/audio/your_bank.bank", event="sounds/my_cool_sound" }
-- Distance from bottom of screendialog_system.dialog_box_y=50-- How wide the dialog box isdialog_system.dialog_box_width=300-- How tall the dialog box isdialog_system.dialog_box_height=70-- How far the player has to move away from the point where it was opened for it to close automatically-- if not specified, uses the InteractableComponent:radiusdialog_system.distance_to_close=15-- Whether to disable the controls of the player while the dialog is opendialog_system.disable_controls=false

functiondialog_system.open_dialog(message : message)returnsdialog

typedialog Contains functions relating to the whole dialog.

  • functiondialog.close() Closes the dialog.
  • functiondialog.back() Shows the previous message.
  • functiondialog.show(message) Switch to the specified dialog.
  • functiondialog.is_too_far() Returns boolean whether the player is too far from the point where the dialog was opened at.

typemessage Contains information what should be displayed in a dialog.

  • fieldname (string) Will be displayed in the nameplate, if nil won't display one.
  • fieldtext (string) Text to display, whitespaces in front of each line will be trimmed, to make it easier to use multine strings.
  • fieldportrait (string) File path to the portait to use, can be PNG or an animation/spritesheet XML. (Dimensions should be 64x64)
  • fieldanimation (string) When specifying an XML, specifies the animation to use.
  • fieldtyping_sound (string) Can choose between built ins ("one", "two", "three", "four", "sans").
  • fieldoptions (table[option]) Table of items of type option
  • fieldon_closing (function) Will be called once the dialog starts closing.
  • fieldon_closed (function) Will be called after the dialog has been closed.

typeoption A clickable option at the end of a dialog.

  • fieldtext (string) Will be displayed if the option is enabled.
  • fieldtext_disabled (string) Will be displayed if the option is disabled, if not provided, uses the same as text.
  • fieldenabled (function|boolean) Function/boolean whether this option should be enabled. The function gets passed a table as it's first argument, which allows for convenient access to common stats of the player like gold, hp etc. See example. The function will run once every 30 frames. Arguments: (stats).
  • fieldfunc The function which will be called when selecting the option, if omitted, will default to dialog.close. Arguments: (dialog, stats).

Option enabled function example:

enabled=function(stats)
returnstats.gold>=100end

Text Format

The text you can display is very dynamic, you can use a multitude of different effects and commands to change the text speed, color, etc.

The effects can be used like this:

  • *Text between asterisks will blink*
  • #Text between pound signs will shake#
  • ~Text between tildes will move in a wave pattern~
  • ^Text between carets will have rainbow colors^

Whereas commands are more elaborate and are used like this:

  • {@pause 60} Stops advancing the text for 60 frames.
  • {@delay 10} Sets the delay between typing of each character to 10 frames.
  • {@color FF0000} Sets the text color to red. Format is in RGB hexadecimal.
  • {@img banana} Display an icon/image registered on the dialog_system object.
  • {@sound tick} Sets the typing sound to the specified sound.

This library currently has no to very little error checking or reporting. So, uh just use it correctly!

About

No description, website, or topics provided.

Resources

Stars

3 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages