Avoid CAST? Alternative?

Hello,

Lots of YouTubers said to avoid to CAST object for performances.

Do you have some alternative? to avoid cast?
some tips to share?

Thanks in advance.

Nothing wrong with cast.

Don’t fall into the youtube misinformation black hole.

You could say cast isn’t a great idea if you’re casting to hundreds of things trying to figure out what just overlapped, but if you’re casting to that much stuff, you have another problem anyway.

6 Likes

I can understand that put a cast inside a loop, it’s not really good.
What i did for that it’s create a variable with a cast at the Event Begin play. And use that variable instead of the cast.

But if it’s a wrong information to avoid cast, i will use them… becuase i build all object by avoiding cast, but structure of them is not really good. to much code inside one classe insteaf of separate them by component and utility to avoid casting…

but it’s not a big deal to make cast sometime… i will use them :smiley:

2 Likes

Cast is a bit like Tick. The nodes are not related, but you can bring the engine to it’s knees if you haven’t put any thought into what you’re doing, that’s all.

The main thing with cast, is it loads the asset. So, if you going through lots of casts trying to figure something out, you’re loading a lot of assets for no good reason.

But, if there’s only your player overlapping objects, well, your player is loaded ANYWAY, so casting doesn’t make any difference.

There are some times where interfaces are a more elegant method than casting, so it’s good to find out about them.

But apart from that, don’t worry :slight_smile:

2 Likes

I don’t really know what you’re doing there, but it sounds like a good place to use an interface.

You don’t want to be casting and then setting variable inside other blueprints ( or calling functions ) for example. Just because that’s not good programming. BP A shouldn’t know about the inside of BP B.

So an interface is a great option there, you can give BP B instructions, or ask it for info, without knowing anything about the code inside BP B.

Once you get it nailed, you can use just a couple of calls to do most things.

But, if there’s only your player overlapping objects, well, your player is loaded ANYWAY, so casting doesn’t make any difference.

Most of the time I cast to retrieve actor on the map.

For example: I walk on block

My character, cast all block each time, to retrieve the color of the block.

If i understand what you say… because they are already all inside the map, it’s not a big deal to cast them to retrieve the current block on which my character step on.

Is it correct?

Yes, that’s right. The system already has ‘block’ loaded, cause they’re in the map, so no big deal.

Again, though, good place for an interface. You can have a call, ‘get color’. And the block tells you what color it is.

Now you don’t have to even know it’s a block. To you, it’s just an actor, and you can ask it something.

That way, your player doesn’t know about blocks. It’s called separation, or decoupling.

2 Likes

So an interface is a great option there, you can give BP B instructions, or ask it for info, without knowing anything about the code inside BP B.

But interface allow me to communicate only if i know the BP which who I need to communicate.

In that case, before to be able to send a message to my blueprint block. (the one the actor step on) I need first to cast the block inside the actor collision box. When i know that my actor communicate with this block, i can send a message through interface.

I have try longtime, to send interface without knowing who will receive it, and it doesn’t work… :frowning:

So in a static project, when you know all your existing actor inside the map. it’s ok
I have a dynamic project, where i build the map dynamicly, so i don’t know where are the block on the map. only way for me to know where is a block is to cast the block each time my collision (overlapping) notice that a new block is inside.

So is it bad to cast all the time the block? or is it ok? Interface could work? did i miss the understanding of the interface?

Now you don’t have to even know it’s a block. To you, it’s just an actor, and you can ask it something.

That way, your player doesn’t know about blocks. It’s called separation, or decoupling.

Ok i will make some search regarding that topic. I was not aware about that.
If i can send the GetColor to the actor… without casting, it will be good :smiley:

The point is with an interface, you send the message without know the type of the other actor. That’s the big difference.

1 Like

If the actor implement the interface it will respond to the call.
I will try that too.

Like I say, you really don’t need to worry about it now. For now, casting is really really fine :slight_smile:

But later ( few months ), you might notice some of the logic of the blocks is in the player. And some of the logic of the player is in the blocks. Then, the way to separate them is using interfaces .

But later ( few months ), you might notice some of the logic of the blocks is in the player. And some of the logic of the player is in the blocks. Then, the way to separate them is using interfaces .

That’s already what i do now. :slight_smile:

1 Like

With an example

Here i get my collision box. Retrieve the actor that I cast to a block. and i ask to the block to change the color.
I’m in the player BP.

The color logic is inside the Block BP.

But like you see, i cast the block before calling the Change Block Color

So the player is telling he block to change?

Notice, you already know it’s a block before you make the interface call. Then it’s too late :slight_smile:

Make it so that your player will call the interface without checking what kind of thing it’s talking to, that’s real interfaces.

Because if it IS a block, boom, it changes colour. If it’s not a block, no problem, nothing happens.

1 Like

Lol, it works…

Amazing :smiley:

2 Likes

Pow! :slight_smile:

See, you can tell the sky sphere to change color. It won’t because it doesn’t know how, but it doesn’t matter…

This is how you do interaction. You can have a call like ‘use’, but it means something different to every actor. Door → open, ball → throw, sky → nothing…

1 Like

And like that… if 2 differents actors, use the same Interface and re-define the same methode… then it will automatically chose the methode link to the good actor.

It change a little bit my view on the general flow. I will rework my game.

Thanks for your help. I see better how to use that interface now.

1 Like

:sunglasses: :+1:

One more question regarding to all of that.

If all my actor have a common interface: getName() where i return the name of the object.

When i use the methode, i can “simulate” a cast with the getName()

So with a methode like :

image

I could say if getName()=targetName then.

It’s correct?

Is it usefull?

It’s an example because here i know the type of actor