Author Topic: How to pass strings onto another variable?  (Read 1100 times)

zutokaza

  • Full Member
  • ***
  • Posts: 146
  • Active - Making stories can take the most time.
    • View Profile
How to pass strings onto another variable?
« on: October 05, 2016, 06:02:59 am »
local w = "W"
local h = "H"
local a = "A"
local t = "T"
local foo

foo = w .. h .. a .. t

print(foo)

I get "WHAT," but my issue is with order. What if I press a button called "t" and I want "t" to appear first?

"TWHA"

or other buttons

"TAHW"

Basically, how to have strings order appear by what button is pressed and have them equal the "foo" variable?
« Last Edit: October 05, 2016, 06:16:32 am by zutokaza »

llamazing

  • Full Member
  • ***
  • Posts: 201
    • View Profile
Re: How to pass strings onto another variable?
« Reply #1 on: October 05, 2016, 06:30:06 am »
Again, you probably want to use a menu for this task, but without knowing more about what you are trying to do, it is hard to say.

Assuming "WHAT" is a sequence of characters entered by the player, you'd want to use the menu:on_character_pressed(character) function. It could look something like this:
Code: (lua) [Select]
local chars_pressed = {}
function my_menu:on_character_pressed(character)
table.insert(chars_pressed, character)
end

function my_menu:print()
local str = table.concat(chars_pressed)
print(str)
chars_pressed = {} --clear table
end

Note that this is compatible with utf-8 (international) characters, but be warned that str:len() will return the number of bytes in the string, not necessarily the number of characters. i.e. if the player enters 4 characters that are each multi-byte characters of 2 bytes, then str:len() would return 8 even though there are only 4 characters. Instead, #chars_pressed can be used to get the number of characters entered.

zutokaza

  • Full Member
  • ***
  • Posts: 146
  • Active - Making stories can take the most time.
    • View Profile
Re: How to pass strings onto another variable?
« Reply #2 on: October 05, 2016, 07:49:58 am »
Not exactly what I wanted, but your answer gives me two solutions. Thanks again llamazing!  :D