Issue disposing creative_prop_asset stored in array

I am creating assets in Verse and storing them in a one dimensional array. This example creates 10 assets. I then dispose of some of them randomly. The tiles that match the criteria for disposal are disposed. The problem is that other that don’t fit the criteria are also disposed even though there is no print out to mark the disposal.

I adapted this code from a youtuber’s version related to two dimensional arrays. His works and mine seems to do way more than desired.

Any ideas why more tiles than requested are destroyed?

using { /Fortnite.com/Devices }
using { /Verse.org/Native }
using { /Verse.org/Random }
using { /Verse.org/Simulation }
using { /UnrealEngine.com/Temporary/Diagnostics }
using { /UnrealEngine.com/Temporary/SpatialMath }

game_device := class(creative_device):

# asset is used in each tile cell
@editable var TileAsset : creative_prop_asset := DefaultCreativePropAsset

# single dimension array for testing tile asseet creation/disposal
var TileArray: []?creative_prop = array{}

OnBegin<override>()<suspends>:void=
    Print("onBegin")
    GenTiles(10)
    Sleep(5.0)
    DisposeRandomTile()

CreateCell(Index:int):?creative_prop=
    Spacer := 610.0
    if (Xp := float[Index * Spacer], Yp := 0.0):
        return SpawnProp(TileAsset, vector3{X := Xp, Y := Yp, Z := 140.0}, IdentityRotation())(0)
    else:
        return false

DisposeRandomTile()<suspends>:void=
    for (Index := 0..TileArray.Length - 1):
        if (Tile := creative_prop[TileArray[Index]?]):
            Rand := GetRandomFloat(0.0, 3.0)
            Print("Cell [{Index}] with Rand: {Rand}")
            if (Rand <= 1.0):
                Tile.Dispose()
                Print("Disposing cell: [{Index}] because {Rand} <= 1.0")

GenTiles(Number:int):void=
    set TileArray = 
        for (Index := 0..Number - 1):
            CreateCell(Index)

I found a fix for my issue. It involves checking for a valid item before attempting to dispose of it. Please see DisposeRandomTile() for the the changes.

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

log_game_device := class(log_channel){}

game_device := class(creative_device):

Logger: log = log{Channel := log_game_device}

# asset is used in each tile cell
@editable var TileAsset : creative_prop_asset := DefaultCreativePropAsset

var Spacer : float = 610.0

# single dimension array for testing tile asseet creation/disposal
var TileArray: []?creative_prop = array{}

OnBegin<override>()<suspends>:void=
    Print("onBegin")
    GenTiles(10)
    Sleep(5.0)
    DisposeRandomTile()

CreateCell(Index:int):?creative_prop=
    if (Xp := float[Index * Spacer], Yp := 0.0):
        return SpawnProp(TileAsset, vector3{X := Xp, Y := Yp, Z := 140.0}, IdentityRotation())(0)
    else:
        return false

DisposeRandomTile()<suspends>:void=
    for (Tile : TileArray){
        if (Prop := creative_prop[Tile?]):
            if (Prop.IsValid[]):
                Rand := GetRandomFloat(0.0, 3.0)
                if (Rand <= 1.0):
                    Prop.Dispose()
                    Print("Disposing of Prop/Tile because {Rand} <= 1.0")
    }

GenTiles(Number:int):void=
    set TileArray = 
        for (Index := 0..Number - 1):
            CreateCell(Index)

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