Organized scripts

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!
Post Reply
User avatar
cromcrom
Posts: 549
Joined: Tue Sep 11, 2012 7:16 am
Location: Chateauroux in a socialist s#!$*&% formerly known as "France"

Organized scripts

Post by cromcrom »

I have quite a lot of external lua files, holding my scripts, in the mod. I add them as script entities on the mod, but I would like to have only one file that holds them all. I gave a try to some scripts I saw here and there, but didn't get any satisfying result.
Could someone show me the basic stuff I have to setup to keep things organized, please ?
A trip of a thousand leagues starts with a step.
minmay
Posts: 2768
Joined: Mon Sep 23, 2013 2:24 am

Re: Organized scripts

Post by minmay »

Do you mean you want to load all of the files in the same script entity? In that case, using an object with multiple ScriptComponents, one for each file, would work. Note that ScriptControllerComponent will only see the ScriptComponent named "script".
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
cromcrom
Posts: 549
Joined: Tue Sep 11, 2012 7:16 am
Location: Chateauroux in a socialist s#!$*&% formerly known as "France"

Re: Organized scripts

Post by cromcrom »

Nice, thanks. How would I call them ?
Actually, if I want to access my library from another script, it goes like:
cromlibrary.script.checkSkill(whatever)

would new name be
globalScript.script.cromlibrary.checkSkill(whatever) ?
A trip of a thousand leagues starts with a step.
minmay
Posts: 2768
Joined: Mon Sep 23, 2013 2:24 am

Re: Organized scripts

Post by minmay »

um...the same way you would do it for any other ScriptComponent: [name of object].[name of component].[field]

if you want to name the object "globalScript" and the component "cromlibrary", then it would be globalScript.cromlibrary.checkSkill(whatever)
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
cromcrom
Posts: 549
Joined: Tue Sep 11, 2012 7:16 am
Location: Chateauroux in a socialist s#!$*&% formerly known as "France"

Re: Organized scripts

Post by cromcrom »

Great, thanks :-)
A trip of a thousand leagues starts with a step.
User avatar
JohnWordsworth
Posts: 1397
Joined: Fri Sep 14, 2012 4:19 pm
Location: Devon, United Kingdom
Contact:

Re: Organized scripts

Post by JohnWordsworth »

I have done this with GrimTK so that you can use the functions using nice and simple calls like "GTK.Widgets.DoSomething()" where GrimTK is the entity, Widgets is a script component on that entity and DoSomething is just a function in that script. What I have done is create an object called "grimtk" which can be called anything (well, apart from "GTK" I guess) in the game. When this object is initialised, it searches for an entity called "GTK" - if it doesn't already exist, then we set it up with all of the components on it loaded from separate files.

This what the GTK generator entity looks like...

Code: Select all

defineObject{
	name = "grimtk",
	baseObject = "script_entity",
	components = {
		{
			class = "Null",
			onInit = function(self)
				if ( findEntity("GTK") == nil ) then
					local gtk = spawn("script_entity", 1, 1, 1, 1, 1, "GTK");
					gtk.script:loadFile("mod_assets/ext/grimtk/scripts/hook.lua");
					gtk:createComponent("Script", "Constants"):loadFile("mod_assets/ext/grimtk/scripts/gtk/constants.lua");
					gtk:createComponent("Script", "Input"):loadFile("mod_assets/ext/grimtk/scripts/gtk/input.lua");					
					gtk:createComponent("Script", "Core"):loadFile("mod_assets/ext/grimtk/scripts/gtk/core.lua");
					gtk:createComponent("Script", "Widgets"):loadFile("mod_assets/ext/grimtk/scripts/gtk/widgets.lua");

					if ( Editor.isRunning() ) then
						gtk:createComponent("Script", "Debug"):loadFile("mod_assets/ext/grimtk/scripts/gtk/debug.lua");
					end

					delayedCall("GTK", 0.01, "emitGtkDidLoad");
				end
				
				if ( findEntity("GTKGui") == nil ) then
					local gtkgui = spawn("script_entity", 1, 1, 1, 1, 1, "GTKGui");
					gtkgui:createComponent("Script", "Basic"):loadFile("mod_assets/ext/grimtk/scripts/gtkgui/basic.lua");
					gtkgui:createComponent("Script", "Dialogue"):loadFile("mod_assets/ext/grimtk/scripts/gtkgui/dialogue.lua");
					gtkgui:createComponent("Script", "Cinematic"):loadFile("mod_assets/ext/grimtk/scripts/gtkgui/cinematic.lua");					
				end
			end
		}
	}
}
My Grimrock Projects Page with links to the Grimrock Model Toolkit, GrimFBX, Atlas Toolkit, QuickBar, NoteBook and the Oriental Weapons Pack.
User avatar
cromcrom
Posts: 549
Joined: Tue Sep 11, 2012 7:16 am
Location: Chateauroux in a socialist s#!$*&% formerly known as "France"

Re: Organized scripts

Post by cromcrom »

Yes john, I studied your script, but get lost between the init, import, and stuff like that.

Right now, I created a lua file called "cromscripts".
I added this code in it:

Code: Select all

defineObject{
	name = "cromscripts",
	baseObject = "base_item",
	components = {
		{class="script",name="crombooks",loadFile=("mod_assets/scripts/crombooks.lua")}
		}
}
I then created a script entity called cromscripts, and attached this lua file to it.

Then I got a "define attempt to call global: defineObject (a nil value)"
...
A trip of a thousand leagues starts with a step.
User avatar
gambit37
Posts: 218
Joined: Fri Mar 02, 2012 3:40 pm

Re: Organized scripts

Post by gambit37 »

I'm pretty sure you can't use 'defineObject' in the editor, which is what your scenario is describing.

In the case you outlined, the code in your script entity should be "cromscripts.crombooks.nameOfFunction()" where nameOfFunction is the name of a function in your imported "crombooks.lua" file.
Post Reply