Can't Cast to Blueprint

Hello!

I’m new to coding/blueprints and hoping to get some help. Making my first little game to learn the ropes.

I have a character with health and air stats. His space helmet is broken, and air depletes on a tick until the character finds a space-pod, or dies.

The space-pod is an actor BP (just a plane mesh with a collision box).

In my character BP is an air deplete function.
When character overlaps a space-pod, air is replenished continuously. Air deplete stops.
When character leaves space-pod, air deplete restarts.
In the space-pod BP there’s a Boolean which sets to 1 on begin overlap. Resets to 0 on end overlap.

This Boolean is called from the air deplete function in character BP to switch the depletion on/off.

Now, this all works if I call the Boolean using ‘Get Actor of Class’ and select the BP_Spacepod actor from the dropdown, but the tooltip tells me this is expensive.

However, if I try to cast to the BP_Spacepod and grab my Boolean that way, it won’t allow me to attach an object. I’ve tried attaching the mesh, the collision box, the scene itself. But no dice. What am I doing wrong here?

Thanks!!

1 Like

You’re having a problem here, because you don’t need to cast actually :slight_smile:

There’s a misunderstanding, that I think comes from many many youtube vids, that casting is a bit like slinging a hook, and hoping to catch your blueprint.

It’s not. Casting is for checking if a reference you have is of a certain type.

In your case, if you said ‘get all actors of class’ and just specified ‘actor’, you could then loop through them, casting each one until you found your blueprint. Yes, that would be wasteful.

But… if you use ‘get actor of class( your blueprint )’ it’s not wasteful at all. Only maybe if you did it on Tick…

The way most people avoid all this in the first place, is using overap. So code your blueprint with a collision volume, and when it overlaps your player, you can tell them ‘you are getting oxygen’…

1 Like

Hiya. Let’s see if we can get you going. What you have up there is not really the right approach to what you want to achieve. Consider the following:

  • a widget with a progress bar and a single event that updates it, nothing else:

image

  • the player blueprint has a widget component that spawns a widget for us:

A single event counts air and updates the widget.

edit:
image

{we lose 5 air units 30x per second}

  • the space air pod:

We step into the pod, we get air. The Event Dispatcher:


This is just one way to approach it from an OK angle. It can still be improved. But now we can place air replenishing actors all over the map and stepping into them gives us air back:

Project link - sometimes seeing it in person is nicer than screenshots:

1 Like

Thanks so much for this. So helpful!

Could you explain the timer handle for me please? What does it do?

tl;dr:

It’s a reference to the timer so we can tell it what to do.


One can automate variable creation by right clicking pins:

image

This works for pretty much any data pin in the engine. Timers count time, a handle allows you to easily access the timer and operate it:

  • perhaps you want to check how much time has elapsed in a racing game
  • perhaps the player goes to the main menu and we want to pause the oxygen loss while they fiddle with settings
1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.