How to pass strings onto another variable?

Started by zutokaza, October 05, 2016, 06:02:59 AM

Previous topic - Next topic
October 05, 2016, 06:02:59 AM Last Edit: October 05, 2016, 06:16:32 AM by zutokaza
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?
Solarus Works on ReactOS Opensource Windows OS

https://www.reactos.org/forum/viewtopic.php?f=2&t=14759&p=120616#p120616

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.

Not exactly what I wanted, but your answer gives me two solutions. Thanks again llamazing!  :D
Solarus Works on ReactOS Opensource Windows OS

https://www.reactos.org/forum/viewtopic.php?f=2&t=14759&p=120616#p120616