Confusion about how to cast properly

Hello,

I am trying to make the enemy in my game when they die to drop coins that when the player collides with them to immediately add to the currency. I actually made almost everything (picks up upon collision, add to the GameMode, and also saving it). I want a popup message to appear that it will tell you how many coins you picked up. I made it so an animated text to appear by setting the opacity from 0 to 1 and after a while back to 0.

But I am having a problem with the text. You see, the coin actor will generate a random number from 1 to 10. I want that number to transfer to my widget so I can print it but can’t make the cast to the actor to work. I don’t know what to put in my object wildcard to reference the correct coins actor because there can be 10 different actors with different random numbers in them.

Hi man,

You can from an actor reference, use "cast to - MYActorCoin " to check if the typer of actor you have there is the MyActorCoin type.
You could make this check for every actor but
A clever way could be flip the problem.

We have Something called “Blueprint Interfaces” to deal messages between multiple type of actor.
OR
you could just make an event to add to the player N coins, and make the coinsActors call it when they collide with player.
OR
There are other ways to checck if the player has triggered a coin1, coin5, coin10
You can check the Class of the actor , ot use some Tags to identify the coins.

I liked the one with the tag as it sounds a very organized way to do it.

I found three nodes “Get all actors with tag”, “Get All Actors of Class with tag” and “Get All Actors of Class Matching tag Query” all with array outputs.
I guess what fits there is the “Get All Actors of Class with tag” as I can give it a tag and my MyCoinActor class.

So…I have some questions

As soon as the CoinsActors will spawn upon the enemy’s death it will take a tag automatically or I have to put the tag?
And how can I check in my widget(I want to call the actor there) the correct tag?

I have never used tags before so I don’t know how they really work.

Suppose you have a class: Animal. You can make child classes that inherit from Animal, these would be Dog, Cat, Wolf, etc… You can cast Animal into Dog, Cat, Wolf etc… because you know that these are animals. Now replace Animal with Actor, and Dog with Coin. When the player overlaps with an actor, you can try casting that Actor to Coin. Everything will make a lot more sense if you watch some tutorials about the fundamentals of programming.

Instead of a tag, you can use the OtherActor pin of the HitResult of the collision between the coin and the player.

Cast it to a CoinBp and read it.

Even better, use an interface as mentioned previously.

The problem with a tag is it requires iterating a bunch of actors and is therefore significantly slower.

You already have both actors involved in the collision/overalap.

Thank you for your replies and sorry for not replying faster, I was a little busy.

I managed to transfer the amount of the coins received using hierarchy.

It confused me at first but I managed t do it.

I have one more question though. The only way to communicate between two completely different actors that not inherit from each other is via custom events?