Is it possible to Switch on wildcard?

I created a function that takes a wildcard as input. Within the function, I want to use the datatype of the wildcard to switch between what code to execute. However, I have not found a way to do this in blueprint and I am unsure if it is possible. Any assistance or suggestions would be appreciated.

Probably not possible. Unless the card is not that wild; are you expecting specific data types?

image

If so, you could cast.

I’ve considered this solution, but I don’t want to use casting as it could be cost-prohibitive in certain situations. Additionally, if I anticipate handling 100 different datatypes, with the final one being an int, I don’t want to perform casting 100 times in the case of the input being an int as it would be unnecessarily costly.

So how are you expecting switching to work when you pass a float vs passing an actor? If you’re using objects only, perhaps there’s no need for a switch at all and you could use an interface.

Not sure how to advise without knowing more. Stumbling in the dark here.

100 different datatypes

Examples?

I am going to attempt to explain an examaple, hope it helps.

I have a grid based game. The grid has a function called PopulateGridWith and takes a wildcard as input. In the game there are many different things that can be placed on the grid: Enemy, DecarativeMesh, Ally, Chest, Trap, etc…

Fore some reason I want to add a Trap to a grid. I dont want to fail to cast to Enemy then DecarativeMesh then Ally then chest and then succeed a cast to trap. That is expensive.

I also want to avoid the tile class to have alot of different functions for populating tiles. Ex: PopulateTileWithEnemy, PopulateTileWithTrap, etc…

I hope this example helps

You do not want a switch for this - you’re right in thinking it’d be too messy. Where is the input coming from? A database / player input? How do we know what needs spawning where?

Fore some reason I want to add a Trap to a grid.

How do we know that? The player chooses it from the interface? Or you have XY sheet and we read rows & columns? Is this like a map editor?

1 Like

This was just an example, I am not trying to make a grid based game. Aother maybe better example could be a function with a wildcard input that simply prints what the input was. For example if the input was a float then function prints “You inputted a float” or if the input was BP_Enemy then the function prints “You inputted an enemy”.

For objects, you could easily use an interface:

image

Each object can return what they are, and be descriptive about it, too.


To identify a float, int, bool, text, vector from a wildcard? Not sure. Is this even feasible? It’s hard for me to imagine a scenario so ambiguous where something like this would be needed - perhaps that’s my problem.

Also function + wildcard? You probably meant a macro.

1 Like

It could be resolved in the following way:

Pack value to JSON:

Unpack with switch:

3 Likes

Ha, clever! That’s going to be a slow operation. And mind the typos!

1 Like

This is different from the original question but I believe accomplishes same idea:

in my own project, I want a single event to be able to deliver many different data structs. I solved by using an Object class for payload.

I make a base class derived from Object. To this I add an instance editable GameplayTag.

Then, each child of this class can set the Tag which is used to identify what the payload is. They can also setup whatever data they want within the object.

Then just construct the object, pass into the event, and whoever receives the event can read that Tag to know which subclass of PayloadObject to cast to. Then they can extract the data.

I think that in theory this is same as the JSON example above, except that within an object blueprint you could pass whatever data you wanted. It could be many structs if you needed.

3 Likes