View Single Post
05/04/19, 04:13 PM   #1
Kronn8
Join Date: Oct 2017
Posts: 5
[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