Hi Guys,
I have a game where I can destroy objects by clicking on them however I would like to make a few of them undestructible. Anyone could help me how to do that?
Thank you
Benett
Hi Guys,
I have a game where I can destroy objects by clicking on them however I would like to make a few of them undestructible. Anyone could help me how to do that?
Thank you
Benett
We need to know how you are making them destructable in the first place.
Without knowing much, I would create a parent class “Clickable_BP”, give it a Boolean variable “Destructible” then make all clickable items children of that class so you can run 1 cast when clicking. Then when you click on a “Clickable_BP” do a “Branch” based on it’s “Destructible” variable and do what you want.
Let me know if this works or if you need any assistance.
[EDIT]
Ok, based off the image you posted it would be really easy. I would still do a “parent” blueprint of with a name like “Clickable_BP”. So what you will need to do is known as “Casting” you will drag out from the “Hit Actor” and “Cast to” whatever object type you want to. This is why I suggest making a parent blueprint.
Here is a really useful lesson so even if you don’t think this is the solution you want I would try it because it will be a very powerful tool in your tool belt.
For a quick overview of how to do that, first create a simple “Actor” blueprint. Name it “Clickable_BP”. In it make a function called “Activate”. Add a simple “Print String” node and set the text to “I am a parent”
Now make a new blueprint, stop where it is asking you what kind you want to make. Look for the search bar at the bottom. Search for “Clickable_BP” select it and hit the green “Select” button. You have just created a “Child” of “Clickable_BP” This will have all the properties and functions of “Clickable_BP” but you can add to them, modify them, or override them. I would create 2 blueprints this way and name them “Destructible_BP” and “NonDestructible_BP”
Working? Awesome.
Now go back to your FirstPersonCharacter and drag out from the “Hit actor” pin. Look for the node “Cast to Clickable_BP”. Then “On Success” have it call “Destroy” on the output pin from the cast.
Go ahead and toss a static mesh component onto your “Clickable_BP” and toss all 3 of the blueprints into the level. You should be able to click on and destroy all 3 of them.
Ok, it’s working? Sweet. Lets learn to call that activate function and how to modify what it dose in the children.
Lets go back to the First PersonChatacter. Remove your “Destroy” call. Dragging out from the cast pin, try to find the “Activate” function you created in your “Clickable_BP”
Now when you click on one of the blueprints it should say “I am a parent”
You successfully have cast to an actor and called a function inside that actor.
Now let’s “Override” that function in the children.
Open up “Destructible_BP”. Look at the “functions” section on the left. When you mouse over it you should see a dropdown labeled “Override”. Click it and select “Activate”. It should open up a new “Activate” function. Add a “Print string” node and then make the text read “I am a child”. Go ahead and change the static mesh in the static mesh component of this blueprint.
When you click on this Blueprint in game it should say “I am a child” while the others should say “I am a parent”
Congratulations, you have successfully overridden a function.
As a note, in the overridden function you can right click it and “Add call to parent function” this will execute the function as written in the parent then let you execute unique code for this child as well.
So now you know how to have multiple blueprints that can react in different ways to a single call. All you need to do now is call “Destroy” in the “Activate” function on your “Destructible_BP” and do whatever you want in your “NonDestructible_BP” when you click on it, if you want to do anything fancy there.
You could also just use the parent and forget all the children if you dont want to do anything fancy at all, but now you have an incredibly awesome tool in your belt.
Let me know if this helped or if you have any questions.
Best of luck!
Better yet make an IClickable interface with an OnClick event/method. In classes that are destructable, destroy them.
Hi Guys,
This is my blueprint for deleting objects with a click. I am new to blueprints so I created it as part of the FirstPersonCharacter buleprint. What do you think? Can I work with this?
As a general rule of thumb you always want an actor to take care of itself. Outside classes should not tell other objects what to do, similar to how your mother doesn’t dictate how you do the laundry and wash the dishes - she only cares that it gets done.
The best approach here is to replace that Destroy call with an interface method call. Create a blueprint interface called IClickable and give it a function OnClick. Attach this blueprint interface to your actors.
Controller Code
Destructible Object
Hey Guys,
Thank you for the answer. Really appreciate it.
I tried everything you guys said but the problem I faced is that I have a lot of objects in
my scene that do different things therefore they are added as different blueprints.
Could I somehow create a Clickable blueprint and make the other blueprints its children?
Or could I just make a blueprint of the few objects I want to make undestructible so when I run a
branch it just identifies what can not be destroyed?