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:

Re: Storing data to entities and other scripting tricks

Post by JKos »

We cannot define new real components(Classes) and userdata is actually ScriptComponent so if you want to do that dynamically you have to do something like this.

Code: Select all

entity:createComponent('Script','data')
entity.data:setSource([[
data = {}
function get(self,name)
   return self.data[name]
end
function set(self,name,value)
   self.data[name] = value
end
]])
NOT tested so please tell if this works.
- 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
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 JKos, I will pretty soon.
A trip of a thousand leagues starts with a step.
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 »

Sorry, but with theses info, I can't even add userData as a component on pre created object (not at runtime, I still have to check)

At runTime, here is how I use your script:
I have a function that create item, and give it/changes various components.

then at some point I have this:

Code: Select all

spawnthing:createComponent('Script','data')
spawnthing.data:setSource([[
data = {}
function get(self,name)
   return self.data[name]
end
function set(self,name,value)
   self.data[name] = value
end
]])

local deteriorationValue = 10+(math.random(1,15))
spawnthing.data:set("maxDeterioration", deteriorationValue)
spawnthing.data:set("Deterioration", deteriorationValue)
Error code is "attempt to call method set (a nil value)"
A trip of a thousand leagues starts with a step.
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 tested it too and it really doesn't seem to work, which is a bit strange because with script_entity setSource and loadFile works. But anyway, if you are using cloneObject you can easily add those data-components to desired objects in init.lua.

cloneObject:
viewtopic.php?f=22&t=8450

then in init.lua

Code: Select all

local addUserDataToThese = {'dagger','herder','ratling1','etc...'}
for _,entname in ipairs(addUserDataToThese) do
  cloneObject{
      name=entname,
      components = {
           userData
      }
   }
end

- 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
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 JKos. I think I will humbly use tables for some times :-)
A trip of a thousand leagues starts with a step.
User avatar
Zo Kath Ra
Posts: 940
Joined: Sat Apr 21, 2012 9:57 am
Location: Germany

Re: Storing data to entities and other scripting tricks

Post by Zo Kath Ra »

JKos wrote: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')
How would I call these functions using delayedCall?
minmay
Posts: 2790
Joined: Mon Sep 23, 2013 2:24 am

Re: Storing data to entities and other scripting tricks

Post by minmay »

cromcrom wrote:Sorry, but with theses info, I can't even add userData as a component on pre created object (not at runtime, I still have to check)

At runTime, here is how I use your script:
I have a function that create item, and give it/changes various components.

then at some point I have this:

Code: Select all

spawnthing:createComponent('Script','data')
spawnthing.data:setSource([[
data = {}
function get(self,name)
   return self.data[name]
end
function set(self,name,value)
   self.data[name] = value
end
]])

local deteriorationValue = 10+(math.random(1,15))
spawnthing.data:set("maxDeterioration", deteriorationValue)
spawnthing.data:set("Deterioration", deteriorationValue)
Error code is "attempt to call method set (a nil value)"
When you call setSource or loadFile, the passed string doesn't get executed immediately. So the component doesn't have the "get" and "set" fields yet in your example, because the script entity source hasn't been run yet. In this example it might never run at all, I haven't actually been able to figure out when it does, but I'm pretty sure the setSource or loadFile call has to be before the ScriptComponent actually initializes.
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
Xanathar
Posts: 629
Joined: Sun Apr 15, 2012 10:19 am
Location: Torino, Italy
Contact:

Re: Storing data to entities and other scripting tricks

Post by Xanathar »

(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)
My progress reading this thread:
  • Wow that's a smart way to embed scripts in other entities
  • Auto objects!! That's what I need in my Orrr3 room !
  • Wooooh thanks mate! ;) You're welcome! :)
:D
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
Eleven Warrior
Posts: 752
Joined: Thu Apr 18, 2013 2:32 pm
Location: Australia

Re: Storing data to entities and other scripting tricks

Post by Eleven Warrior »

OMG This Post is so far the best AUTO everything :) This is saving a lot of extra objects on the field eg: floor_trigger connected to script entity.... Now I just use the script entity no more floor_triggers yeeeeeeeeeeeeeeeeeeeee THXS SO MUCH JKOS Love you bro :)

EDIT: How do I add this to Lever, wall button and torch I tried and I cannot get it to work lol

EDIT2: Ok I think I got it. I hope this is right:

Code: Select all

defineObject{
   name = 'script_autotrigger_button',
   baseObject = 'script_entity',
    components = {
	{
	class = 'Button',
	sound = 'button',
	},
	{
	class = "Model",
	model = "assets/models/env/wall_button.fbx",
	},

	{
	class = "Animation",
	animations = {
	press = "assets/animations/env/wall_button_press.fbx",
	}
	},
	{
	class = "Clickable",
	offset = vec(0,1.375,0),
	size = vec(0.25, 0.25, 0.25),
	--debugDraw = true,
	},
	{
	class = "Script",
	source = [[
function execute(trigger)
   -- your code here
end
self.go.button:addConnector('onActivate',self.go.id,'execute')
         ]]
      }
    }
}
Post Reply