Not logged inGosu Forums
Forum back to Help Search Register Login
Up Topic Gosu / Gosu Exchange / Ruby: Operations on strings for dialog
- - By EdwinOdesseiron Date 2013-06-23 11:50
Hi all.

I made a handy dialog system for my project, which stores the dialog within a hash (example dialog looks like that:
'Dialog' => [
          ["Dialog Message"],
          [["Reply 1", "Resulting dialog"]],
          [["Reply 2", "Resulting dialog"]],
          [["Reply N", "Resulting dialog"]]


And it works well, includes line break etc. I also made a small function, that checks if either message or replies include "[PLAYER_NAME]", it is substituted with the name of a player character that is talking:
@message["[PLAYER_NAME]"] = if @message.include?("[PLAYER_NAME]")

And I need 2 more functions like that, and I can't think of how to do them. These functions are
- if message/reply includes "[VAR X]", then it is substituted with the value of variable X (so if I'd write "[VAR $game_width]", it would write "1366" in dialog.
- if message/reply includes "[NOTES KEY]", then this part is deleted, but it also calls function add_note(KEY).

So I pretty much can't figure out how to get variable values from string. Any help will be greatly appreciated!
Parent - By lol_o2 Date 2013-06-23 13:26
For first one use eval method, e.g.
eval "$game_width" would return 1366
Command you put into string after eval is treated like normal code.
Parent - By jlnr (dev) Date 2013-06-23 18:03
String has many different methods that can do this in one way or another - :)

I would recommend String#gsub (globally substitute). You can use it to remove parts from a string by replacing them with '', and (I didn't even know this) you can pass a Hash as the second argument that contains a table to replace things. You could use this for the $game_width part, e.g.

your_dialog_line.gsub /\$[a-z_]+/, { "$game_width" => "1366", "$player_name" => "Herbert", "$level_name" => "Tutorial" }

The /\$[a-z_]+/ bit is a Regexp that will match all $variable_names in your_dialog_line.
Parent - By Spooner Date 2013-07-01 14:20
jlnr shows how best to replace variables, but I wouldn't put actions inside the text.

Use YAML files and nested Hashes rather than code Hash/Array for all this. Much cleaner and keeps it from clogging the code and redesign it to use hashes rather than arrays:

# dialogs.yml
      :talker: Peter Heinz
      :message: |
          Blah Blah Blah
          And what do you think of that, $player_name?
      :sound: key.ogg
         - [:muster_troops, 15] # Calls #muster_troops(15)
         - [:invade_poland] # Calls #invade_poland
            :option: Get off, you fiend!
            :dialog: talking_to_pete_angry
            :option: I love you!
            :dialog: talking_to_pete_lustful
            :option: Do you want to buy some eggs?
            :dialog: talking_to_pete_bemused

EDIT: Forgot to add actions.
Up Topic Gosu / Gosu Exchange / Ruby: Operations on strings for dialog

Powered by mwForum 2.29.7 © 1999-2015 Markus Wichitill