Storing data to entities and other scripting tricks

Ask for help about creating mods and scripts for Grimrock 2 or share your tips, scripts, tools and assets with other modders here. Warning: forum contains spoilers!
User avatar
JKos
Posts: 464
Joined: Wed Sep 12, 2012 10:03 pm
Location: Finland
Contact:

Storing data to entities and other scripting tricks

Post 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.
Last edited by JKos on Thu Nov 27, 2014 10:17 pm, edited 2 times in total.
- 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
NutJob
Posts: 426
Joined: Sun Oct 19, 2014 6:35 pm

Re: Storing data to entities

Post 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.
User avatar
cromcrom
Posts: 549
Joined: Tue Sep 11, 2012 7:16 am
Location: Chateauroux in a socialist s#!$*&% formerly known as "France"

Re: Storing data to entities

Post by cromcrom »

Thanks Jkos, amazing stuff. I am glad I haven't started serious work yet ^^
A trip of a thousand leagues starts with a step.
alois
Posts: 112
Joined: Mon Feb 18, 2013 7:29 am

Re: Storing data to entities

Post 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 :)
User avatar
JKos
Posts: 464
Joined: Wed Sep 12, 2012 10:03 pm
Location: Finland
Contact:

Re: Storing data to entities and other scripting tricks

Post 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)
- 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
akroma222
Posts: 1029
Joined: Thu Oct 04, 2012 10:08 am

Re: Storing data to entities and other scripting tricks

Post by akroma222 »

Thanks jKos! These will be very handy indeed :)
Grimfan
Posts: 369
Joined: Wed Jan 02, 2013 7:48 am

Re: Storing data to entities and other scripting tricks

Post by Grimfan »

Now this is a thing of beauty! :D
User avatar
cromcrom
Posts: 549
Joined: Tue Sep 11, 2012 7:16 am
Location: Chateauroux in a socialist s#!$*&% formerly known as "France"

Re: Storing data to entities and other scripting tricks

Post 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 ?
A trip of a thousand leagues starts with a step.
minmay
Posts: 2790
Joined: Mon Sep 23, 2013 2:24 am

Re: Storing data to entities and other scripting tricks

Post 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.
Grimrock 1 dungeon
Grimrock 2 resources
I no longer answer scripting questions in private messages. Please ask in a forum topic or this Discord server.
User avatar
cromcrom
Posts: 549
Joined: Tue Sep 11, 2012 7:16 am
Location: Chateauroux in a socialist s#!$*&% formerly known as "France"

Re: Storing data to entities and other scripting tricks

Post 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 ?
A trip of a thousand leagues starts with a step.
Post Reply