ESOUI

ESOUI (https://www.esoui.com/forums/index.php)
-   General Authoring Discussion (https://www.esoui.com/forums/forumdisplay.php?f=174)
-   -   local myTestImport = require "MyDirectory/MyFile" -- Throws exception (https://www.esoui.com/forums/showthread.php?t=8510)

Kronn8 05/04/19 04:13 PM

[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

Baertram 05/04/19 04:24 PM

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.

Kronn8 05/04/19 05:32 PM

Quote:

Originally Posted by Baertram (Post 38007)
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.


All times are GMT -6. The time now is 09:56 PM.

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