Can you define a script_entity with functions?

Talk about creating Grimrock 1 levels and mods here. Warning: forum contains spoilers!
Post Reply
User avatar
pferguso
Posts: 40
Joined: Tue Nov 06, 2012 6:09 pm

Can you define a script_entity with functions?

Post by pferguso »

Is it possible to define a script_entity that has its own functions contained within it? Normally, I'd make a few functions in a script entity and place it on the level in the editor. What I'd like to do is be able to spawn a certain script from a monster, that contains useful functions that the monster, and other monsters, and existing level scripts can access.

Here's what I have:

Code: Select all

defineObject{
	name = "helper_functions",
	class = "ScriptEntity",
	helloWorld = function()
		print("hello World!")
	end,
}
This entity spawns properly, but if I try to use helper:helloWorld(), this causes a crash, because it hays helloWorld is a nil value. Any help would be appreciated.
User avatar
HaunterV
Posts: 676
Joined: Mon Apr 16, 2012 9:54 pm
Location: Barrie, Ontario, Canada

Re: Can you define a script_entity with functions?

Post by HaunterV »

intriguing concept... i think everyone just cuts n pastes from mod to mod at the moment... I think. Defined awesome entities is a novel concept that i hope plays out.
Grimrock Community 'FrankenDungeon 2012. Submit your entry now!: http://tinyurl.com/cnupr7h
SUBMIT YOUR ASSETS! Community Asset Pack (C.A.P.): http://tinyurl.com/bqvykrp
Behold! The HeroQuest Revival!: http://tinyurl.com/cu52ksc
3socks
Posts: 11
Joined: Sun Nov 04, 2012 11:29 pm

Re: Can you define a script_entity with functions?

Post by 3socks »

You will have to use some kind of entity with a hook (for example a door and onOpen hook) to spawn the script you want in this case a helloWorld function. Then you need to spawn this entity and call a function to activate the hook. In this hook you write code you want. However the code for that script has to be encapsulated in a string, because it will be processed by an eval function. And after the hook was triggered you can destroy the entity.

So it will all look like this:

Code: Select all

cloneObject{
   name = "HelloDoor",
   baseObject = "dungeon_door_metal",
   onOpen = function()
      local helloScript = [[
                            function helloWorld()
                                 print('Hello world')
                            end
                    ]]
      spawn("script_entity", 1,1,1,0,'helperFunctions')
      helperFunctions:setSource(helloScript)
   end
}
And then somewhere you spawn the entity and call open so it will trigger the hook:

Code: Select all

spawn("HelloDoor", 1,1,1,0, 'door')
door:open()
door:destroy()
Script entity will then be spawned and you can call the helloWorld function like this:

Code: Select all

helperFunctions:helloWorld()
Is that what you were describing you want to do?

EDIT:
Sorry for multiple edits, but this is the final version, I hope! :)
Last edited by 3socks on Thu Nov 08, 2012 12:39 am, edited 2 times in total.
User avatar
pferguso
Posts: 40
Joined: Tue Nov 06, 2012 6:09 pm

Re: Can you define a script_entity with functions?

Post by pferguso »

Thanks for the reply. I'll give that a shot and post the result. I can't get to it for a couple days though because I'm out of town. I miss my dungeon!!
User avatar
pferguso
Posts: 40
Joined: Tue Nov 06, 2012 6:09 pm

Re: Can you define a script_entity with functions?

Post by pferguso »

Ok, it still not working. I tried attaching it to a Monster onMove hook. Here is what I have:
SpoilerShow

Code: Select all

onMove = function(monster, dir)
		local helper = findEntity("helper")
		if (helper == null) then
			spawn("script_entity", monster.level, monster.x, monster.y, 0, "helper")
			local helper = findEntity("helper")
			local helloScript = [[
                		function helloWorld() 
					print('hello world')
				end
                	]]
			helper:setSource(helloScript)
			print('helper created.') --this works
		else	
			print('helper exists.')  --this works
			helper:helloWorld() --this DIES
		end
	end,
It looks like the script entity is getting spawned, and the helloScript is properly getting set as the source, but when I attempt to call the function, the program dies. The bad part is that because this script entity is not placed into the game by the editor, I don't get to see any error messages, it just crashes Grimrock. Perhaps the problem is in the syntax of the helloScript function itself. Any further assistance is greatly appreciated.
User avatar
Neikun
Posts: 2457
Joined: Thu Sep 13, 2012 1:06 pm
Location: New Brunswick, Canada
Contact:

Re: Can you define a script_entity with functions?

Post by Neikun »

What is the purpose of the double square brackets?
"I'm okay with being referred to as a goddess."
Community Model Request Thread
See what I'm working on right now: Neikun's Workshop
Lead Coordinator for Legends of the Northern Realms Project
  • Message me to join in!
User avatar
Xanathar
Posts: 629
Joined: Sun Apr 15, 2012 10:19 am
Location: Torino, Italy
Contact:

Re: Can you define a script_entity with functions?

Post by Xanathar »

The double square brackets say that everything inside them - up to the closing double square brackets - is a big string, whose new lines must be interpreted as new lines.

In pratice:

danteinferno = "Nel mezzo del cammin di nostra vita\nmi ritrovai per una selva oscura\nché la diritta via era smarrita."

is the same as

danteinferno = [[Nel mezzo del cammin di nostra vita
mi ritrovai per una selva oscura
ché la diritta via era smarrita.]]

See also: http://lua-users.org/wiki/StringsTutorial
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
JohnWordsworth
Posts: 1397
Joined: Fri Sep 14, 2012 4:19 pm
Location: Devon, United Kingdom
Contact:

Re: Can you define a script_entity with functions?

Post by JohnWordsworth »

Have been using Lua for about 6 months (even embedded into an iPhone app for a client), and I didn't realise about the [[multi-line string]]. Very interesting!
My Grimrock Projects Page with links to the Grimrock Model Toolkit, GrimFBX, Atlas Toolkit, QuickBar, NoteBook and the Oriental Weapons Pack.
User avatar
JKos
Posts: 464
Joined: Wed Sep 12, 2012 10:03 pm
Location: Finland
Contact:

Re: Can you define a script_entity with functions?

Post by JKos »

I think the problem is here: helper == null
lua doesn't have null but nil

I have used this method to dynamically spawn script entities on my scripting framework for about a month now and it works fine. But if you don't need dynamically spawned script entities you should not use it, it just makes your script more messy and harder to maintain.
- LoG Framework 2http://sites.google.com/site/jkoslog2 Define hooks in runtime by entity.name or entity.id + multiple hooks support.
- cloneObject viewtopic.php?f=22&t=8450
Post Reply