Jump to content

Recommended Posts

Hi,

 

i am trying to create a mod for don't starve together which is so far a real pain! I already created a few lua plugins for other games before so i have a bit lua experience but i am drowning in lua errors..

modmain.lua"]:2: attempt to call global 'require' (a nil value)

modmain.lua"]:11: attempt to index global 'io' (a nil value)

to mention two of them.

I am trying to create a small plugin to help admins to administrate their server because executing lua code through an input field is not very comfortable.

Also, do i really have to reload the whole game everytime i change my lua files? Can't the lua universe reload the specific files itself (as implemented by other games)

 

--require("io")
--require "util"
Admin = {}



-- End of config

function Admin:Log(str)
	file = io.open("adminlog.txt", "a")
	file.writeLine(str)
	file.close()
end

Admin:Log("Test")
print("[ADMINCOMMANDS] Loading files...")
include('hooks.lua')

This is my modmain.lua


print("[ADMINCOMMANDS] Loading hooks.lua..")

-- function ExecuteConsoleCommand(fnstr, guid, x, z)
local OldExecuteConsoleCommand = GLOBAL.ExecuteConsoleCommand
local function ExecuteConsoleCommand(fnstr, guid, x, z)

	return OldExecuteConsoleCommand(fnstr, guid, x, z)
end

This is my hooks.lua (i am not even sure if dst supports:

include()

Hopefully someone can help me..

Link to comment
https://forums.kleientertainment.com/forums/topic/64136-weird-lua-behavior/
Share on other sites

"modmain.lua" and "modworldgenmain.lua" don't have direct access to the global environment. Use GLOBAL.require, etc.

Also, you can unload packages like this (loading them again might require more work depending on what script you're reloading):

package.loaded["prefabs/evergreen"] = nil --delete the existing package

Thank you for your fast reply the GLOBAL. solution was working to acces io but it wont let me access file

Quote

:13: variable 'file' is not declared

However since there is no way to gain access to the global environment i will abort at this point :/ it just would make things too complicated

I think i found it

	local env = 
	{
		TUNING=TUNING,
		modname = modname,
		pairs = pairs,
		ipairs = ipairs,
		print = print,
		math = math,
		table = table,
		type = type,
		string = string,
		tostring = tostring,
		Class = Class,
		GLOBAL = _G,
		MODROOT = MODS_ROOT..modname.."/",
	}

i think these are the only functions / calls accessible within a module..

16 hours ago, MrCoala said:

i think these are the only functions / calls accessible within a module..

To clarify:

  1. You can access any global variable from the modmain environment using GLOBAL. That's a variable for _G, the global environment.
  2. This environment only affects modmain.lua and modworldgenmain.lua (and scripts imported from said files using modimport("filename")). Other modules, such as "prefabricated" entities, components, stategraphs, etc. have full access to the global environment per se.

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...