External Scripts / Organisation

Talk about creating Grimrock 1 levels and mods here. Warning: forum contains spoilers!
User avatar
Fhizban
Posts: 68
Joined: Sun Oct 21, 2012 2:57 pm
Location: Germany
Contact:

External Scripts / Organisation

Post by Fhizban »

hey,
maybe some of the script gurus can help: i like to keep the bulk of my scripts in external files. now this seems to be impossible, i can only put item/object/etc. definitions into external lua scripts. did i forget something?

example: i have a custom barrel and would like to do: onDie = fiz_spawnLoot()

now the fiz_spawnLoot() would be defined in a external lua script and NOT in a script entity. is this even possible? the script is quite long and will be modified/updated often. also i would like to call this function from various hooks (onDie of objects and monsters etc.)

I saw something similiar in the log framework, but did not fully grasp its functionality.
--
Fhizban's Asset Repository - the place where you find all my LoG contributions:
viewtopic.php?f=14&t=3904
User avatar
Wolfrug
Posts: 55
Joined: Wed Oct 03, 2012 6:56 pm

Re: External Scripts / Organisation

Post by Wolfrug »

Fhizban wrote:hey,
maybe some of the script gurus can help: i like to keep the bulk of my scripts in external files. now this seems to be impossible, i can only put item/object/etc. definitions into external lua scripts. did i forget something?

example: i have a custom barrel and would like to do: onDie = fiz_spawnLoot()

now the fiz_spawnLoot() would be defined in a external lua script and NOT in a script entity. is this even possible? the script is quite long and will be modified/updated often. also i would like to call this function from various hooks (onDie of objects and monsters etc.)

I saw something similiar in the log framework, but did not fully grasp its functionality.
AFAIK you can't, but it's really not that much of a hassle to use script entities. You can gather them on top of level 1 or something in the corner and give them all descriptive names. Easier to fix errors that way as well ;)

-Wolfrug
Try my Mordor: Depths of Dejenol LoG-ification. Feedback much appreciated!
User avatar
Fhizban
Posts: 68
Joined: Sun Oct 21, 2012 2:57 pm
Location: Germany
Contact:

Re: External Scripts / Organisation

Post by Fhizban »

thanks wolfrug, do you mean they are global? so i dont have to place the same script entity on each dungeon level? yiehaa!
--
Fhizban's Asset Repository - the place where you find all my LoG contributions:
viewtopic.php?f=14&t=3904
User avatar
Xanathar
Posts: 629
Joined: Sun Apr 15, 2012 10:19 am
Location: Torino, Italy
Contact:

Re: External Scripts / Organisation

Post by Xanathar »

Script entities are global, and you can even refer to them from "outside" scripts (say, for example, in a party hook defined in init.lua).

What I usually do for any scripting entity of size (not the smallish ones containing hudPrints and little more):
  • Have a file whose name is the name of the scripting entity, prefixed with underscore
  • Whenever I want to make some change, I change it in the lua file and copy/paste it in the entity
I think this could be worth a tool.. maybe I'll do it sooner or later.

Another point on my workflow if it can be of help to anyone: I've created a file system junction so that my dungeons are all also inside the dropbox folder for constant backup :)
Waking Violet (Steam, PS4, PSVita, Switch) : http://www.wakingviolet.com

The Sunset Gate [MOD]: viewtopic.php?f=14&t=5563

My preciousss: http://www.moonsharp.org
User avatar
msyblade
Posts: 792
Joined: Fri Oct 12, 2012 4:40 am
Location: New Mexico, USA
Contact:

Re: External Scripts / Organisation

Post by msyblade »

You can use one script entity, and call/trigger it multiple times from multiple sources.I used it for party exp awards, probably 30 triggers on that entity throughout the dungeon. I also use a global SE for Ixnatifuals randomizing tomes.
Currently conspiring with many modders on the "Legends of the Northern Realms"project.

"You have been captured by a psychopathic diety who needs a new plaything to torture."
Hotel Hades
User avatar
Komag
Posts: 3654
Joined: Sat Jul 28, 2012 4:55 pm
Location: Boston, USA

Re: External Scripts / Organisation

Post by Komag »

in-editor script entities have very big advantage of non-crash-inducing debugging. I would take five times as long to make my scripts work if I had to restart the editor for every little typo, not to mention even being able to figure out what I did wrong in the first place.
Finished Dungeons - complete mods to play
User avatar
msyblade
Posts: 792
Joined: Fri Oct 12, 2012 4:40 am
Location: New Mexico, USA
Contact:

Re: External Scripts / Organisation

Post by msyblade »

Great, great point Komag. The editor names any flaws in a script, and points them out to you individually, until they all work. Big help that is when your heads all muddled with strings and numbers.
Currently conspiring with many modders on the "Legends of the Northern Realms"project.

"You have been captured by a psychopathic diety who needs a new plaything to torture."
Hotel Hades
3socks
Posts: 11
Joined: Sun Nov 04, 2012 11:29 pm

Re: External Scripts / Organisation

Post by 3socks »

I've looked at LOG framework code and its authors were able to hack it a little bit to achieve the effect of including external scripts.

I will write a short tutorial about how they do it, and how anyone can make use of external scripts. It's an advanced topic, so be aware of it.

1. Suppose we create an external script in mod_assets/scripts directory called hello.lua and we write some code in it.

Code: Select all

function hello()
  print('Hello World')
end
2. Then we have to import this file in init.lua file in scripts directory.

Code: Select all

-- import standard assets
import "assets/scripts/standard_assets.lua"

-- import custom assets
import "mod_assets/scripts/hello.lua"
3. Now comes the hack. We have to spawn the code somehow and they use an entity for that. In their case it's dungeon_door_metal, but I gues it can be almost any kind of object. We will add this code to our hello.lua script.

Code: Select all

cloneObject{
	name = "HelloDoor",
	baseObject = "dungeon_door_metal",
	onOpen = function()
		spawn("script_entity", 1,1,1,0,'hello')
		hello:setSource(obj)
   end
}
In onOpen method of this object, we spawn a script. The last attribute of spawn method is the name by which we can access our code later. We then set a source to obj.

4. obj has to be a string containing our code. I think the game mechanics then use some kind of eval on this later. We need to encapsulate our code in multiline string. So we will replace our hello function with this.

Code: Select all

local obj = [[
function hello()
  print('Hello World')
end
]]
5. Finally in the log dungeon editor we create a script that loads this entity, in this case a door, and we call it's open function. This will call the onOpen method and our code will be spawned.

Code: Select all

spawn("HelloDoor", 1,1,1,0,'helloInit')
helloInit:open()
We then don't need the door so we can destroy it.

Code: Select all

helloInit:destroy()
6. As I metioned in a step 3, we can now access our code through a global variable hello (that's what we passed to the spawn method) anywhere in our scripts.

Code: Select all

function test()
	hello.hello()
end
This code will call the function hello in our external script and print 'Hello world' to the screen.

And that's it. We can now have our code structured in separate scripts.

EDIT:
Fixed a few typos and rename some variables to make it more understandable.
Last edited by 3socks on Mon Nov 05, 2012 5:10 pm, edited 2 times in total.
User avatar
Grimwold
Posts: 511
Joined: Thu Sep 13, 2012 11:45 pm
Location: A Dungeon somewhere in the UK

Re: External Scripts / Organisation

Post by Grimwold »

3socks wrote:I've looked at LOG framework code and its authors were able to hack it a little bit to achieve the effect of including external scripts.

I will write a short tutorial about how they do it, and how anyone can make use of external scripts. It's an advanced topic, so be aware of it.
Excellent tutorial! Thanks for sharing this.

Just to clarify one thing... does the code in step 4. actually replace the code we wrote in step 1. in our external lua file?
User avatar
Fhizban
Posts: 68
Joined: Sun Oct 21, 2012 2:57 pm
Location: Germany
Contact:

Re: External Scripts / Organisation

Post by Fhizban »

@3socks thank you so much! this is exactly what i was looking for. I will check it out after work.
--
Fhizban's Asset Repository - the place where you find all my LoG contributions:
viewtopic.php?f=14&t=3904
Post Reply