I am wondering if it is possible to spawn one creative_prop OnBegin() and then Dispose() of it and use SpawnProp() to spawn another in a function in the same script.
Currently, I am making a local variable to store the prop instance inside, but am only able to Dispose() this prop from inside the same function, but not the same script.
NPC_tag := class(NPC_master){}
# Custom NPC script that derives from the base class 'creative_device'
NPC_master := class(creative_device):
# ========= General =========
@editable var NpcName: string = "Name"
@editable var InteractDialog: popup_dialog_device = popup_dialog_device{}
@editable var NpcInteraction: button_device = button_device{}
var WorldTransform: transform = transform{}
var StashTransform: transform = transform{}
# ========= Meshes =========
@editable IdleMesh: creative_prop_asset = DefaultCreativePropAsset
@editable InteractMesh: creative_prop_asset = DefaultCreativePropAsset
# Runs when the device is started in a running game
OnBegin<override>()<suspends>:void= {
Print("NPC_master has spawned!")
set WorldTransform = GetTransform()
set StashTransform = GetTransform()
NpcInteraction.InteractedWithEvent.Subscribe(HandleInteraction)
InteractDialog.DismissedEvent.Subscribe(HandleDismiss)
HandleIdle()
}
HandleIdle():void = {
NpcInstance := SpawnProp(IdleMesh, WorldTransform)
if(SpawnedIdleMesh : creative_prop = NpcInstance(0)?):
#spawn {Idle(SpawnedIdleMesh)}
NpcInteraction.Enable()
InteractDialog.Hide()
if(NpcInstance(0)?){
}
}
# Functionality to run on interaction with button device
HandleInteraction(Agent:agent):void = {
NpcInstance := SpawnProp(InteractMesh, WorldTransform)
if (SpawnedInteractMesh : creative_prop = NpcInstance(0)?):
#spawn {Interact(SpawnedInteractMesh)}
NpcInteraction.Disable()
InteractDialog.Show()
}
# Functionality to run on dismissal of dialog device
HandleDismiss(Agent:agent):void = {
Print("Finished conversation")
NpcInteraction.Enable()
}
Idle(PropA:creative_prop, PropB:creative_prop)<suspends>:void={
PropB.Dispose()
}
Interact(PropA:creative_prop, PropB:creative_prop)<suspends>:void={
PropA.Dispose()
}
An update on this issue, I have been able to dispose of instances of creative_props by storing them in a global variable and then feeding them into a function parameter for disposal whenever it is needed.
This does work on the first interaction and dismissal of the button_device and popup_dialog_device, although not after this first loop of logic.
Below shows the current script:
# Custom NPC script that derives from the base class 'creative_device'
NPC_master := class(creative_device):
# ========= General =========
@editable var NpcName: string = "NPC Name"
@editable var InteractDialog: popup_dialog_device = popup_dialog_device{}
@editable var NpcInteraction: button_device = button_device{}
var WorldTransform: transform = transform{}
var StashTransform: transform = transform{}
var TrueOrFalse: logic = true
# ========= Meshes =========
@editable IdleMesh: creative_prop_asset = DefaultCreativePropAsset
@editable InteractMesh: creative_prop_asset = DefaultCreativePropAsset
var StoredIdle: creative_prop = creative_prop{}
var StoredInteract: creative_prop = creative_prop{}
# Runs when the device is started in a running game
OnBegin<override>()<suspends>:void= {
Print("NPC_master has spawned!")
set WorldTransform = GetTransform()
NpcInteraction.InteractedWithEvent.Subscribe(HandleInteract)
InteractDialog.DismissedEvent.Subscribe(HandleIdle)
SpawnIdleMesh()
}
# Runs on dismissing event
HandleIdle(Agent:agent):void = {
Print("Finished conversation")
SpawnIdleMesh()
NpcInteraction.Enable()
InteractDialog.Hide()
}
# Runs on interaction with button device
HandleInteract(Agent:agent):void = {
Print("Started conversation")
SpawnInteractMesh()
NpcInteraction.Disable()
InteractDialog.Show()
}
SpawnIdleMesh():void={
CleanupMesh(StoredInteract)
NpcInstance := SpawnProp(IdleMesh, WorldTransform)
if(SpawnedIdleMesh : creative_prop = NpcInstance(0)?):
set StoredIdle = SpawnedIdleMesh
Print ("Removed StoredInteract mesh")
}
SpawnInteractMesh():void={
CleanupMesh(StoredIdle)
NpcInstance := SpawnProp(InteractMesh, WorldTransform)
if(SpawnedInteractMesh : creative_prop = NpcInstance(0)?):
set StoredInteract = SpawnedInteractMesh
Print ("Removed StoredIdle mesh")
}
CleanupMesh(Prop:creative_prop):void={
Prop.Dispose()
}
As shown in the script above, the NPC’s animated mesh (using animated_mesh_device) is swapped as the player uses the hidden button and popup dialog. On the second use of the NPC onwards, the ‘interact’ mesh is disposed, but the idle one remains, despite using the same function to destroys the meshes.
If anybody could suggest a fix for this, or let me know whether it is a known bug with the button_device, then please do!
I found a solution! It’s messy and not great, but adding the Dispose() function at the beginning of the functions to remove the mesh there prevents duplicate meshes appearing on the NPC when interacted with.