Abstraction and Interfaces
So I'm having trouble with abstraction in UE4 and c++. In c# I would use an interface and pass around objects as (e.g.) IAbility which would allow functions to take IAbility as a parameter and call any implemented members of the object on whatever derived class happened to be passed in.
So if a character had a list of IAbility, any class that implemented IAbility would be able to be placed in the container.
Then I could access a member of the list and call any function implemented from IAbility and it would call that function from the object's concrete type.
So far the closest I've gotten in UE4 is TSubclassOf and having to get and cast the default object that comes back from that. I'm looking into c++ interfaces but UE4 seems to really not like using interfaces for whatever reason.
Which also needs to be cast back to (or have the cast stored) the original type in order to use it. This causes a loss of abstraction as I won't/shouldn't know the concrete type when I'm trying to generically handle abilities.
In short, I'm looking for a way to take an abstract class or interface and use the derived class members without casting it back to the original type. I don't know if this is something that requires a workaround from c++ or UE4 but I haven't been able to figure it out yet. Any guidance would be appreciated and I'd be happy to clarify anything that might have been confusing as I know this was a wall of text. I feel like I'm missing something obvious, so sorry if it is.
tl;dr Is there a way to use abstract classes or interfaces generically or do you have to explicitly cast everything.
the container can hold Actors or Objects, then when you try to place an item in the container, you can check if it implements the interface by casting to the interface. this will give you a huge list of all actors in the editor to choose from, and i don't know of any way to narrow it down to subclasses that implement the interface.
any class members that you want to know about generically, you should have an interface function that returns those values, and you need to set up the interface function on each class that implements it, so that they return the proper values when asked through the interface function. an interface does not give you access to all members of a class, only values relevant to the interface is accessible through the return values of the interface functions.
so iAttack has a GetAttackStats function, and it returns a struct filled with Damage,Speed,Accuracy,etc... and a weapon can implement iAttack, and in its version of GetAttackStats, it can return its relevant values. but once you have a character and a weapon each implement the iAttack interface, and you want to put both in the same list, you will have to use a list of Actor, because that is the closest common ancestor between weapons and characters... but why would you ever need to be so generic that characters and weapons show up in the same list? is this list displayed to the player on the same menu page or is there some internal use for this list?
if you were more specific in explaining what you are trying to do from a gameplay/players perspective, rather than explaining the tool you want to use to achieve generic communication, maybe we can find a better way to organize your data so its generic enough and easy for designers/players to use. so if you describe how an ability is used in gameplay, it would be easier to help you design the flow of information. it may turn out that you can separate abilities as components of the things that own the abilities, or keep them completely separate, and you may never actually need to list all things that can have abilities in the same list, depending on the design of the game. your design may not require things to be as generic as you are trying to make them, and there could exist a more user friendly approach that works for your specific needs.
so what are you actually trying to make? is this a turn based attack menu? or some kind of RPG team roster? does this array represent options a player would select in a menu?
answered Oct 19 '16 at 03:03 AM
Follow this question
Once you sign in you will be able to subscribe for any updates here