can i spawn script_entity?

Talk about creating Grimrock 1 levels and mods here. Warning: forum contains spoilers!
User avatar
Crisim
Posts: 16
Joined: Wed Aug 29, 2012 2:28 pm

can i spawn script_entity?

Post by Crisim »

I have a lever connected to script_entity with this code

Code: Select all

function hiAll ()
  	local test = findEntity("script_entity_2")
  	hudPrint("Hi Adventurers")
  	if test == nil then
  	  	test = spawn("script_entity",1,13,15,0,"script_entity_2")
  	  	test:setSource("function newHiAll()\nhudPrint(\"Crisim rules!\")\nend")
  	  	test.newHiAll() --exception "attemp to call field 'newHiAll' (a nil value)
  	end
end
is this possible ? it exists only in my buggy mind ?
Lilltiger
Posts: 95
Joined: Sun Sep 16, 2012 1:12 am

Re: can i spawn script_entity?

Post by Lilltiger »

I don't think a spawned script like that will get parsed by the lua parser correctly.
But why would you want to do that?
User avatar
djoldgames
Posts: 107
Joined: Fri Mar 23, 2012 11:28 pm
Contact:

Re: can i spawn script_entity?

Post by djoldgames »

Crisim wrote:is this possible ? it exists only in my buggy mind ?
Yes, spawn scripts with source is possible, but I don't know why your code throw exception :?

In my mod I use the framework scripts, complete generated "on the fly". It is idea from JKos in Advanced scripting: a scripting framework thread.

For example I created a new project with this code in "init.lua"

Code: Select all

cloneObject{
	name = "MyScripts",
	baseObject = "dungeon_door_metal",
	onOpen = function()
		local MyFunction = [[
		function Activate()
			hudPrint("Testing!")
		end
		print("MyScript Activated...")
		]]
		spawn("script_entity", 1,1,1,0,'MyScript')
		MyScript:setSource(MyFunction)
	end
}
and this code in script_entity

Code: Select all

script = spawn("MyScripts", 1,1,1,0,'scripts_Init')
scripts_Init:open()
scripts_Init:destroy()

function Test()
	MyScript.Activate()
end
This by using cloned door works beautiful...
Last edited by djoldgames on Tue Oct 02, 2012 9:19 pm, edited 1 time in total.
User avatar
Crisim
Posts: 16
Joined: Wed Aug 29, 2012 2:28 pm

Re: can i spawn script_entity?

Post by Crisim »

if i write this in a script entity (not in a function)

Code: Select all

spawn("script_entity",1,13,15,0,"myTinyScript")
myTinyScript:setSource("function newHiAll()\nhudPrint(\"Crisim rules!\")\nend")
then this attached to a lever event

Code: Select all

function activateLever()
   myTinyScript.newHiAll()
end
works correctly, but i need to dynamically set the body of the script_entity (well, i 've achieved what i need with a workaround, but this was the preferred option).
is this possible?
User avatar
petri
Posts: 1917
Joined: Thu Mar 01, 2012 4:58 pm
Location: Finland

Re: can i spawn script_entity?

Post by petri »

It is recommended that you don't use undocumented functions. They could be changed or removed in the future.

It is also likely that this could be accomplished more efficiently/cleaner without creating scripts on the fly. Could you describe why you need to set script's source from another script, please?
User avatar
Crisim
Posts: 16
Joined: Wed Aug 29, 2012 2:28 pm

Re: can i spawn script_entity?

Post by Crisim »

Well, i achieved the desired result using static functions, if statements and a bunch of script variables.

IMO spawning a custom script_entity and destroying that once it finished its work would have been a cleaner approach.
User avatar
JKos
Posts: 464
Joined: Wed Sep 12, 2012 10:03 pm
Location: Finland
Contact:

Re: can i spawn script_entity?

Post by JKos »

Hi Petri,

Please don't remove script_entity:setSource() because as far as I know it's the only way to write redistributable/reusable scripts-libraries for LoG.
My framework for example can be attached to any existing project by just including the framework.lua to init.lua and calling some hook-function which creates script entities on the fly.
We can't access any variables or functions defined in lua-files from script entities, so I think it's the only way. Or is there some other way that I'm not aware of (besides of copy pasting all script entities one by one)? I would like to know if there is :)
- 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
User avatar
petri
Posts: 1917
Joined: Thu Mar 01, 2012 4:58 pm
Location: Finland

Re: can i spawn script_entity?

Post by petri »

JKos wrote:Hi Petri,

Please don't remove script_entity:setSource() because as far as I know it's the only way to write redistributable/reusable scripts-libraries for LoG.
My framework for example can be attached to any existing project by just including the framework.lua to init.lua and calling some hook-function which creates script entities on the fly.
We can't access any variables or functions defined in lua-files from script entities, so I think it's the only way. Or is there some other way that I'm not aware of (besides of copy pasting all script entities one by one)? I would like to know if there is :)
Could you show a simple example how your framework is structured?

EDIT: what's wrong with just copy-pasting the whole framework into a script entity?
User avatar
JKos
Posts: 464
Joined: Wed Sep 12, 2012 10:03 pm
Location: Finland
Contact:

Re: can i spawn script_entity?

Post by JKos »

Sure: here is a simple a example. Added --snap-- comment to where I cut the code, but the general idea is like this:

framework.lua

Code: Select all

framework.data = [[
registry = {}

function set(entity,key,value)
	if not data.registry[entity.id] then data.addEntity(entity) end
	data.registry[entity.id][key] = value
end

function removeEntity(entity)
	data.registry[entity.id] = nil
end
 -- snap --
]]
-- snap --
-- this door loads script entities
cloneObject{
	name = "LoGFramework",
	baseObject = "dungeon_door_metal",
	onOpen = function()
		-- load framework sources to script entities
		spawn("script_entity", 1,1,1,0,'data')
		-- snap --
		fw:setSource(framework.data)	
		-- load modules
		for moduleName,source in pairs(modules) do
			if (logfw_init.options.modules[moduleName]) then
				spawn("script_entity", 1,1,1,0,moduleName)
				script = findEntity(moduleName)
				script:setSource(source)
			end
		end
	end,
}


create script entity named logfw_init in your dungeon
and put this code to it:

Code: Select all

options = {}
-- enable some modules
options.modules = {
   monsters_can_open_doors = false,
   damage_dealing_doors = true,
   illusion_walls = true,
}
spawn("LoGFramework", 1,1,1,0,'fwInit')
fwInit:open() -- this loads all script entities defined in framework.lua
fwInit:destroy()
If I put all code to single script-entity it would be a mess and pretty difficult to maintain in ingame editor, I have now about 500 lines of code in framwork.lua. And I like that I am able to develop new features in editor and then just copy-paste it to framework.lua when it's done, and it isn't showing in editor anymore. And if framework.lua grows too big, I can make another lua-file which adds script_entities to modules-table and include it to framwork.lua.
- 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
Lilltiger
Posts: 95
Joined: Sun Sep 16, 2012 1:12 am

Re: can i spawn script_entity?

Post by Lilltiger »

A better way would be to allow for parsing external scripts as if they where script_entities, so one just att it to the init.lua and dosent need to spawn script instances.
Post Reply