Thread Tools Display Modes
04/25/17, 03:20 PM   #1
Rahon
Join Date: Apr 2017
Posts: 3
function expected instead of nil

Hello,

i run into following issue:


Code:
user:/AddOns/FooAddon/FooAddon.lua:5: function expected instead of nil stack traceback: user:/AddOns/FooAddon/FooAddon.lua:5: in function '(main chunk)'

when im trying to run this code:


Code:
ADDON_NAME = "Test" EVENT_MANAGER: RegisterForEvent(ADDON_NAME,EVENT_MOUNTED_STATE_CHANGED, IsOnMount()) -- d(IsMounted("player")) -- d(onMount) local function IsOnMount(event, onMount) if onMount == IsMounted("player") then d("Mounting") else d("Unmounting") end

I'm guessing that it has something to do with "IsOnMount()" not receiving and/or the EVENT_MANAGER not giving away its variables - in this case the EventID and the Boolean value.

This is my first experience in coding in lua, so im glad for any help I can get.

cheers
  Reply With Quote
04/25/17, 03:36 PM   #2
AssemblerManiac
AddOn Author - Click to view addons
Join Date: Jun 2014
Posts: 51
You have a stray space after "EVENT_MANAGER:"
  Reply With Quote
04/25/17, 08:18 PM   #3
Dolgubon
 
Dolgubon's Avatar
AddOn Author - Click to view addons
Join Date: Jan 2016
Posts: 408
Are you using a text editor that shows line counters? (Or do you have that setting turned on?) Line numbers can be very helpful for fixing bugs, especially in larger programs. For example, the lua error you encountered tells you that the error is on line 5, so you could just look at that particular line and figure out what's wrong.
  Reply With Quote
04/25/17, 11:51 PM   #4
votan
 
votan's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2014
Posts: 577
Originally Posted by Rahon View Post
Code:
ADDON_NAME = "Test" EVENT_MANAGER: RegisterForEvent(ADDON_NAME,EVENT_MOUNTED_STATE_CHANGED, IsOnMount()) -- d(IsMounted("player")) -- d(onMount) local function IsOnMount(event, onMount) if onMount == IsMounted("player") then d("Mounting") else d("Unmounting") end

I'm guessing that it has something to do with "IsOnMount()" not receiving and/or the EVENT_MANAGER not giving away its variables - in this case the EventID and the Boolean value.

This is my first experience in coding in lua, so im glad for any help I can get.

cheers
  1. Welcome here
  2. IsOnMount() is a call not a reference. You write simply InOnMount, no brackets.
  3. In Lua nothing is known before it is declared. Means that IsOnMount is nil at the moment EVENT_MANAGER is used. You have to move it below the function declaration.
  4. ADDON_NAME should not be global, use the local keyword

greets.
  Reply With Quote
04/26/17, 03:20 AM   #5
Rahon
Join Date: Apr 2017
Posts: 3
Originally Posted by Dolgubon View Post
Are you using a text editor that shows line counters? (Or do you have that setting turned on?) Line numbers can be very helpful for fixing bugs, especially in larger programs. For example, the lua error you encountered tells you that the error is on line 5, so you could just look at that particular line and figure out what's wrong.
Yes im using the ZeroBrane IDE.

It seems pretty easy to use
  Reply With Quote
04/26/17, 03:27 AM   #6
Rahon
Join Date: Apr 2017
Posts: 3
Originally Posted by votan View Post
  1. ADDON_NAME should not be global, use the local keyword
How exactly does the keyword function,

Is it telling the EVENT_MANAGER where to look for the mentioned function?

As far as i haven understood it, you can call different variables from other functions with e.g differentFunctionNName.VariableName.

Does self.VariableName equal variableName?

Is this in some sense comparable to a pointer from c?

(Thatīs the only programming language i have basic knowledge about)
  Reply With Quote
04/26/17, 03:43 AM   #7
Dolgubon
 
Dolgubon's Avatar
AddOn Author - Click to view addons
Join Date: Jan 2016
Posts: 408
What Votan means is that when you declare the variable (ADDON_NAME = "Test") you should put local first (local ADDON_NAME = "Test")

That makes it so that ADDON_NAME is only available to code within that file. So if some other addon calls ADDON_NAME, then it will be nil. self.variable name is not generally defined. If you have a table, the table might have a field called variableName, and then it would be defined, but in all other cases it will not be defined. (and in fact, if self is not a table you will get a lua error - 'Attempt to index a non table variable')


You also cannot do functionName.variableName. Local variables within a function are unavailable outside of it, and global variables within a function will be available anywhere simply with variableName.

I would suggest reading some lua tutorials about scope.
  Reply With Quote
04/26/17, 07:20 AM   #8
Letho
AddOn Author - Click to view addons
Join Date: Apr 2016
Posts: 238
I usually create a table for my addons name:

Code:
AM.name = "AuraMastery"
AM.version = "0.08a"

function AM.foo()  -- 'public' method
  bar()
end

local function bar()  -- 'private' method
  d("Look at that huge pile of crap!")
end
  Reply With Quote
04/26/17, 09:56 AM   #9
Rhyono
AddOn Author - Click to view addons
Join Date: Sep 2016
Posts: 659
My rule of thumb has always been that most addons should be in the local scope, but if yours is one that is likely to be incorporated into another (such as MM), then use the global scope. As long as you use a properly unique global name you shouldn't run into issues, however, take CraftStore and Crafting Stations: if they both used global CS, you'd likely run into some issues.
  Reply With Quote

ESOUI » Developer Discussions » Lua/XML Help » 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