Jump to content

Recommended Posts

Hey All,

Now that I have become addicted to messing around with this game I am at a point where I need the ability to do more testing.  Basically what I am doing is just seeing what I can and can't do by printing things out and gathering data etc.. This leads me to my question.  Is there a way that I can call a function from the console that I have in my modmain.lua?  Here is a quick example of what I am talking about.

function ShowMeStuff()
	
	local me = AllPlayers[1]
	local x,y,z = me.Transform:GetWorldPosition()
	local ents = TheSim:FindEntities(x,y,z,3)

	for k,v in pairs(ents) do	
			print(v)
	end
end

basically I want to go into game after have loaded the mod and open up the console and type ShowMeStuff() and have it fire that function.

Yes. You can store values or leave reference of functions/tables to GLOBAL.
GLOBAL is one of a mod environment. It is defined in mods.lua

function CreateEnvironment(modname, isworldgen)

	local modutil = require("modutil")
    require("map/lockandkey")

	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,

as you can see, it is the reference of _G which means the global scope of the entire DS(T). By using this, Modders can access the game's global references in literally 'global' scope, not the global scope in the mod in the modmain. To be short, it is unifying stuff.
In runtime, where the point of you write console commands, GLOBAL is not used anymore but be used _G.
And _G can be skipped. Well, it's skipped by default though, For example,
Calling ShowMeStuff() is Identically equal(===) to calling _G.ShowMeStuff(). Reference of two is the same.

You can find other examples in most mods especially the character mods.

local require = GLOBAL.require
local STRINGS = GLOBAL.STRINGS

STRINGS.CHARACTER_TITLES.esctemplate = "The Sample Character"
STRINGS.CHARACTER_NAMES.esctemplate = "Esc"
STRINGS.CHARACTER_DESCRIPTIONS.esctemplate = "*Perk 1\n*Perk 2\n*Perk 3"
STRINGS.CHARACTER_QUOTES.esctemplate = "\"Quote\""

STRINGS.CHARACTERS.ESCTEMPLATE = require "speech_esctemplate"

This is a code wrote in Extended Sample Character mod. As you can see, STRINGS is the variable of GLOBAL.
So putting values into it is indeed accessing the game's global reference and overriding it. And because of the overriding, you need to name it uniquely if you don't want your functions to override or be overridden. It is absolutely no idea to figure out one variable is overridden or not in runtime unless you're using local variables. So you need careful name your variables that are going to go GLOBAL.


If you are careful of the above enough, you can do it stuff like this.

GLOBAL.YAKUMOYUKARI_MODNAME = KnownModIndex:GetModActualName("Yakumo Yukari")
GLOBAL.YUKARI_DIFFICULTY = GetModConfigData("diff")

local Language = GetModConfigData("language")
GLOBAL.YUKARI_LANGUAGE = "en"
if Language == "AUTO" then
	for _, moddir in ipairs(KnownModIndex:GetModsToLoad()) do
		local modname = KnownModIndex:GetModInfo(moddir).name
--		if modname == "한글 모드 서버 버전" or modname == "한글 모드 클라이언트 버전" then 
--			GLOBAL.YUKARI_LANGUAGE = "kr"
		if modname == "Chinese Language Pack" or modname == "Chinese Plus" then
			GLOBAL.YUKARI_LANGUAGE = "ch"
--		elseif modname == "Russian Language Pack" or modname == "Russification Pack for DST" or modname == "Russian For Mods (Client)" then
--			GLOBAL.YUKARI_LANGUAGE = "ru"
		end 
	end 
else
	GLOBAL.YUKARI_LANGUAGE = Language
end

modimport "scripts/tunings_yukari.lua"
TUNING.YUKARI_STATUS = TUNING["YUKARI_STATUS"..(GLOBAL.YUKARI_DIFFICULTY or "")]

This is the code I wrote in my character mod. I personally prefer to store some variables(including functions) in GLOBAL that can be used at multiple times or in multiple files. For GLOBAL.YUKARI_LANGUAGE one, you can check the mod's language option by just using

if GLOBAL.YUKARI_LANGUAGE == "ch" then

this. and if it's not related to modmain, delete GLOBAL. Then it does the same thing. But you have to delete GLOBAL because the other files in your mod have been just placed or patched(if the file name is same) into the runtime. Otherwise, the only distinguishable files of all mods are related to modmain and the files that are imported(by modimport or require).

  • Like 2

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