Jump to content

Creating global variables, functions, and console commands in modmain.lua


Recommended Posts

Hi, I'm incredibly new to modding don't starve. I'm having trouble with a simplified commands mod, which is exactly as boring as it sounds. So I make a function in modmain.lua, and it's treated as an undefined variable, and I don't understand why, here's the code:

print("Started mod")
c_PrintText = function(things)
	print(tostring(things))
	print("Successfully printed. B haha")
end
c_PrintText("testing")

If there's any advice you could give me on this sort of stuff, send some help this way, please.
 

Link to comment
Share on other sites

I think you're missing something. I'm not sure what it's called, but you kind of have to tell the code where to put your function.

You can do either of these, and probably others:

-- Tell the system that you are creating a local function.
-- This cannot in any way be accessed outside the scope within which it is declared.
local PrintPoo = function(myParameter)
	print("Poo")
end

-- So this will not work.
local uselessFunction = function()
	-- Making a local function within the scope of uselessFunction
	local PrintPoo = function(myParameter)
		print("Poo"..myParameter)
	end
end
-- We are now outside the scope of uselessFunction.
-- This line will give you an error saying PrintPoo doesn't exist.
PrintPoo("some value for myParameter")

-- You can also make a global function.
function PrintPoo(myParameter)
	print("Poo"..myParameter)
end

You can also declare a function for a specific class like this:

-- This is the class
MyClass = class(function(instance, someParameter)
	instance.someParameter = someParameter
end)

-- This is the function being added to the class.
function MyClass:PrintPoo(myParameter)
	print(myParameter)
end

-- Here we create an instance of the class.
myInstance = MyClass("value for someParameter")

-- And here we use the function on the class.
myInstance:PrintPoo("value for myParameter")

But it's not actually that simple, and it would be much better and easier for you to progress in your knowledge of coding and making mods, if you looked at my newcomer post. I HIGHLY recommend taking the LUA Crash Course linked in that post, and looking through some of the simpler syntax examples and generally learning LUA, before you start doing this. It will save you LOADS of time, because you don't have to wait for us to answer questions which are a click away (see section 16).

Edited by Ultroman
Link to comment
Share on other sites

11 hours ago, Ultroman said:

I think you're missing something. I'm not sure what it's called, but you kind of have to tell the code where to put your function.

That's scope, and the person's neglecting the sandbox modmain.lua is put in.

 

To get things like print to function, you'll need to prepend "GLOBAL." to it for accessing it.

From scripts/mods.lua:

Spoiler

	local env = 
	{
        -- lua
		pairs = pairs,
		ipairs = ipairs,
		print = print,
		math = math,
		table = table,
		type = type,
		string = string,
		tostring = tostring,
		Class = Class,

        -- runtime
        TUNING=TUNING,

        -- worldgen
        GROUND = GROUND,
        LOCKS = LOCKS,
        KEYS = KEYS,
        LEVELTYPE = LEVELTYPE,

        -- utility
		GLOBAL = _G,
		modname = modname,
		MODROOT = MODS_ROOT..modname.."/",
	}

 

To 'fix' the code to put the function into the global scope for being able to access at the server's console:

local print = GLOBAL.print
print("Started mod")
GLOBAL.c_PrintText = function(things)
	print(tostring(things))
	print("Successfully printed. B haha")
end
c_PrintText("testing")

 

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
 Share

×
  • Create New...