ESOUI

ESOUI (https://www.esoui.com/forums/index.php)
-   Lua/XML Help (https://www.esoui.com/forums/forumdisplay.php?f=175)
-   -   function expected instead of nil (https://www.esoui.com/forums/showthread.php?t=6997)

Rahon 04/25/17 03:20 PM

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

AssemblerManiac 04/25/17 03:36 PM

You have a stray space after "EVENT_MANAGER:"

Dolgubon 04/25/17 08:18 PM

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.

votan 04/25/17 11:51 PM

Quote:

Originally Posted by Rahon (Post 30715)
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.

Rahon 04/26/17 03:20 AM

Quote:

Originally Posted by Dolgubon (Post 30727)
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 :D

Rahon 04/26/17 03:27 AM

Quote:

Originally Posted by votan (Post 30729)
  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)

Dolgubon 04/26/17 03:43 AM

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.

Letho 04/26/17 07:20 AM

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


Rhyono 04/26/17 09:56 AM

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.


All times are GMT -6. The time now is 01:47 PM.

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