Problem parsing values to a string

Started by Shimon, January 28, 2017, 01:04:25 PM

Previous topic - Next topic
January 28, 2017, 01:04:25 PM Last Edit: January 28, 2017, 01:24:38 PM by Shimon
Hi, I'm still working on making an equipped item widget to display the currently equipped item and its name.
I managed to sort out getting the image to work, but I can't seem to use those same return values to display the item name.

I return the assigned item name and its variant:
local equipped_name = game:get_item_assigned(1):get_name()
local eq_var = game:get_item(equipped_name):get_variant()

join the data:
local txt_ref = equipped_name .. "." .. eq_var
local item_string = "pause.equipped."  .. txt_ref

The printed result of item_string if I have the bow selected shows
pause.equipped.bow.1

now I want to use that result to reference the text to be displayed under the item icon,  this way when the item changes I don't have to mess about with giant if loops checking against each item

I tried to use:
    widget:make_text(sol.language.get_string(item_string), 12, 32, "center") ** error string expected got table **
However this works:
    widget:make_text(sol.language.get_string("pause.equipped.bow.1"), 12, 32, "center") returns the string: bow

I have tried using tostring() it joins everything up and its value still prints pause.equipped.bow.1 but still returns ** error string expected got table ** when using it inside make_text().
All the processes on converting data into a string I've looked at, make it look like the data is converted into a string but the data always comes back as a table when I'm using it as a string

Any help on this would would be really appreciated.

The error seems to say that your variable item_string contains a list instead of a string.
Could you paste a copy of all the code of the function containing that code so that we can find the problem? Thanks.
"If you make people think they're thinking, they'll love you. But if you really make them think, they'll hate you."

January 28, 2017, 03:00:07 PM #2 Last Edit: January 28, 2017, 03:34:30 PM by Shimon
Yes that's my problem I need to convert the value returned from item_string: pause.equipped.bow.1
into a string value of:  "pause.equipped.bow.1"

http://pastebin.com/P4pS9YgV

This script no longer throws either: string expected got table or string expected got nil error,  but the if loop used to check the conversion still returns false even more puzzled now

Many thanks for looking into this.

Think I've just figured it script above didn't concatenate properly : local item_string = "pause.equipped." .. eq_name_con  .. eq_var_con
I forgot the to add the "."  to separate the values
I've just done little test and text is showing up and changing the equipped item changes the text - sweet!
Looks like I've got things sorted :)
Many thanks anyway.

No problem! :D
"If you make people think they're thinking, they'll love you. But if you really make them think, they'll hate you."

hmmmm, why using tostring() if the name of the object is already a string ?

January 28, 2017, 06:30:36 PM #5 Last Edit: January 28, 2017, 06:32:19 PM by Shimon
It's actually what helped me figure it out, realizing its the variant number value that was causing the problem. The equipped_name returned true when validating it was a string and the prefixed value of item_string is a string anyway so it had to be the var value which was causing problems (it just took a while and lots of testing to see it), also trying to join up the values before using tostring() was not a good idea ;) It's cleaned up and working now.