Page 1 of 1

Organized scripts

Posted: Sun Nov 08, 2015 11:20 am
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 ?

Re: Organized scripts

Posted: Sun Nov 08, 2015 12:52 pm
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".

Re: Organized scripts

Posted: Sun Nov 08, 2015 1:20 pm
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) ?

Re: Organized scripts

Posted: Sun Nov 08, 2015 1:36 pm
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)

Re: Organized scripts

Posted: Sun Nov 08, 2015 2:22 pm
by cromcrom
Great, thanks :-)

Re: Organized scripts

Posted: Sun Nov 08, 2015 2:31 pm
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
		}
	}
}

Re: Organized scripts

Posted: Sun Nov 08, 2015 3:29 pm
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)"
...

Re: Organized scripts

Posted: Mon Nov 09, 2015 10:31 am
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.