Casting a Struct? FTableRowBase

Is there a way to “cast” a struct? Specifically FTableRowBase child? I would like to retrieve a generic row from one of the data tables and then ‘cast’ it into a specific row in a function but I haven’t figured out a way to do this yet.

What stops you from using the generic Cast<>()? Does some problem occur?

According to this post (Can use use inheritence with UStructs? - C++ Programming - Unreal Engine Forums), unreal reflection system doesn’t support it (hence the reason your Cast<>() might fail), however, I believe you should be able to cast them C-style (which is not safe however, so be sure you only do it when you’re sure about the type):

(FCustomTableRow)YourRowBase

No overload accepts argument type of FTableRowBase. Only accepts subclasses, WeakObjectPtr and Linkers i think?

Yeah I attempted that but no suitable conversion supposedly.

Also tried making it - which compiled:

FCustomTableRow* ChildRow = (FCustomTableRow*) &BaseRow;

But filled data was gibberish.

And are you sure your struct actually inherits from FTableRowBase?

Yeah, they are used in DataTables and just getting the row itself works fine, but casting it doesn’t seem to work regardless of method that I’ve tried so far.

USTRUCT(BlueprintType)
struct FAmmoRow : public FTableRowBase
{
//data here
}

I tried it out in C++, seemingly the FindRow function has a template, so you can use it like this:

dt->FindRow<FAmmoRow>(...)

It should return the corresponding type. You might also use GetAllRows the same way.

1 Like

Yeah I’m already using that but what i’m trying to do is create a function that might retireve a row from one of many databases (each ‘item type’ has its own database - “legacy” issue so can’t rework it at the moment).

I’m trying to create a function that would always return the basic type, which I would then cast to correct struct when using, but maybe it would work if I make it a template function instead (however that makes it unusable in BPs so its pretty much same as above.

Anyway, thank you for the help so far, if nothing else it gave me something to think about, just thought that there should be an easy way to cast derived structs but I guess it isn’t that easy at the moment.