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
]])
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
]])
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)
Code: Select all
local addUserDataToThese = {'dagger','herder','ratling1','etc...'}
for _,entname in ipairs(addUserDataToThese) do
cloneObject{
name=entname,
components = {
userData
}
}
end
How would I call these functions using delayedCall?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')
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.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:Error code is "attempt to call method set (a nil value)"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)
My progress reading this thread:(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)
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')
]]
}
}
}