• Announcement: Lua.org now officially recommends this forum as a meeting place for the Lua community

Variable can't decide what it is (1 Viewer)

Noneal

Newcomer
Joined
Jun 14, 2022
Messages
1
Reaction score
0
I've been getting really confused with this app I'm building. I'm trying to pass a parameter to a different scene. When I try to access passed in value, I can't use it. I keep getting the error "
bad argument #1 to 'find' (string expected, got table)"
When I try to convert the value from a table to string, I get "bad argument #1 to 'concat' (table expected, got string)". I'm pretty new to Lua, so any help would be very appreciated. Here is some relevant code.

Scene1:
Lua:
local function gotoOpt()
    scene:removeEventListener( "create", scene )
    local customParams = {
        var1 = "scene3"
    }
    composer.gotoScene( "scene2", { effect="fade", time=1, params=customParams })
end

Scene2:
Lua:
function scene:create( event )
          local sceneGroup = self.view
          print(event.params.var1 == string)
          print(event.params.var1 == table)
          print(type(event.params.var1))
          print(type(event.params.var1) == string)
end

The results of the four print commands are as follows:
false
false
string
false
 

Herly Quijano

Newcomer
Joined
Mar 19, 2021
Messages
77
Reaction score
10
The thing in the second, the function type returns a string and you are comparing with a non-string values:
Lua:
-- Bad
print(type(event.params.var1) == string)
print(type(event.params.var1) == table)
-- Good
print(type(event.params.var1) == "string")
print(type(event.params.var1) == "table")
So you are comparing:
Lua:
"string" == string
In fact the values string and table in lua are actually tables, rather libraries:
string: table:
 
Last edited:
Top