Arrays (list button activate List props)

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)
1 Like

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:

fortnite-wrapping-subscribe-to-pass-additional-data-to-listeners

This will allow you to change your OnButton function to

OnButton(Agent : agent, ButtonIndex : int)

and then you can subscribe to the InteractedWithEvent for each button in the following way

for (Index -> Button : ButtonList):
    Button.InteractedWithEvent.SubscribeAgent(OnButton, Index)

Now you can use that index in OnButton to get the Prop and then call SmoothRotate with that prop.

I hope this helps, Good luck :slight_smile:

3 Likes

Hi @Starkiller9229
Thank you for your help

I changed the code to

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)

Then I got 2 errors:

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)
1 Like

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 :slight_smile: 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.

2 Likes

Thank you for your help @Starkiller9229
worked perfectly :tada:

Now I’m trying to do it with a logical array but I don’t know why I can’t change its value in the same index

 @editable var RotateList : []logic = array{}

     OnButton(Agent : agent, ButtonIndex : int) : void=
         if (RotateList[ButtonIndex]):
             if (Prop := ItemList[ButtonIndex]):
                 spawn:
                     SmoothRotate(Prop)
             set RotateList[ButtonIndex] = false
         else:
             set RotateList[ButtonIndex] = true

How can I change the value of a logical array?

1 Like

I’m glad you got it working :slight_smile:

As for changing the array, wrap the assignment in an if:

if (set RotateList[ButtonIndex] = false){}

and do the same for the other one

2 Likes

you are the best
Thank you for your help

Apparently then I always have to put it inside the if

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.