I want to put a generic baseclass into my game for game items.
They have an id, a text representation, a picture widget thing etc…
Now if id create my game from scratch without ue4 deriving from “MyGameItemBase” having those properties would be one possible solution, this doesnt work here tho.
Now I have heroes in my game. Heroes are derived from Characters.
Currently I made the bad choice of deriving “MyGameItem” from actor and having a hero as member variable and with it an item type (enum) that can be hero…
So I ask game item for the type and if its hero I know I can get the hero from it…
Now if I want to add different game items (lets say artifacts, wepaons or cards whatever) I need to add them as property… this is bad.
One better way would be to use an interface like: IGameItem. All the interface would do tho is implementing getters and setters for some properties.
Does anyone have an idea how to nicely solve this? (I must also be able to put the items into an array and so forth)
An Interface is the thing you want to use when you have Classes with different BaseClasses, but you still want to blindly call a function on them.
So if you have different Actors/Objects that should do something on a specific function call, then you would simply add the Interface and let them implement the functions.
So yes, this would be the way I would solve it.
You kinda only have the two ways of either having the same BaseClass for them and having the functions in the BaseClass + letting Children override them.
OR
Using the Interface mentioned above.
I want to notice here though, that I didn’t completely understand your current setup.
Hm, I think you would be better of creating a Struct with all your data (ID, Name, etc) and then creating a Variable of that Struct in your Character.
And for the others you might want to create a new BaseClass, add a Variable of that Struct to it and REPARENT the MyGameCard Class from Actor to this new one.
Is there a reason why you can’t just create a new BaseClass as said above and reparent your Actor Children to that?
Reparenting should be under “File->Reparent Blueprint” inside of the Blueprint itself.
how is my character a game object if i put some properties on it? it can not be identified/cast as such and can not be put into an array together with the other game objects…
The other ones are already children of MyGameItem It just doesnt work with characters wich is a slight annoyance and i hoped to overcome this with typical ways like interfaces or some nice pattern bgut it wont work in ue4
I think ill just derive a herobase from gameitem and give it a member hero wich derives from character with a reference to the herobase to get to the game item properties…
You’d combine the Character and BaseClass changes with the Interface and write generic get and set functions that you implement on Items and Heros.
Then you can save them in a generic “Actor” array and call the interface message on it.
There might be no easier way as your Character derives from APawn and your Items from AActor.