Page 2 of 2
Re: Storing data to entities and other scripting tricks
Posted: Wed Nov 26, 2014 4:13 pm
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.
Re: Storing data to entities and other scripting tricks
Posted: Wed Nov 26, 2014 9:53 pm
by cromcrom
Thanks a lot JKos, I will pretty soon.
Re: Storing data to entities and other scripting tricks
Posted: Thu Nov 27, 2014 7:54 pm
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)"
Re: Storing data to entities and other scripting tricks
Posted: Thu Nov 27, 2014 10:13 pm
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
Re: Storing data to entities and other scripting tricks
Posted: Thu Nov 27, 2014 10:19 pm
by cromcrom
thanks JKos. I think I will humbly use tables for some times

Re: Storing data to entities and other scripting tricks
Posted: Thu Apr 23, 2015 9:05 pm
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?
Re: Storing data to entities and other scripting tricks
Posted: Fri Apr 24, 2015 1:32 am
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.
Re: Storing data to entities and other scripting tricks
Posted: Mon May 11, 2015 10:58 am
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! 

Re: Storing data to entities and other scripting tricks
Posted: Thu May 21, 2015 4:44 am
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')
]]
}
}
}