ESOUI

ESOUI (https://www.esoui.com/forums/index.php)
-   General Authoring Discussion (https://www.esoui.com/forums/forumdisplay.php?f=174)
-   -   KeyBinding - function expected instead of nil (https://www.esoui.com/forums/showthread.php?t=7612)

Shadowfen 02/08/18 07:50 PM

KeyBinding - function expected instead of nil
 
I don't know anymore what how to find what I'm doing wrong.

I've got bindings.xml (formerly named Bindings.xml):
Code:

<Bindings>
  <Layer name="SI_KEYBINDINGS_CATEGORY_GENERAL">
    <Category name="Fast Ride">
      <Action name="FASTRIDE_SWITCH">
        <Down>FastRide.keySwitch()</Down>
      </Action>
    </Category>
  </Layer>
</Bindings>

I've got FastRide.lua:
Code:

FastRide = {
        name = "FastRide",
}
local FR = FastRide
ZO_CreateStringId("SI_BINDING_NAME_FASTRIDE_SWITCH", "Toggle Rapids")

FastRide.keySwitch = function()
        d("keySwitch: start")
        d("keySwitch: end")
end

local function onAddonLoaded(ev, addonName)
        if addonName == FastRide.name then
                EVENT_MANAGER:UnregisterForEvent(FastRide.name, EVENT_ADD_ON_LOADED)
        end
end

EVENT_MANAGER:RegisterForEvent(FastRide.name, EVENT_ADD_ON_LOADED, onAddonLoaded)

SLASH_COMMANDS["/fastride.key"] = function()
        FR.keySwitch()
end

FastRide.lua is followed by bindings.xml in the manifest.

All other addons are turned off (except for BugCatcher).

I can use the /fastride.key and get the messages in chat.
When I bind to a key and then use that key, I get a
Code:

2018-02-08T19:31:32.375-06:00 |cff0000Lua Error: :1: function expected instead of nil
stack traceback:
        :1: in function '(main chunk)'|r

in the interface.log.

Why isn't the keybind working??

Rhyono 02/08/18 10:35 PM

Use a global variable instead of d() for this. You can't always rely on it for load situations. So make it something like test_var = false, then inside the function = true, then afterwards, use /script d(test_var)

So you can see if it's really calling it.

Shadowfen 02/08/18 11:22 PM

It's really not calling it from the keybind because of the error code that claims the function is nil...

Code:

2018-02-08T19:31:32.375-06:00 |cff0000Lua Error: :1: function expected instead of nil
stack traceback:
        :1: in function '(main chunk)'|r

It had other stuff in it before I reduced the code down to the most basic thing that should not crash.
:(

But I can call it from the slash command...

Rhyono 02/08/18 11:36 PM

Have you tried

function FastRide.keySwitch()

instead of

FastRide.keySwitch = function()

Shadowfen 02/09/18 12:17 AM

Quote:

Originally Posted by Rhyono (Post 33861)
Have you tried

function FastRide.keySwitch()

instead of

FastRide.keySwitch = function()


Yeah, I tried that first - then went to the FastRide.keySwitch = function() to see if it would make a difference. It did not.

votan 02/09/18 01:04 AM

From what you have posted I don't see a mistake either.
Try this:
Code:

<Bindings>
  <Layer name="SI_KEYBINDINGS_CATEGORY_GENERAL">
    <Category name="Fast Ride">
      <Action name="FASTRIDE_SWITCH">
        <Down>SLASH_COMMANDS["/fastride.key"]()</Down>
      </Action>
    </Category>
  </Layer>
</Bindings>


Rhyono 02/09/18 01:05 AM

What about making it a standalone global function?

Try just calling a function named KeySwitch() from the bindings that just outputs to chat/var.

Shadowfen 02/09/18 09:28 AM

Changing to

Code:

<Action name="FASTRIDE_SWITCH">
    <Down>SLASH_COMMANDS["/fastride.key"]()</Down>
</Action>

gets me a new error message:

Code:

2018-02-09T09:24:34.222-06:00 |cff0000Lua Error: :1: = expected near '<eof>'|r

Shadowfen 02/09/18 09:45 AM

Creating a global standalone function does work!

I just hate to pollute the global namespace...

Shadowfen 02/11/18 09:40 AM

Found the problem!
 
I wanted to report back that I found what the problem was... and it was not in the keybinding code itself. :o

The name for my saved variables (specified in ZO_SavedVars:NewAccountWide() and ZO_SavedVars:NewCharacterIdSettings() and the manifest file) was the same as the namespace name for the addon. So, the addon namespace was being saved and reloaded by ZO_SavedVars and you cannot save functions as a saved variable - so those would get saved as value nil and reloaded that way.

That was why the global function still worked - because it was in the _G namespace instead of the addon namespace.

You can tell that you have this problem when you look at your addon saved variables file and find all sorts of addon namespace-level variables there, some of them with the value of nil and a generated comment saying that function is not a vaild type. Your addon's behaviour will be wildly unpredictable from toon to toon.

Thank you to everyone for your help.

Mastyr 08/29/18 03:15 PM

Error in Lua
 
user:/AddOns/FasterTravel/FasterTravel.lua:487: attempt to index a nil value
stack traceback:
user:/AddOns/FasterTravel/FasterTravel.lua:487: in function '__new_ZO_Dialogs_ShowPlatformDialog'
<Locals> name = "FAST_TRAVEL_CONFIRM", node = tbl, params = tbl, nodeIndex = 56, dialog = ud </Locals>
user:/AddOns/FasterTravel/FasterTravel.lua:519: in function 'hookFunction'
<Locals> name = "FAST_TRAVEL_CONFIRM" </Locals>
EsoUI/Libraries/Utility/ZO_Hook.lua:19: in function 'ZO_Dialogs_ShowPlatformDialog'

Baertram 08/29/18 03:19 PM

Quote:

Originally Posted by Mastyr (Post 35889)
user:/AddOns/FasterTravel/FasterTravel.lua:487: attempt to index a nil value
stack traceback:
user:/AddOns/FasterTravel/FasterTravel.lua:487: in function '__new_ZO_Dialogs_ShowPlatformDialog'
<Locals> name = "FAST_TRAVEL_CONFIRM", node = tbl, params = tbl, nodeIndex = 56, dialog = ud </Locals>
user:/AddOns/FasterTravel/FasterTravel.lua:519: in function 'hookFunction'
<Locals> name = "FAST_TRAVEL_CONFIRM" </Locals>
EsoUI/Libraries/Utility/ZO_Hook.lua:19: in function 'ZO_Dialogs_ShowPlatformDialog'

1st Did you check if the adodn is updated to the most current version?
2nd The error message telly you alread where to post this message to. The comments of the addon "FasterTravel":
http://www.esoui.com/downloads/info1...eleporter.html
-> btw there are other addons replacing this one afaik. EasyTravel e.g.
3rd You could add info when this error message happens and you might get help then.


All times are GMT -6. The time now is 09:08 PM.

vBulletin © 2024, Jelsoft Enterprises Ltd
© 2014 - 2022 MMOUI