# Arrays (list button activate List props)

**URL:** <https://forums.unrealengine.com/t/arrays-list-button-activate-list-props/1690822>\
**Category:** Verse\
**Tags:** unreal-editor-for-fortnite, fortnite-creative, question, fortnite\
**Created:** [February 15, 2024, 4:06pm UTC](https://forums.unrealengine.com/t/arrays-list-button-activate-list-props/1690822 "2024-02-15T16:06:25Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mikamikaus](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/mikamikaus/32/880058_2.png) [@Mikamikaus](https://forums.unrealengine.com/u/Mikamikaus)\
**Post date:** [February 15, 2024, 4:06pm UTC](https://forums.unrealengine.com/t/arrays-list-button-activate-list-props/1690822/1 "2024-02-15T16:06:25Z")

</div>

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

```auto
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)

```

---

<div class="post-metadata">

**Author:** ![Starkiller9229](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Starkiller9229](https://forums.unrealengine.com/u/Starkiller9229)\
**Post date:** [February 15, 2024, 6:20pm UTC](https://forums.unrealengine.com/t/arrays-list-button-activate-list-props/1690822/2 "2024-02-15T18:20:16Z")

</div>

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](https://dev.epicgames.com/community/snippets/d8k/fortnite-wrapping-subscribe-to-pass-additional-data-to-listeners)

This will allow you to change your OnButton function to

```auto
OnButton(Agent : agent, ButtonIndex : int)

```

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

```auto
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 🙂

---

<div class="post-metadata">

**Author:** ![Mikamikaus](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/mikamikaus/32/880058_2.png) [@Mikamikaus](https://forums.unrealengine.com/u/Mikamikaus)\
**Post date:** [February 26, 2024, 3:30pm UTC](https://forums.unrealengine.com/t/arrays-list-button-activate-list-props/1690822/3 "2024-02-26T15:30:45Z")

</div>

Hi @Starkiller9229  
Thank you for your help

I changed the code to

```auto
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:

```auto
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)

```

---

<div class="post-metadata">

**Author:** ![Starkiller9229](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Starkiller9229](https://forums.unrealengine.com/u/Starkiller9229)\
**Post date:** [February 26, 2024, 5:22pm UTC](https://forums.unrealengine.com/t/arrays-list-button-activate-list-props/1690822/4 "2024-02-26T17:22:44Z")

</div>

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](https://www.youtube.com/watch?v=v30hvvCVTCs)

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:

```auto
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.

---

<div class="post-metadata">

**Author:** ![Mikamikaus](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/mikamikaus/32/880058_2.png) [@Mikamikaus](https://forums.unrealengine.com/u/Mikamikaus)\
**Post date:** [February 27, 2024, 12:28pm UTC](https://forums.unrealengine.com/t/arrays-list-button-activate-list-props/1690822/5 "2024-02-27T12:28:23Z")

</div>

Thank you for your help @Starkiller9229  
worked perfectly 🎉

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

```auto
 @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?

---

<div class="post-metadata">

**Author:** ![Starkiller9229](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Starkiller9229](https://forums.unrealengine.com/u/Starkiller9229)\
**Post date:** [February 28, 2024, 6:34am UTC](https://forums.unrealengine.com/t/arrays-list-button-activate-list-props/1690822/6 "2024-02-28T06:34:32Z")

</div>

I’m glad you got it working 🙂

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

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

```

and do the same for the other one

---

<div class="post-metadata">

**Author:** ![Mikamikaus](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/mikamikaus/32/880058_2.png) [@Mikamikaus](https://forums.unrealengine.com/u/Mikamikaus)\
**Post date:** [February 28, 2024, 12:23pm UTC](https://forums.unrealengine.com/t/arrays-list-button-activate-list-props/1690822/7 "2024-02-28T12:23:15Z")

</div>

you are the best  
Thank you for your help

Apparently then I always have to put it inside the if

---

<div class="post-metadata">

**Author:** ![system](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/1/3/713c9d3f58553f0de89543d76a8a3a2779dc9fa4.png) [@system](https://forums.unrealengine.com/u/system)\
**Post date:** [March 29, 2024, 12:23pm UTC](https://forums.unrealengine.com/t/arrays-list-button-activate-list-props/1690822/8 "2024-03-29T12:23:18Z")

</div>

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