I’m making a city builder, so I have buttons that spawn different building types, and I need multiple buildings of the same type to be spawned in all at once, and that works, the only problem is that I have a description box that appears when I click on a building, and it has a “destroy” button that is supposed to destroy the building, but instead, it destroys every building of that type. This is how I destroy it:
The “destroy Actor” in the first image’s target is set to “self” because I want to destroy itself, but that’s what makes it destroy every building of that type, so I need a way to create a copy instead, but I don’t know how to do that.
This is how I spawn a building when I click a button in the hud widget I made:
Hi, can you show where you call the event dispatcher DestroyActor from your first image? And what is HudWidget of your player controller?
The “destroy Actor” in the first image’s target is set to “self” because I want to destroy itself, but that’s what makes it destroy every building of that type
It will only destroy itself, not any other actor of that same class. If all buildings get destroyed, then because you call that code on each building.
My guess currently is, that you bind all your buildings to the same event dispatcher (in the event begin play) and therefore you destroy them all when you call the event dispatcher.
Give it an Output called like “House” of the Masterclass of your buildings or just the residential building class.
Add that Interface to the Residential House Blueprint and get a self statement and hook it up inside the Interface
Now when you want to destroy the actor, make a quick Custom Event like “Destory Building” inside the Bulding Class and hook a simple Destroy Actor node to it for now.
I think you can already get the actor of the building you clicked on so get that one, hook it up the “House Ref (Message)” and from the Output, call “Destroy Building” (The Custom Event).
I hope you get the Idea what I’m talking about, let me know if it helped you.
I call the event dispatcher in the second image, it’s in the widget hud. I have the same event dispatcher for each building type, so let’s say I have 3 houses, each of them have their own event dispatcher, so if I have 5 house1 spawned, it’s will destroy all 5 rather than just the one I clicked on. So is there a way to make dispatchers some things that haven’t spawned in yet? Because if I spawn another house1 I’ll need another dispatcher I assume.
Your current code will destroy all the buildings, since you bind them all to the same event dispatcher.
I don’t see why you need any event dispatcher here at all. What you could do is:
(-) When you click on the building (e.g. use one of the GetHitResultUnderCursor functions, check that the resulting actor is a building via e.g. casting or tag), remember that actor reference. I guess you already more or less have that, since you stated above that you open a widget once you click on a building.
(-) Now when you click the destroy button in your widget, use that remembered reference above, and then call DestroyActor on that reference. Or if you want / need to do something in your building when its destroyed, either cast to it (e.g. make a building baseclass and cast to that) or call an interface event on your building.