I am aware there is lots of content already on casting. I’ve done lots of research into it, but honestly still have no idea how to cast to objects or widgets. This part im stuck on now (pictured), I want on the button press to destroy an actor in my scene. However, I have no idea how to reference it. The item i want to be destroyed is just a static mesh placed in my scene. I feel like this shouldn’t be difficult but whatever i plug into it, it just comes up with compiling errors.
Please provide the pic
I want on the button press to destroy
an actor in my scene.
There are many ways of doing what you need. Choosing the right solution will depend on the actual use scenario.
If all you want is a strict 1-to-1 relationship between a widget button and a static mesh actor in the Level Blueprint, create the widget in the Level Blueprint and:
No casting required in this case. But this is pretty limited in scope. Consider describing briefly what the end goal is - it would be easier to suggest a communication method more suited to your needs.
Apologies for forgetting to add the image. So obviously i just need to plug something into my target in the destroy actor.
That’s an interesting method to see. Haven’t managed to get it to work for my case, however could be handy so thank you very much. Im a beginner so any help is great.
I have however attached an image of my project, sorry about forgetting to link it before hand.
That’s an interesting method to see.
Event driven bread and butter of blueprints
Haven’t managed to get it to work for
This was exactly for your case… Assuming you did not do what I suggested - otherwise you’d have it working now. Tough crowd, I see
I’ll try again.
What you’re trying to accomplish with the widget fetching an object reference is fundamentally unsound and will only create further roadblocks. Trying to access Level Blueprint actors from within the widget is awkward at best. If you absolutely must do it this way, tag your actor or get all actor of class. Do note this leads nowhere in the long run, though.
You clearly have a vision, consider describing the interaction between the widget and level actor. How does it work when there’s more than 1 object? The same button destroys another actor?
sorry to be difficult, unfortunately my knowledge on these nodes is very little and im struggling! And yes the point you make is very valid at the end. To be more specific, the actors I am wanting to be destroyed on a button click are being spawned in each round. (3 randomly spawn in each round, the player approaches it and presses the button which appears as you get close enough, in turn destroying it). It’s a good point I need to consider as it could destory all the spawned actors instead of just the one im clicking on.
- I didn’t get your method to work. I think I need to brush up on my knowledge of binding and custom events more!!
Thank you very much as always though.
also to point out, the widget button is being displayed within the spawned actor. Very much so like an enemy health bar but a button instead. This might make it possible for it not to destroy all the actors if you press it
To be more specific, the actors I am
wanting to be destroyed on a button
click are being spawned in each round.
(3 randomly spawn in each round, the
player approaches it and presses the
button which appears as you get close
enough, in turn destroying it).
How does the player choose which object to destroy? The closest one they’re next to? The one they look at? The one they click? All of the above. So essentially, this one button will be responsible for destroying dynamically spawned objects? Is that correct. Can you also tell which blueprints spawns the objects?
I didn’t get your method to work. I
think I need to brush up on my
knowledge of binding and custom events
No worries. It’s of no use in this scenario. You said ‘The item i want to be destroyed is just a static mesh placed in my scene’ but now we know that ‘3 randomly spawn in each round’. That’s a different story.
Is this a widget component by any chance or a widget you Added to Viewport?
Yes sorry I thought that describing it like that would be easier, of course I realise now it’s not.The actor being spawned is simply a cube, a box collision, and the widget (button). The widget is added into my actor viewport yes. As you can see in the picture, the actor as has approached and the button is visible.
The way the player interacts with the button and it turn destroys it. Is simply approaching it, and once inside the box collision, makes the button/widget visible. If the button is visible it will allow the player to press the button. I have attached an image of the blueprint for it.
And finally this is the blueprint for the spawning. In essence it makes 3 cubes spawn at a random target in the map. When there are 0 enemies left, these get destroyed and 3 more are spawned randomly. This system is working well.
And just in case ill add this image. its of the viewport of the actor being spawned in. The object selected is the widget/button.
Really appreciate the time and help you’re giving me man. Learning lots here!
There are some scary things in your script, especially the spawning bit. Tick executes every frame so you’re running this code at least 60 times every second. In addition, if you look at the tooltip of the GetAllActorsOfClass, you’ll notice it tells you specifically to not do exactly that - do not run it every frame. It works because you have a branch that blocks some of the scary bits. Also, you hooked up an array to a Destroy node - that’s gonna leak memory sooner or later.
Consider looking into an event driver approach: somethings happens => trigger another thing. Last enemy died? Trigger another wave! No need to check every frame what is not frame sensitive.
And we can see you do it at least 3 times, and we don’t like it! ;p
First of all:
The widget is added into my actor
Not really - this is a widget component, it automatically creates and handles the actual widget for you. This means that in order to access your actual widget you’ll need to make one extra step - you will need to access the actual widget from the widget component. Included below.
Here’s one way to do it (also, wouldn’t a sphere be better than a box here?):
The above is far from perfect as we do not know what is happening when you overlap more than 1 object. I did not included that. Hopefully this helps a bit.
Oh wow thank you so much - it worked perfectly!! Unfortunately the blueprint I copied from you makes very little sense to me - it’s like another language but I’ll try my best to get my nodes knowledge up
Oh no my messy scripts are definitely not a pretty sight. You raise a really good point once again. To this point I have been using ticks quite a lot, for the simple fact that if something doesn’t happen on the game start (or the even begingplay node) I am unsure how to trigger something mid game. So my solution to it is having it check every tick, which like you said is an awful way of doing it. Hopefully in due course Ill improve and learn these basics.
As always, cant thank you enough for the thorough help you have given.
Hopefully in due course Ill improve
and learn these basics.
It does take time, true. Here’s an example of a basic respawn loop:
I commented it a bit to make it clearer, perhaps it will inspire you.
Good luck with the rest. The screenshot looks pretty moody!
Ah yes blueprints with comments on them are perfect for learning. Ill be sure to save that somewhere safe, thank you very much!