Quantcast KeyBinding - function expected instead of nil - ESOUI
Thread Tools Display Modes
02/08/18, 07:50 PM   #1
Shadowfen
AddOn Author - Click to view addons
Join Date: Jun 2016
Posts: 39
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??
  Reply With Quote
02/08/18, 10:35 PM   #2
Rhyono
AddOn Author - Click to view addons
Join Date: Sep 2016
Posts: 561
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.
  Reply With Quote
02/08/18, 11:22 PM   #3
Shadowfen
AddOn Author - Click to view addons
Join Date: Jun 2016
Posts: 39
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...
  Reply With Quote
02/08/18, 11:36 PM   #4
Rhyono
AddOn Author - Click to view addons
Join Date: Sep 2016
Posts: 561
Have you tried

function FastRide.keySwitch()

instead of

FastRide.keySwitch = function()
  Reply With Quote
02/09/18, 12:17 AM   #5
Shadowfen
AddOn Author - Click to view addons
Join Date: Jun 2016
Posts: 39
Originally Posted by Rhyono View Post
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.
  Reply With Quote
02/09/18, 01:04 AM   #6
votan
 
votan's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2014
Posts: 498
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>
__________________
@votan73 (EU - megaserver)
  Reply With Quote
02/09/18, 01:05 AM   #7
Rhyono
AddOn Author - Click to view addons
Join Date: Sep 2016
Posts: 561
What about making it a standalone global function?

Try just calling a function named KeySwitch() from the bindings that just outputs to chat/var.
  Reply With Quote
02/09/18, 09:28 AM   #8
Shadowfen
AddOn Author - Click to view addons
Join Date: Jun 2016
Posts: 39
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
  Reply With Quote
02/09/18, 09:45 AM   #9
Shadowfen
AddOn Author - Click to view addons
Join Date: Jun 2016
Posts: 39
Creating a global standalone function does work!

I just hate to pollute the global namespace...
  Reply With Quote
02/11/18, 09:40 AM   #10
Shadowfen
AddOn Author - Click to view addons
Join Date: Jun 2016
Posts: 39
Found the problem!

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

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.
  Reply With Quote
08/29/18, 03:15 PM   #11
Mastyr
Join Date: Nov 2015
Posts: 1
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'
  Reply With Quote
08/29/18, 03:19 PM   #12
Baertram
 
Baertram's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 2,530
Originally Posted by Mastyr View Post
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.
  Reply With Quote

ESOUI » Developer Discussions » General Authoring Discussion » KeyBinding - function expected instead of nil

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off