Solarus-Games English Forum

Solarus => Development => Topic started by: zutokaza on October 05, 2016, 06:02:59 AM

Title: How to pass strings onto another variable?
Post by: zutokaza 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?
Title: Re: How to pass strings onto another variable?
Post by: llamazing 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.
Title: Re: How to pass strings onto another variable?
Post by: zutokaza on October 05, 2016, 07:49:58 AM
Not exactly what I wanted, but your answer gives me two solutions. Thanks again llamazing!  :D