Prototype pattern using templates in UE4?

Hello!

I am working on thesis for my bachelor degree and I need Your help with creating the weapon system in my game.
As I read a book Game Programming Patterns (by Robert Nystrom), it seemed very good idea to use prototype pattern there.

In the example in his book there are spawners that have clone() function and they clone based on the enemy prototype. As I was thinking, the idea is basically the same - I have the weapons, they have shoot() function and they clone based on the bullet prototype. Although the pattern seems clear enough, I have problem implementing it.

So the idea would be creating a weapon instance: Weapon* weapon = new Weapon(weaponsprite, bulletprototype). This is not correct code, so please don’t pay attention to it.

First of all, the constructors - as I read about it, the constructors are called when I use the CreateDefaultSubobject with my class template. I created a function Init() with the arguments I need to initialize the weapon.
Secondly, I wanted to make my class generic using class templates (so whatever bullet I add between the <> I could create a weapon based on that bullet type). However, as I read this is not possible aswell. One solution was to create an abstract base class for UObject (so it would be possible to use templates), but then there will be problem with overriding functions.

Maybe I miss something crucial and could do this alot easier. It would be great if you share your knowledge about this and maybe I would be able to overcome this problem.

Thank you in advance
Waiting for you reply!

1 Like