Hi everyone,
I’m currently working on implementing an item system and am trying to figure out how to implement spawning/despawning meshes when dropping and picking up items. I’ve seen a few examples where an actor’s UStaticMeshComponent is destroyed on pickup, but how would I go about respawning that mesh when the item is dropped? Any tips are helpful at this point.
Add it again i guess
You name from component decleration
AddComponent(FName("Mesh"), false, FTransform(), NULL);
I didn’t feel okay with destroying the mesh just for hiding/showing purposes, so I implemented it in the following way (I don’t know if this is the “right” way of doing it). I don’t have access to my code at the moment, but this is the general gist of it.
- On
AActor::PostInitializeComponents
, I store a reference to my UStaticMeshComponent
's StaticMesh
member in a private UStaticMesh*
variable.
- On pickup (called on an overlap event), I call
UStaticMeshComponent::SetStaticMesh
with a nullptr
as its argument so the mesh will no longer be displayed. At this point, I also disable my collision detection and add the item to the player’s inventory.
- On drop, I call
UStaticMeshComponent::SetStaticMesh
again, this time providing my private UStaticMesh*
variable as its argument. This will display the mesh again and I turn my collision detection on again. When dropping, I update the item’s location to be the player’s location + FVector(100, 0, 0)
and apply a little impulse to simulate a throwing/dropping action.
I’ll see if I can dig up a concrete example for this later, but this is the general way I handled it.