Jump to content

Recommended Posts

This is suggestion about improving modding system. Most of things for modders will be much easier if you implement this. And it does not affect performance of the core game.

I suggest global variable LOCALS with the following structure: 

--Global variable.
LOCALS = { 
	prefabs = {},
	components = {},
	widgets = {},
	stategraphs = {},
	screens = {},
	brains = {},
	behaviours = {},
}

There are many local functions and variables in lua files of the game. Those files can publish a link to their locals using global variable LOCALS. It will cost nothing (I mean CPU usage). Example for "mound" prefab: 

--Post-initialization in mound.lua before "return" statement.
LOCALS.prefabs.mound = {
	LOOTS = LOOTS,
	ReturnChildren = ReturnChildren,
	spawnghost = spawnghost,
	onfinishcallback = onfinishcallback,
	onfullmoon = onfullmoon,
	GetStatus = GetStatus,
	--OnSave = OnSave,
	--OnLoad = OnLoad,
	OnHaunt = OnHaunt,
	oninit = oninit,
}

After that any modder can use it. For example, it will be possible to change loot from graves: 

--Changing loot in modmain.lua
AddPrefabPostInit("world",function(inst)
	GLOBAL.LOCALS.prefabs.mound.LOOTS.purplegem = 1
end)

Otherwise such tasks are VERY difficult for most of modders.

I think it's very good and important idea. It makes things easier.

Link to comment
https://forums.kleientertainment.com/forums/topic/66987-access-to-local-stuff/
Share on other sites

If this were automated and handled with the mod loader, then might I suggest having a table name for the workshop id?
 

Since namespaces and such, this would keep variables/functions/etc isolated from one another.

 

Like:

GLOBAL.LOCALS.workshop-##.prefabs.mound.LOOTS.purplegem
or
GLOBAL.LOCALS.##.prefabs.mound.LOOTS.purplegem

I'd like to keep it as simple as possible (i.e. as fast as possible). And the better way is initialization of each game file only once.

No need of isolation. I can access the namespace of your mod at any time just using:

local your_env = GLOBAL.ModManager:GetMod("workshop-##").env

It's lua. And it's DS. Everything is accessible. But my suggestion makes things much more easier and simple for modders. They could use local tables, they could disable core event listeners, they could access any other local variables using known local functions, they could check the fact of hooking functions by other mods etc.

I get it with tables.

But with functions? If I do, say:

AddPrefabPostInit("world", function(inst)
	GLOBAL.LOCALS.prefabs.mound.ReturnChildren = function() end
end)

Wouldn't the mound prefab still use the old unedited ReturnChildren?

Unless mound calls LOCALS.prefabs.mound.ReturnChildren() when using the function ReturnChildren.

And I don't know how fast would that be, instead of using locals.

 

Maybe something like?

-- At the beginning in mound.lua

-- Implement it
LOCALS.prefabs.mound = {
	LOOTS = {
		nightmarefuel = 1,
		amulet = 1,
		gears = 1,
		redgem = 5,
		bluegem = 5,
	},
	ReturnChildren = function(inst)
		-- ...
	end,
	spawnghost = function(inst, chance)
		-- ...
	end,
	-- ...
}

-- Override it
-- Mods populate the LOCALS_OVERRIDES table
for k, v in pairs(LOCALS_OVERRIDES.prefabs.mound) do
	LOCALS.prefabs.mound[k] = v
end

-- Localize it
local LOOTS = LOCALS.prefabs.mound.LOOTS
local ReturnChildren = LOCALS.prefabs.mound.ReturnChildren
local spawnghost = LOCALS.prefabs.mound.spawnghost

-- Use it
-- ...

 

Maybe I'm missing the point?

Of course, just giving the function away means you can reference it on stuff like RemoveEventCallback.

1 hour ago, Maris said:

I meant this:

Yeah, you can use the references to use with other functions, like RemoveEventCallback.

But you still would be missing on function overrides that may stay local to the file.

 

local function SetLocals(mod_data)
	LOOTS = mod_data.LOOTS or LOOTS
	ReturnChildren = mod_data.ReturnChildren or ReturnChildren
	spawnghost = mod_data.spawnghost or spawnghost
	onfinishcallback = mod_data.onfinishcallback or onfinishcallback
	onfullmoon = mod_data.onfullmoon or onfullmoon
	GetStatus = mod_data.GetStatus or GetStatus
	OnHaunt = mod_data.OnHaunt or OnHaunt
	oninit = mod_data.oninit or oninit
end

--Post-initialization in mound.lua before "return" statement.
LOCALS.prefabs.mound = {
	LOOTS = LOOTS,
	ReturnChildren = ReturnChildren,
	spawnghost = spawnghost,
	onfinishcallback = onfinishcallback,
	onfullmoon = onfullmoon,
	GetStatus = GetStatus,
	--OnSave = OnSave,
	--OnLoad = OnLoad,
	OnHaunt = OnHaunt,
	oninit = oninit,

	SetLocals = SetLocals,
}

How about this?

15 hours ago, DarkXero said:

How about this?

I'm not sure.

It won't be local functions if you put it in LOCALS table. If you have only links to local functions, you still can change that local functions using lua function setupvalue. But if it will be overrideable in LOCALS table, you can't get a link to 100% local function of the prefab because there is no guarantee that it will not be overwritten.

Also I emphasize that my suggestion won't take additional cpu usage. But your suggestion a little bit more complicated and takes very little additional cpu usage during the game (after initialization).

Ofc your suggestion is good too but... well... it's a different suggestion. It has its advantages and disadvantages.

8 hours ago, Maris said:

It won't be local functions if you put it in LOCALS table. If you have only links to local functions, you still can change that local functions using lua function setupvalue. But if it will be overrideable in LOCALS table, you can't get a link to 100% local function of the prefab because there is no guarantee that it will not be overwritten.

Ah, you are right. Should be more like:

local function SetLocals(mod_data)
	LOOTS = mod_data.LOOTS or LOOTS
	ReturnChildren = mod_data.ReturnChildren or ReturnChildren
	spawnghost = mod_data.spawnghost or spawnghost
	onfinishcallback = mod_data.onfinishcallback or onfinishcallback
	onfullmoon = mod_data.onfullmoon or onfullmoon
	GetStatus = mod_data.GetStatus or GetStatus
	OnHaunt = mod_data.OnHaunt or OnHaunt
	oninit = mod_data.oninit or oninit
end

local function GetLocals()
	return {
		LOOTS = LOOTS,
		ReturnChildren = ReturnChildren,
		spawnghost = spawnghost,
		onfinishcallback = onfinishcallback,
		onfullmoon = onfullmoon,
		GetStatus = GetStatus,
		--OnSave = OnSave,
		--OnLoad = OnLoad,
		OnHaunt = OnHaunt,
		oninit = oninit,
	}
end

--Post-initialization in mound.lua before "return" statement.
LOCALS.prefabs.mound = {
	GetLocals = GetLocals,
	SetLocals = SetLocals,
}

 

8 hours ago, Maris said:

Also I emphasize that my suggestion won't take additional cpu usage. But your suggestion a little bit more complicated and takes very little additional cpu usage during the game (after initialization).

Ofc your suggestion is good too but... well... it's a different suggestion. It has its advantages and disadvantages.

Yes, of course.

I was just brainstorming here, to practically remove the usage of debug library.

Your idea is good too.

Archived

This topic is now archived and is closed to further replies.

Please be aware that the content of this thread may be outdated and no longer applicable.

×
  • Create New...