Ask a simple question, get a simple answer

Ask for help about creating mods and scripts for Grimrock 2 or share your tips, scripts, tools and assets with other modders here. Warning: forum contains spoilers!
User avatar
Eleven Warrior
Posts: 745
Joined: Thu Apr 18, 2013 2:32 pm
Location: Australia

Re: Ask a simple question, get a simple answer

Post by Eleven Warrior »

Do you need a tester for your mod?
User avatar
7Soul
Posts: 209
Joined: Sun Oct 19, 2014 1:56 am
Location: Brazil

Re: Ask a simple question, get a simple answer

Post by 7Soul »

Is there a way to include a lua file into another file? I'm trying to organize my 4k lines script, but I'm not having luck with 'loadfile' or 'require'
Eleven Warrior wrote: Mon Oct 07, 2019 1:06 am Do you need a tester for your mod?
Me? Not yet
Join the LoG discord server: https://discord.gg/ArgAgNN :D

My Mods
User avatar
Isaac
Posts: 3185
Joined: Fri Mar 02, 2012 10:02 pm

Re: Ask a simple question, get a simple answer

Post by Isaac »

7Soul wrote: Mon Oct 07, 2019 8:35 pm Is there a way to include a lua file into another file? I'm trying to organize my 4k lines script, but I'm not having luck with 'loadfile' or 'require'
It depends on how you intend to use it.

Use: import "mod_assets/script/my_script.lua"
...in any of the startup scripts, to insert the contents of a file into that script when it's loaded.

There is a hoop to hop through for getting script_entity sources to load during the game.

You must first add a ScriptController component, and name it 'controller'; it must precede the script component in the definition. You must load the source for the Script component inside the ScriptController's onInit function.
User avatar
7Soul
Posts: 209
Joined: Sun Oct 19, 2014 1:56 am
Location: Brazil

Re: Ask a simple question, get a simple answer

Post by 7Soul »

Isaac wrote: Tue Oct 08, 2019 2:20 am
7Soul wrote: Mon Oct 07, 2019 8:35 pm Is there a way to include a lua file into another file? I'm trying to organize my 4k lines script, but I'm not having luck with 'loadfile' or 'require'
It depends on how you intend to use it.

Use: import "mod_assets/script/my_script.lua"
...in any of the startup scripts, to insert the contents of a file into that script when it's loaded.

There is a hoop to hop through for getting script_entity sources to load during the game.

You must first add a ScriptController component, and name it 'controller'; it must precede the script component in the definition. You must load the source for the Script component inside the ScriptController's onInit function.
I'm a little lost maybe I explained my problem badly

I have a lua file called functions.lua, and in-game I have a script_entity (called "functions") that loads functions.lua, and I also import it in init.lua

What I was looking for was to divide functions.lua into smaller files, all loaded into the single script_entity, so all my functions.script.whatever() still work
Join the LoG discord server: https://discord.gg/ArgAgNN :D

My Mods
User avatar
Isaac
Posts: 3185
Joined: Fri Mar 02, 2012 10:02 pm

Re: Ask a simple question, get a simple answer

Post by Isaac »

Do you mean... having many single, or a few function containing scripts, and << somehow >> loading them all into one script_entity—while keeping the function files all separated?

If so, why—exactly/specifically?
User avatar
7Soul
Posts: 209
Joined: Sun Oct 19, 2014 1:56 am
Location: Brazil

Re: Ask a simple question, get a simple answer

Post by 7Soul »

Isaac wrote: Tue Oct 08, 2019 8:52 am Do you mean... having many single, or a few function containing scripts, and << somehow >> loading them all into one script_entity—while keeping the function files all separated?

If so, why—exactly/specifically?
I like keeping my functions organized when I do programming, maybe put my champion functions in one file, monster stuff in another, misc stuff in another, etc. I can't just put it in another script_entity because the I'd have to add "script_entity_name.script" in front of everything
Join the LoG discord server: https://discord.gg/ArgAgNN :D

My Mods
User avatar
Isaac
Posts: 3185
Joined: Fri Mar 02, 2012 10:02 pm

Re: Ask a simple question, get a simple answer

Post by Isaac »

7Soul wrote: Tue Oct 08, 2019 8:38 pm I can't just put it in another script_entity because the I'd have to add "script_entity_name.script" in front of everything
You can define (at the top of every script) a shorter name for the script component—like "ut" for instance.

Code: Select all

ut = script_entity_name.script
The only other way that I am aware of, is to use the [volatile] Config object to hold your functions.

Example:

Code: Select all

--objects.lua

--custom 'global'~ish functions

Config.Fn = {}
	Config.Fn.chYell = function(champion) champion:playDamageSound() end
	Config.Fn.chFormat = function(champion, text)
						local text = text or ""			
						text = text:gsub("pro@", iff(champion:getSex()=='male','his', 'her')) 
						text = text:gsub("name@", champion:getName()) 
						return text
					end
	Config.Fn.chRage = function(champion) champion:setConditionValue("rage", .1) end

Code: Select all

--script_entity_1

function stub()
	if math.random(8) < 5 then
		math.randomseed(Time.systemTime())
		local champ = party.party:getChampion(math.random(4))
		local eventStrings = {
				"name@ stubbed pro@ toe!",
				"name@ was bitten by fleas!",
				"name@ has tripped!",
				"name@ has something in pro@ eye!",
				"name@ has itchy feet!",
			}
		
		Config.Fn.chYell(champ)
		hudPrint(Config.Fn.chFormat(champ, eventStrings[math.random(#eventStrings)]))
		Config.Fn.chRage(champ)
	end
end

party.party:addConnector("onMove", self.go.id, "stub")

minmay
Posts: 2780
Joined: Mon Sep 23, 2013 2:24 am

Re: Ask a simple question, get a simple answer

Post by minmay »

Isaac wrote: Wed Oct 09, 2019 10:56 am
7Soul wrote: Tue Oct 08, 2019 8:38 pm I can't just put it in another script_entity because the I'd have to add "script_entity_name.script" in front of everything
You can define (at the top of every script) a shorter name for the script component—like "ut" for instance.

Code: Select all

ut = script_entity_name.script
this will crash when the player tries to save
Grimrock 1 dungeon
Grimrock 2 resources
I no longer answer scripting questions in private messages. Please ask in a forum topic or this Discord server.
User avatar
Isaac
Posts: 3185
Joined: Fri Mar 02, 2012 10:02 pm

Re: Ask a simple question, get a simple answer

Post by Isaac »

Yeah. :(
The ut variable would have to be local, inside of a 'do' block; along with the statements that use it.
User avatar
Eleven Warrior
Posts: 745
Joined: Thu Apr 18, 2013 2:32 pm
Location: Australia

Re: Ask a simple question, get a simple answer

Post by Eleven Warrior »

How do I change the ambient track in a level, like a plate connected to a function ty :)
Post Reply