Page 1 of 2

Storing data to entities and other scripting tricks

Posted: Wed Nov 05, 2014 7:36 pm
by JKos
Ok, would it be nice if you could store entity specific data directly to the entity? Like:
crab_1.data:set('is_boiled',true)
and access that data like this:
crab_1.data:get('is_boiled')

Well now you can, just add this component definition at the beginning of your init.lua

Code: Select all

userData = {
	class = "Script",
	name = "data",
	source = [[
data = {}
function get(self,name)
	return self.data[name]
end
function set(self,name,value)
	self.data[name] = value
end
]],		
}
And if you like to store data to crab-entity you can do this

Code: Select all

defineObject{
   name = "crab",
   baseObject = "crab",
   components = {
		userData
   }
}
Tested and it is save game compatible.

Re: Storing data to entities

Posted: Wed Nov 05, 2014 7:40 pm
by NutJob
JKos wrote:Ok, would it be nice if you could store entity specific data directly to the entity?
Why, yes I do.
JKos wrote: Like:
crab_1.data:set('is_boiled',true)
and access that data like this:
crab_1.data:get('is_boiled')

Well now you can, just add this component definition at the beginning of your init.lua

Code: Select all

local userData = {
	class = "Script",
	name = "data",
	source = [[
data = {}
function get(self,name)
	return self.data[name]
end
function set(self,name,value)
	self.data[name] = value
end
]],		
}
And if you like to store data to crab-entity you can do this

Code: Select all

defineObject{
   name = "crab",
   baseObject = "crab",
   components = {
		userData
   }
}
Tested and it is save game compatible.
Please stop making me do full overhauls of my module. ~laughs~

Thank you, for, yet another, fantastic way of staying organized.

Re: Storing data to entities

Posted: Wed Nov 05, 2014 7:56 pm
by cromcrom
Thanks Jkos, amazing stuff. I am glad I haven't started serious work yet ^^

Re: Storing data to entities

Posted: Wed Nov 05, 2014 8:54 pm
by alois
That's very nice!

Just a remark, though: the "defineObject" has to be in the same init.lua file, after the various "import" (if you place the 'new' crab into monsters lua, it will not work because "userData" is local).

Alois :)

Re: Storing data to entities and other scripting tricks

Posted: Thu Nov 06, 2014 11:09 pm
by JKos
I didn't want to make own tread for this so I renamed the thread and post it in here.

Autotrigger-script
This entity is a combination of floortrigger and script entity, and it will automatically execute its own execute-method when the floortrigger is activated.
Just add this to your init.lua

Code: Select all

defineObject{
	name = 'script_autotrigger',
	baseObject = 'script_entity',
    components = {
		{
			class = 'FloorTrigger'
		},
		{
			class = "Script",
			source = [[
function execute(trigger)
	-- your code here
end
self.go.floortrigger:addConnector('onActivate',self.go.id,'execute')
			]]
		}
    }	
}
then place script_autotrigger entity to your dungeon and type your code inside of its execute method (in editor, don't modify the definition)
This just saves a lots of time since you don't have to add connectors and script entities manually. And this same trick can be used with anything basically, for example buttons, levers or torch_holders.

(I got this idea from Xanathar's GrimQ library for LoG1, sorry for copying your ideas if you happen to read this :) . I loved those autoexec-scripts)

Re: Storing data to entities and other scripting tricks

Posted: Fri Nov 07, 2014 7:40 am
by akroma222
Thanks jKos! These will be very handy indeed :)

Re: Storing data to entities and other scripting tricks

Posted: Sat Nov 08, 2014 2:21 pm
by Grimfan
Now this is a thing of beauty! :D

Re: Storing data to entities and other scripting tricks

Posted: Wed Nov 26, 2014 8:24 am
by cromcrom
Has anybody been able to createComponent this userData, please ?
I tryed all manners of capital sensitive userdata, to no avail.
so

Code: Select all

spawnthing:createComponent("userData")
returns an error.

I copied the original script at the top of my init.lua file.

Any help please ?

Re: Storing data to entities and other scripting tricks

Posted: Wed Nov 26, 2014 8:40 am
by minmay
createComponent takes two arguments. The first is the class of the component, the second is the name. For example, to create a SoundComponent named "steve", you'd use object:createComponent("Sound","steve").
userData is a table that acts as a component definition, you cannot add it to an object at runtime, you can only use it in an object's original definition. To add a component to an object at runtime you have to use createComponent then set all the fields one at a time, although of course you could write a script to automate this.

Re: Storing data to entities and other scripting tricks

Posted: Wed Nov 26, 2014 12:36 pm
by cromcrom
Thanks a lot for the answer, minmay. This is confusing, because I was able to add a "usableItem" component to a branch, using only

Code: Select all

spawnthing:createComponent("usableItem")
, so the confusion.

I am sorry, what you say is still confusing to me:
userData is a table that acts as a component definition, you cannot add it to an object at runtime, you can only use it in an object's original definition. To add a component to an object at runtime you have to use createComponent then set all the fields one at a time, although of course you could write a script to automate this.
So, can I add it at runtime, if I set all the fields one at a time, as a component, or I can't, because it is a table ?