Some background: I have an NPC Spawner creative device with four instances, with two NPCs assigned to each (8 NPCs in total). Depending on a random number (0, 1), the corresponding NPC will be spawned. For example, if “0” is generated, the NPC at index 0 will be spawned. Here’s the array generation:
# Array Control
SpawnerData := class(creative_device):
var RandomArray :[]int = array{}
GenerateRandomArray(): void =
for (i : int = 0 .. 3):
NewValue : int = GetRandomInt(0,1)
set RandomArray += array{NewValue}
Print ("RandomArray[{i}] is now {NewValue}")
This successfully generates four random integers and appends them to RandomArray. However, when I try to reference the updated RandomArray in different classes, I realize that I’m actually referencing the initial empty RandomArray array. Here’s what my NPC spawners are doing:
# NPC Spawner Control (4 Instances)
npc_spawner := class(creative_device):
@editable
SpawnerArrayDataRef : SpawnerData = SpawnerData{}
@editable
NPCSpawners : []npc_spawner_device = array{} # Two NPCs per instances
@editable
InstanceID: int = 0
# Code to enable spawner
EnableSpawner(Spawner : npc_spawner_device): void =
Spawner.Enable()
# Spawns corresponding NPC
SpawnNPC() : void =
for (i := 0 .. SpawnerArrayDataRef.RandomArray.Length - 1):
if (i = InstanceID):
if (Spawner := NPCSpawners[SpawnerArrayDataRef.RandomArray[i]]):
EnableSpawner(Spawner)
Print("Enabling Spawner {i}")
# Debug
CheckRandomArray (): void =
Print("{SpawnerArrayDataRef.RandomArray.Length}")
InstanceID is manually edited in the editor to distinguish between each instance of npc_spawner. The code to call all functions is in a separate manager class:
# RUNS THE GAME
GameManager := class(creative_device):
@editable
SpawnerArrayDataRef : SpawnerData = SpawnerData{}
@editable
NPCSpawnerRef : npc_spawner = npc_spawner{}
OnBegin<override>()<suspends>: void =
SpawnerArrayDataRef.GenerateRandomArray()
NPCSpawnerRef.SpawnNPC()
On start, SpawnerArrayDataRef.GenerateRandomArray() runs fine, but NPCSpawnerRef.SpawnNPC() does not. After some looking, I realized it was because the functions are referencing the initial RandomArray variable, and not the updated one with values in it. How can I rewrite this so functions reference the new variable?
I’m new to Verse (and programming in general), so this might not be the most optimized way to do things. Any help is appreciated!