Menu
Lumberyard
User Guide (Version 1.13)

Example Script

The following is a script sample that is intended to be placed on an entity with a constraint component of Magnet type, with its owner set as Other entity. This script alternates between three different entities to be constrained for Transition Interval seconds.

local constraintexample = { Properties = { ConstrainedEntity1 = EntityId(), ConstrainedEntity2 = EntityId(), ConstrainedEntity3 = EntityId(), TransitionInterval = 3, }, } function constraintexample:OnActivate() self.TransitionCountDown = self.Properties.TransitionInterval self.ConstrainedIdx = 1 self.ConstrainedEntities = { self.Properties.ConstrainedEntity1, self.Properties.ConstrainedEntity2, self.Properties.ConstrainedEntity3 } self.tickBusHandler = TickBus.Connect(self) self.constraintHandler = ConstraintComponentNotificationBus.Connect(self, self.entityId) Debug.Log("ConstraintComponent activated for entity: " .. tostring(self.entityId)) end function constraintexample:OnTick(deltaTime, timePoint) self.TransitionCountDown = self.TransitionCountDown - deltaTime if (self.TransitionCountDown < 0.0) then ConstraintComponentRequestBus.Event.SetConstraintEntities(self.entityId, self.ConstrainedEntities[self.ConstrainedIdx], self.entityId) self.ConstrainedIdx = ((self.ConstrainedIdx + 1) % table.getn(self.ConstrainedEntities)) + 1 self.TransitionCountDown = self.Properties.TransitionInterval end end function constraintexample:OnDeactivate() self.tickBusHandler:Disconnect() self.constraintHandler:Disconnect() end function constraintexample:OnConstraintEntitiesChanged(oldOwner, oldTarget, newOwner, newTarget) Debug.Log("Constraint Changed - old owner:" .. tostring(oldOwner) .. " old target:" .. tostring(oldTarget)) Debug.Log(" new owner:" .. tostring(newOwner) .. " new target:" .. tostring(newTarget)) end function constraintexample:OnConstraintEnabled() Debug.Log("Constraint Enabled: " .. tostring(self.entityId)) end function constraintexample:OnConstraintDisabled() Debug.Log("Constraint Disabled: " .. tostring(self.entityId)) end return constraintexample