Hi, I am working on a turn-based battle system, where I have set it so that data for attacks, such as their stats and the names of related functions dictating how the attack works, are stored on data tables. I am now trying to set up the ability for a function to call an attack’s “OnUse” function using the value on the corresponding row of the table. I then need to pass the required variables from the game mode as parameters. These functions are stored on a Blueprint Function Library. Is there a way to call these functions by name, and pass said parameters to them? Maybe if there’s a way to store a reference directly to the functions as a variable in the table and call them, with parameters at runtime?
Hey @SwipesLogJack_T!
That’s totally possible! So what you need to use is “Get DataTable Row”. You’ll specify the data table to use and the name of the row, then the output of that node is a struct. You can then break the struct and use the data from that data table row, such as “Name” “Damage” etc to generate your stats on the fly!
Thanks, this helps, but now, i’m having trouble trying to figure out how to call the function in the library by name. I can’t seem to find any direct way to do this and timers don’t seem to work for this. Is there a simple way to do this?
I think I’m starting to understand what you’re asking, but just to clarify, you want to access the Blueprint Function Library using a variable like “NameOfFunction” and grab the appropriate function from the library and run it based solely off of that variable?
Yes, this is exactly it, sorry for the late reply, i was working in the early hours of the morning and went to bed. Is this possible? I guess if need be, I could create an Actor Component and store extra functions there, using them to call the functions from the library, with all the parameters included, but making one for every attack is going to add up quickly. Is there a faster way?
you could just have a switch on name or enum link to a different function but thats actually not very modular since you have to add new switches for new abilitys.
the way i did it was to just spawn an actor/object that simply executes the function that way all i need is the class reference and i can run anything
There are several ways to do this, one of the simplest (although least optimized) is to use CallFunctionByNameWithArguments
. You have to format arguments like a command line call, but converting to string just works in most cases. This method however does not let you retrieve return value or out parameters.
For libraries the object you need to call the function on is the CDO.
//sample: void UMyFuncLib::OnUse(UObject* Target, float SomeParam)
UObject* Lib = GetMutableDefault<UMyFuncLib>();
FString Cmd = FString::Printf(TEXT("OnUse %s %f"), *Target->GetFullName(), SomeParam);
bool bSuccess = Lib->CallFunctionByNameWithArguments(Cmd, GWarn, nullptr, true);
Yeah, this is just about the same conclusion I’d come to, thanks.
The issue I have is that some of the abilities will require return values to the game mode. Thanks for the help.
There are other methods that will let you retrieve it.
But if your functions have different signatures, you will have to switch based on function name anyways so it is kinda pointless.
If you can have a canonical signature for all your functions, then you can call any of them with a single implementation (and retrieve return/out params).
This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.