vanblam wrote: ↑Fri May 17, 2019 6:58 amYea I see what you mean, the method would have to be universal for on or off, hence toggle. So there is no way( with out doing it in the editor) to have it switch once on a delay, then activate its target without breaking the mod/save.
Oh, I see. You can do this, but it is substantially more of a pain. I did something very similar in the Dungeon Master Resource conversion:
Code: Select all
-- NOTE: This object has to spawn an extra object to work, which is generally
-- invisible to both the player and designer. However, it means that extra care
-- is required if you wish to destroy this object.
defineObject{
name = "dm_conduit_wheel",
components = {
{
class = "Lever",
sound = "dm_silence",
onInit = function(self)
spawn("dm_conduit_wheel_helper",self.go.level,self.go.x,self.go.y,self.go.facing,self.go.elevation,"wh"..self.go.id)
end,
},
{
class = "Controller",
onActivate = function(self)
if self.go.lever:isDeactivated() then
self.go.lever:toggle()
end
end,
onDeactivate = function(self)
if self.go.lever:isActivated() then
self.go.lever:toggle()
end
end,
onToggle = function(self)
self.go.lever:toggle()
end,
onInitialActivate = function(self)
local helper = findEntity("wh"..self.go.id)
if helper then
helper.animation:play("activate")
else
Console.warn("no wheel helper found for "..self.go.id)
end
end,
onInitialDeactivate = function(self)
end,
},
{
class = "Model",
model = "mod_assets/dmcsb_pack/models/nil.fbx",
enabled = false,
},
{
class = "Animation",
animations = {
activate = "mod_assets/dmcsb_pack/animations/nil.fbx",
deactivate = "mod_assets/dmcsb_pack/animations/nil.fbx",
},
enabled = false,
},
},
placement = "wall",
editorIcon = 12,
tags = {"dm","dm_user"},
}
defineObject{
name = "dm_conduit_wheel_helper",
placement = "wall",
components = {
{ -- dummy for lever animation
class = "Model",
model = "mod_assets/dmcsb_pack/models/env/dm_lever_wheel.fbx",
},
{
class = "Animation",
animations = {
activate = "mod_assets/dmcsb_pack/animations/env/dm_wheel_counterclockwise.fbx",
deactivate = "mod_assets/dmcsb_pack/animations/env/dm_wheel_clockwise.fbx",
},
onAnimationEvent = function(self, event)
if event:find("dm_wheel_end") then
self.go.clickable:enable()
elseif event:find("dm_wheel_start") then
self.go.clickable:disable()
elseif event:find("dm_wheel_on") then
local main = findEntity(self.go.id:sub(3))
if main then
main.controller:activate()
else
Console.warn("no lever found for wheel "..self.go.id)
end
elseif event:find("dm_wheel_off") then
local main = findEntity(self.go.id:sub(3))
if main then
main.controller:deactivate()
else
Console.warn("no parent conduit found for wheel helper "..self.go.id)
end
end
end,
},
{
class = "Clickable",
offset = vec(0,1.37774,0),
size = vec(0.8,0.8,0.2),
onClick = function(self)
local main = findEntity(self.go.id:sub(3))
if main then
if main.lever:isActivated() then
self.go.animation:play("deactivate")
else
self.go.animation:play("activate")
end
else
Console.warn("no parent conduit found for wheel helper "..self.go.id)
end
end,
},
},
}
It works as you'd expect in the editor, you only have to place the dm_conduit_wheel object, connectors work like a regular lever but with the delay, etc. But the implementation under the hood is quite nasty.
In retrospect, I didn't actually need to use this two-object approach; it could all be done in one object if I did more manual animation management.
The big issue is, if there's a ClickableComponent and a LeverComponent on the same object, clicking the ClickableComponent is going to toggle the LeverComponent and play its animation, even if the LeverComponent is disabled. But I think you should be able to prevent that by putting a dummy component that does something on clicks (like ButtonComponent or another LeverComponent) before the LeverComponent, and that dummy component will "catch" the click before the lever does, and then you can do all the lever manipulation with the ClickableComponent's onClick hook and animation events, like in the object above.