Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Controller Plugin Question — Gideros Forum

Controller Plugin Question

I'm new to Gideros, it's my first asking here!
With the Controller plugin added to my project, I tried using the sample code on the documentation:

--------------------------------------------------------------------------------
require "controller"

controller:addEventListener(Event.KEY_DOWN, function(e)
print("Button Down ", e.playerId, e.keyCode, findKeyCode(e.keyCode))
end)

controller:addEventListener(Event.KEY_UP, function(e)
print("Button Up ", e.playerId, e.keyCode, findKeyCode(e.keyCode))
end)

controller:addEventListener(Event.RIGHT_JOYSTICK, function(e)
print("Player: ", e.playerId)
print("RIGHT_JOYSTICK:", "x:"..e.x, "y:"..e.y, "angle:"..e.angle, "strength:"..e.strength)
end)

controller:addEventListener(Event.LEFT_JOYSTICK, function(e)
print("Player: ", e.playerId)
print("LEFT_JOYSTICK:", "x:"..e.x, "y:"..e.y, "angle:"..e.angle, "strength:"..e.strength)
end)

controller:addEventListener(Event.RIGHT_TRIGGER, function(e)
print("Player: ", e.playerId)
print("RIGHT_TRIGGER:", "strength:"..e.strength)
end)

controller:addEventListener(Event.LEFT_TRIGGER, function(e)
print("Player: ", e.playerId)
print("LEFT_TRIGGER:", "strength:"..e.strength)
end)

controller:addEventListener(Event.CONNECTED, function(e)
print("Player: ", e.playerId, "connected")
print("Are there any controllers?", controller:isAnyAvailable())
print("Controller count", controller:getPlayerCount())
print("Name of controller "..e.playerId, controller:getControllerName(e.playerId))
print("players", #controller:getPlayers())
end)

controller:addEventListener(Event.DISCONNECTED, function(e)
print("Player: ", e.playerId, "disconnected")
end)
--------------------------------------------------------------------------------

When I connect my joystick it appears on console that there is a player. When I try moving the analogic or the arrows it prints on the screen correctly. But any other keys returns an error like this:

"main.lua:4: attempt to call a nil value"

And the console simply don't work anymore.
What am I doing wrong? Thanks in advance!

Comments

  • piepie Member
    Hi and welcome!
    I have never used that plugin and I don't have a controller to check but you can check with a print_r what is the event receiving: if your code is the one taken from the documentation I guess that something inside the print function doesn't exist and throws an error.
    function print_r(arr, indentLevel)
        local str = ""
        local indentStr = "#"
     
        if(indentLevel == nil) then
            print(print_r(arr, 0))
            return
        end
     
        for i = 0, indentLevel do
            indentStr = indentStr.."\t"
        end
     
        for index,value in pairs(arr) do
            if type(value) == "table" then
                str = str..indentStr..index..": \n"..print_r(value, (indentLevel + 1))
            else 
                str = str..indentStr..index..": "..tostring(value).."\n"
            end
        end
        return str
    end
     
    --use it like print_r(table) where table is the dispatched event table
     
    controller:addEventListener(Event.KEY_DOWN, function(e)
    	print("Button Down")
    	print_r(e)
     
    end)
    p.s. To format as code in this forum just select it from the dropdown and put stuff inside the tags. :)

    +1 -1 (+2 / -0 )Share on Facebook
  • same as pie here :)
    There is also a Gideros Studio sample project for the controller plugin, you can test it as well.

    my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
    +1 -1 (+1 / -0 )Share on Facebook
  • hgy29hgy29 Maintainer


    But any other keys returns an error like this:

    "main.lua:4: attempt to call a nil value"

    And the console simply don't work anymore.

    Your KEY_UP/KEY_DOWN event listeners call a function named 'findKeyCode', but this function isn't defined in your code, that's why you get an 'attempt to call a nil value'.


    Likes: MoKaLux

    +1 -1 (+1 / -0 )Share on Facebook
Sign In or Register to comment.