Quantcast local myTestImport = require "MyDirectory/MyFile" -- Throws exception - ESOUI
Thread Tools Display Modes
05/04/19, 04:13 PM   #1
Kronn8
Join Date: Oct 2017
Posts: 4
[SOLVED] local myTestImport = require "MyDirectory/MyFile" -- Throws exception

THE PROBLEM

I'm trying to refactor my old AddOn into multiple files.

When I run `> lua MyAddon.lua` from the command line, it runs as expected, spitting out "Hello World".

However, I get the familiar "function expected instead of nil" error when the UI loads.

My directory structure:

Code:
Elder Scrolls Online\live\AddOns
  |
  +-- MyAddon\
        |
        +-- MyAddon.lua
        |
        +-- MyDirectory\
              |
              +--MyFile.lua
`MyAddon.lua` contains
Lua Code:
  1. local myTestImport = require "MyDirectory/MyFile" -- Exception: function expected instead of nil
  2. print(myTestImport)

And `MyFile.lua` contains
Lua Code:
  1. return "Hello World"




THE SOLUTION

There was more wrong with my code than I thought. Here's what worked:

`MyAddon.txt` contains
Code:
## Title: MyAddon
## Author: Kronn8
## APIVersion: 100026

MyDirectory\MyFile.lua
FreeMyInventory.lua
`MyAddon.lua` contains
Lua Code:
  1. MyAddon = MyAddon or {}
  2. MyAddon.name = "MyAddon"
  3.  
  4. -- Load files (optional, but otherwise, you'll have to use MyAddon.MyFile each time you want to use MyFile)
  5. local MyFile = MyAddon.MyFile
  6.  
  7. function MyAddon:confirmStartup()
  8.     d(MyFile)
  9. end
  10.  
  11. EVENT_MANAGER:RegisterForEvent(MyAddon.name, EVENT_PLAYER_ACTIVATED, MyAddon.confirmStartup)

And `MyFile.lua` contains
Lua Code:
  1. MyAddon = MyAddon or {}
  2.  
  3. local message = "Hello World"
  4.  
  5. MyAddon.MyFile = message

Last edited by Kronn8 : 05/04/19 at 05:48 PM. Reason: Solved
  Reply With Quote
05/04/19, 04:24 PM   #2
Baertram
 
Baertram's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 2,293
Lua in eso is not allowing to use directory or file commands like require!
Just include the files to load in the manifest txt file of your addon and define a global variable like myAddonNamespace ={}. In your example this would be myTestImport = {} and then e.g. myTestImport.helloWorldText = "Hello World" and then in the other file do d(myTestImport.helloWorldText)
and add your needed variables and functions in different files to it.
  Reply With Quote
05/04/19, 05:32 PM   #3
Kronn8
Join Date: Oct 2017
Posts: 4
Originally Posted by Baertram View Post
Lua in eso is not allowing to use directory or file commands like require!
Just include the files to load in the manifest txt file of your addon and define a global variable like myAddonNamespace ={}. In your example this would be myTestImport = {} and then e.g. myTestImport.helloWorldText = "Hello World" and then in the other file do d(myTestImport.helloWorldText)
and add your needed variables and functions in different files to it.
Thanks for your help! I'll put my functioning code in my original post.
  Reply With Quote

ESOUI » Developer Discussions » General Authoring Discussion » local myTestImport = require "MyDirectory/MyFile" -- Throws exception

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