Page 1 of 1

Can you define a script_entity with functions?

Posted: Wed Nov 07, 2012 11:14 pm
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.

Re: Can you define a script_entity with functions?

Posted: Wed Nov 07, 2012 11:24 pm
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.

Re: Can you define a script_entity with functions?

Posted: Wed Nov 07, 2012 11:46 pm
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! :)

Re: Can you define a script_entity with functions?

Posted: Thu Nov 08, 2012 7:25 pm
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!!

Re: Can you define a script_entity with functions?

Posted: Sun Nov 11, 2012 9:53 pm
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.

Re: Can you define a script_entity with functions?

Posted: Sun Nov 11, 2012 10:03 pm
by Neikun
What is the purpose of the double square brackets?

Re: Can you define a script_entity with functions?

Posted: Sun Nov 11, 2012 10:07 pm
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

Re: Can you define a script_entity with functions?

Posted: Sun Nov 11, 2012 10:14 pm
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!

Re: Can you define a script_entity with functions?

Posted: Mon Nov 12, 2012 12:27 pm
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.