How to set "trigger device" type Array's element into new array.

Hi,

Let me question how to set “trigger device” type Array’s element into new array.
Regarding to the official document,
Array | Epic Developer Community (epicgames.com)

I implemented the following verse code.
However, I confirm on Fortnite, the sentence “if(set “NewArray”[TriggerArrayIndex] = TriggeredCard):” fails to be executed. What is the cause of this problem?

Regards,

using { /Fortnite.com/Devices }
using { /Verse.org/Simulation }
using { /UnrealEngine.com/Temporary/Diagnostics }
using { /UnrealEngine.com/Temporary/SpatialMath }
using { /Verse.org/Random }
using { /Verse.org/Verse }
using { /UnrealEngine.com/Temporary/UI }
using { /Fortnite.com/UI }
using { /Verse.org/Colors }
using { /Verse.org/Simulation/Tags }
using { /Fortnite.com/Playspaces }
using { /Fortnite.com/Characters }
using { /UnrealEngine.com/Temporary/SpatialMath }

prop_teleport := class(creative_device):

    @editable
    TriggerCard : []trigger_device = array{}

    OnBegin<override>()<suspends>:void=
    	# Prepare New Arrays
        var SelectedCards : []trigger_device = array{}
	    var NonSelectedCards : []trigger_device = array{}
	    
	    # Set the original array element into new ones
	    var TriggerArrayIndex : int = 0
	    for(Index -> TriggeredCard : TriggerCard):
	        if(ToString(Index) = CardNo):
	            Print("Test")
	            # This sentence fails to be executed
	            if(set SelectedCards[TriggerArrayIndex] = TriggeredCard):
	                Print("Selected!")
	                set TriggerArrayIndex += 1
	        else:
	            Print("Test2")
	            # This sentence fails to be executed
	            if(set NonSelectedCards[TriggerArrayIndex] = TriggeredCard):
	                Print("Out!")

I believe you are just trying to add new elements to the array, right? In that case you should be doing set SelectedCards += array{TriggeredCard}.
The code you are trying to execute currently is searching for the index 0 of an empty array, so it fails because it cannot change its value.

1 Like

Thank you for your reply

I tried to add += with your advise.
However, using += on the code causes Error as the picture shows.

=+ is correct (but with code, the same problem still occur) or is there any problem here?

Regards

You did not write array{TriggeredCard} as mentioned in the previous reply. To add an element to an array you must write the element you want to add inside an array like this for example:
set MyArray += array{MyElement}

Just to complete what @SeedohFN is saying, arrays are immutable in Verse, which means they cannot be modified. So we just replace them instead. When you do :

set MyArray += array{MyElement}

You’re actually creating a new array which is equal to MyArray + array{MyElement} and putting the result inside MyArray again.

Which is also why we have to keep the size of arrays low so that the concatenation doesn’t get too heavy.

1 Like

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