Cast to (insert class/blueprint name here)

Does anyone know the node I’m looking for? Can’t seem to find a node that will take an object and a class as inputs and returns the object casted to the passed in class.

The trick is that “Cast To …” methods will show only when you want to create a node by draging it directly from Return Value. So don’t click the right muse button on blueprints, only click and drag from Return Value (or Getter) of object you want to cast. Like on the image. Remember that blueprints are smart and won’t let you cast anything you want, only objects that inherit from one another.

That didn’t really help. My point is that I can’t to be a way to cast to a variable class without using several conditional branches and checks, which isn’t efficient or scalable. For instance say I want to take an object return value and cast it to a class, but the class I want to cast it to depends on some other conditions (the class is variable and will change with execution.)

That’s the kind of node I am looking for. If it exists, where do I find it? Also, if it doesn’t exist How would I go about doing this in the most scalable (and hopefully efficient) way possible?

To clarify the original question:

For instance say I want to take an object return value and cast it to a class, but the class I want to cast it to depends on some other conditions (the class is variable and will change with execution.)

That’s the kind of node I am looking for. If it exists, where do I find it? Also, if it doesn’t exist How would I go about doing this in the most scalable (and hopefully efficient) way possible?

Thinking about the code, this may not even be possible due to limitations in C - family languages as I am not sure that you can even do such a cast in C++ as the ‘as’ keyword takes a type as a second operand, which I’m not sure can be replaced by a variable.

You can’t dynamicly cast to some class (which i think what you mean by saying “depends”), but it think there might be possibility to develop such a node in C++ from UK2Node class. You can reduce casting by building more developed base classes or more specific base classes in base classes, for example Item class have subclass Weapon and then subclass RailGun.

I’ve started playing around with casting and there are few things.

I made class Gun, and classes Pistol, Shotgun and Bazooka that inherits from Gun.

In C++ (as far as I know) you can’t have an instance of a parent and cast it to childs. You need a child that is type of it’s parent, for example:

Gun* myGun = new Bazooka();

If I understand correct you are trying to do something like:

Gun* myGun = new Gun();
((Bazooka*)myGun)->Shoot();

Normally in C++ it will cast, but it will still execute Shoot method from Gun, not Bazooka. In blueprints it doesn’t cast at all. That’s because the program would have to have information about every children in one parent.

But you can create an array of Guns and fill them with different kind of guns and then pick them (based on index) and shoot.

Edit 21.11.2014:
I’m not sure if that’ll help, but I found old code from UE3 in UnrealScript, where I was casting almost dynamically. I had class named PackageTemplate and a TesterPackage that inherits from it. There I had some function like:

function LoadPackage(class<PackageTemplate> PackageType)
{
   local PackageTemplate PackageToLoad;
   PackageToLoad = new PackageType;
   PackageToLoad.Load();
}

And I was running it using:

LoadPackage(class'TesterPackage');

If it was possible in UnrealScripts it should be possible in C++ too. But still you need proper parent-children structure for this.

When you need to cast depending on some input your class setup wrong.
Cast to is a way to tell the program “Hey game, you don’t realize it but this is actually of type X”. This is not “Hey game, morph this Apple to an Strawberry, because you know, both are Fruits”.
If you don’t know the type you need, your code makes no sense.

Read about interfaces, this is most likely what you want to use.

Or introduce a parent class which you then extend in your subtypes. Eg a parent Weapon a with child blueprints like Gun, Turret, Bazooka. This makes it easy to define generic functions (in the parent) and - if you need - you overwrite it in your subclasses.

He might not necessarily be trying to change an Apple into a Strawberry. He could simply have a variable which is being dynamically set to multiple classes, making it impossible to know what to cast to.

However, I do agree that an interface would probably work best in that situation.

@PixelOmen try to give a sample. I think you will have trouble with that, because it means you want to say that you can do an generic action on specific objects which do not share anything in common.

Example: You’re tired of having to create 2 nodes to cast to MyPlayerController each time, and would rather make a static GetCastedController() I can call, that just gets my player controller, already casted and ready to go. It would accept the player class, find the controller, do the cast, and return the results.