Lua variable type confusion

Viewed 61

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 the passed in value from the new scene, 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:

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

Scene2:

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

1 Answers

I cant really say anything about your problems with find and concat without seeing how you use them, although for me I've got problems in the past when mixing up sting.find and :find, which CAN give some of those errors.

But regarding types, type(something) returns a string indicating a type ("string", "boolean", "table", etc), so any comparisons need to compare to a string to be true, which is why your type comparisons return false when you don't expect it.

Related