I wanted a list of buttons and a list of props
When active, the button wanted this button to activate a function related to this prop that is in the same index
example, when I press the index 2 button, it would activate the rotation of the index 2 prop
I already have the function for just 1 button and 1 prop but I wanted it for lists because I’m going to use several
If you can help me I would appreciate it
item_rotator := class(creative_device):
@editable var Time : float = 3.0
@editable var Item : creative_prop = creative_prop{}
@editable var ItemList : []creative_prop = array{}
@editable ActiveButton : button_device = button_device {}
@editable ButtonList : []button_device = array{}
# Runs when the device is started in a running game
OnBegin<override>()<suspends>:void=
ActiveButton.InteractedWithEvent.Subscribe(OnButton)
OnButton(Agent: agent) : void=
spawn:
SmoothRotate(Item)
SmoothRotate<private>(Prop: creative_prop)<suspends> : void=
Transform := Prop.GetTransform()
Position := Transform.Translation
Rotation := Transform.Rotation
NewRotation := Rotation.ApplyYaw(180.0)
MoveResult := Prop.MoveTo(Position, NewRotation, Time)
#suspende por 3 segundos
if (MoveResult = move_to_result.DestinationReached):
SmoothRotate(Prop)
You will need to pass the index of the pressed button to the OnButton and SmoothRotate functions. To do this I would recommend taking a look at this code snippet:
Unknown member SubscribeAgent in listenable(agent).
This invocation calls a function that has the ‘decides’ effect, which is not allowed by its context. The ‘decides’ effect indicates that the invocation calls a function that might fail, and so must occur in a failure context that will handle the failure. Some examples of failure contexts are the condition clause of an ‘if’, the left operand of ‘or’, or the clause of the ‘logic’ macro.
spawn:
SmoothRotate(Prop)
i get error:
Unknown member SubscribeAgent in listenable(agent).
All Code:
itens_rotator := class(creative_device):
@editable var Time : float = 3.0
@editable var ItemList : []creative_prop = array{}
@editable var ButtonList : []button_device = array{}
# Runs when the device is started in a running game
OnBegin<override>()<suspends>:void=
for (Index -> Button : ButtonList):
Button.InteractedWithEvent.SubscribeAgent(OnButton, Index)
OnButton(Agent : agent, ButtonIndex : int) : void=
Prop := ItemList[ButtonIndex]
spawn:
SmoothRotate(Prop)
SmoothRotate<private>(Prop: creative_prop)<suspends> : void=
Transform := Prop.GetTransform()
Position := Transform.Translation
Rotation := Transform.Rotation
NewRotation := Rotation.ApplyYaw(180.0)
MoveResult := Prop.MoveTo(Position, NewRotation, Time)
#suspende por 3 segundos
if (MoveResult = move_to_result.DestinationReached):
SmoothRotate(Prop)
Create a new class called utils.verse (any name will work) and then paste the code from the snippet in it, then you will be able to access the SubscribeAgent function. Graeme Bull does a good job of explaining the use of the code snippet in this video
As for the function that has the decides effect, I believe it is the Prop := ItemList[ButtonIndex] so you should wrap it in an if:
if (Prop := ItemList[ButtonIndex]):
spawn:
SmoothRotate(Prop)
I hope this gets it working Good luck
Some extra info on accessing elements in an array:
Anytime we access an element from an array we have to wrap it in an if since there is a chance that the element is not found, in cases such as when the index is less than 0 or greater than the length of the array.