Jump to content

Lua issues revolving around mod structure; modules; and require()


TheodoreLogan

Recommended Posts

Hello, 

 

I'm new to modding Project Zomboid and, if someone has a moment, I'm having a few difficulties.

 

My questions revolve around an issue I'm having where I'm not able to use 'require' to get my other modules like I normally would.

Here is what I would like to do in a bare bones example:

 

--File: media/lua/server/MyMod/MyMod.lua



MyMod = {}
print('Setting up...');
MyMod.module1 = require 'MyMod/Module1.lua';
print('Loaded module: '..MyMod.module1.foo());


 

--File: media/lua/server/MyMod/Module1.lua



local M = {}
function M.foo() return 'I\'m Module1!'; end
return M;


 

When I load the mod I get an exception telling me MyMod.module1 is null, when I expect to get my returned Module1 table, or an exception that it could not find the file.

 

--Output:



Loading: ../media/lua/server/MyMod/MyMod.lua
Setting up...
-----------------------------------------
attempted index: foo of non-table: null
-----------------------------------------


 

So, if you bare with me I have three related questions:

1. 

Does PZ stop me from using require to load other lua files? why? (or am I doing it wrong?)

 

2. 

Why does PZ load every .lua file in my mod. I normally expect a single point of entry, such as loading the file "MyMod/MyMod.lua" which would then allow me to decide what modules the mod loads.

Is there a reason it doesn't have a single point of entry and how should I be using this to my advantage? 

 

3.

Other mods declare their modules using their mod name as a prefix to avoid possible conflicts; I was hoping I could avoid pollution of the global scope and just have everything under one roof, i.e inside MyMod, instead of MyMod;MyModModule1;MyModModule2;etc.

Is there any way to do this?

 

Thanks for reading, 

Any questions/clarifications please ask!

 

Logan.

 

Update: I'm guessing my issue with require() has something to do with the differences between Kahlua2 and Lua 5.1? (only because my example works fine using Lua 5.1)

Link to comment
Share on other sites

1.

After a quick test I'm not getting require() to cause any action either, but this is logical as you mention the files are already required 

 

2.

This is a little weird but not too crazy in a language in which everything is global unless it's not. The biggest problem is dependency in an environment where you cannot programatically control the loading of resources; however PZ's mass script loader seems to consistently load files in alphabetical order. 

 

3.

Declare "MyMod = {}" near the top of the load chain and have your modules insert their features

 

Edit:

There is also a "Require" key of mod.info, that force-loads another mod. It's used by http://pz-mods.net/other/ModUtilities/

Link to comment
Share on other sites

Declare "MyMod = {}" near the top of the load chain and have your modules insert their features

 

Firstly, thanks for answering tet!   :D

and thanks for testing require, it's nice to know I'm not going mad =)

 

My main concern with this method is that it becomes the responsibility of each module to load itself into the parent (MyMod) rather than the other way around. Also it would bug me to have something like "aaa.lua" just to make sure it loads first.

 

I've been looking through Kahlua2 which PZ uses to load its lua, and I think I am going to set up my own test project to see if the require implementation displays the same behaviour I'm seeing in PZ. If it does I'll put a request in on Github to try and fix it.

 

If I find any answers I'll update this post =)

Link to comment
Share on other sites

I should clarify that I was using require in the way you wanted to use it, on in-mod Lua -- files that get required by the mod loader.

 

Many mods require files from ProjectZomboid/media/lua/*

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...