Download

Random item generation design pattern

Hi,

I’m working on a (loot) game and want to generate random loots like in Diablo, POE, etc…

I’m currently struggling with the “good” design pattern to adopt to achieve this in blueprints, I almost get it working but I don’t think my design choices are that good.

Here is what I’ve done so far:

  • Actor Blueprint: BP_Item (base item configuration)
  • Actor Blueprint: BP_Weapon (inherit from BP_Item) (base weapon item configuration)
  • Blueprint function library: LIB_Loots (functions to generate random loots)
  • DataTable: DT_ItemsTypes (one handed weapon, potion, shield…)
  • DataTable: DT_ItemsQualities (common, rare, magic…)
  • DataTable: DT_Items (Sword, Great Axe, Health Potion…)

So in my “GenerateLoot” function (from LIB_Loots) I do some stuff to “choose” the loot and then I get the “Blueprint Class” I have to create (BP_Weapon for exemple) and here my problem (for now)

Based on the class’s name, I don’t know how to Instantiate the objet (don’t need to spawn it) I first look at “Create Object” node, but it seems to work only for blueprint inherited from Object class (and I read that you can’t use a construct in it ? oO) and my blueprints are inheriting from Actor (here is maybe a mess to start with)

Then I tried to use “Spawn Actor From Class” node, but it seems that I can’t use it in my “LIB_Loots” (blueprint function library), either in my Blueprint actors

To “solve” my problem I need Blueprints acting like Object (but with a construct, as I will generate the random attributes in it) but I don’t know how to do it.

Finally, maybe my “setup” is in the wrong way (and that’s ok), if so do you have any suggestion ?

Thank you for reading !

There is an outstanding request for prefabs, but I’m not sure when those will be available. It might be easier to create the object from scratch at runtime. I have made some experiments with components. Just spawn an empty object and then add components as needed.

This seems related to the unresolved question we have about how to access information on an unspawned component (link in signature).

It seems at the moment that an object must be constructed first if you want to pull non-default variables from it.

Specifically for your item generation, have you considered using a factory style pattern? You pass in all the info for your data table generated loot drop, and it constructs and returns a reference to the correct object. Passing in the data as a struct would probably help to keep it tidy (pictured is just a single integer).

1b95860d110d5c6b0f9cda8dd502e82ab3cab4d7.png

I see no reason why you make items from AActor class.
The pick up item to drop on world is just a visual container anyway.
You can use UDataAsset to build item types; There are other ways (I create Item objects like they were assets in Asset Browser, not blueprints, but that is way too much complicated to explain here), but UDataAssets are the thing for base item classes…

You can mark properties as “visible on spawn” in blueprints, and then when you spawn actor from class, those properties will be visible on the spawn Blueprint node.

But, I actually agree with Bruno; it’s better to keep the actor lightweight, and use some kind of struct/data asset to describe the variation.
Plus whatever custom mesh you generate, perhaps? Are you doing that, like Borderlands?

Thank you all for your answers !

@mbelarouci what the type and/or parent of the objects in your screenshot ? (like RR Resource Base Money), I don’t get how to make objects that I can instantiate like that

Agreed, I already have a “Pickup Actor” and just want to create item object (object as “instantiated class”, not a 3d object ^^) but I didn’t find how to create them in blueprints (I already tried Actor & Object as parent). My temporary solution today was to merge my “pickup actor” and my “item object” so I can use the spawn actor to instantiate it, and the construct to randomize the attributes, but I don’t think that’s a good idea…

How do you create a UdataAsset object ? Don’t see it when I create a new blueprint
EDIT: I found a Data Asset class in the “Miscellaneous” category, but the parent can only be “BlackboardData”, “EnvQuery”, “PaperTerrainMaterial” and “TireType”

Thank you,

They’re objects -

  • they don’t have a physical location in the world but they can store a mesh as a variable, which can then be attached to a socket.

If you want the thing you’re making to have a location in the world then you can use the same pattern with actors, though you’ll have to supply a transform for where you want it to spawn. Actors are a bit more taxing for the engine though, but it shouldn’t be a problem unless you’re spawning a massive number.

Spawn Actor From Class node, to replace the Construct node:
e5a6211ebe456294eefef3048629ca3c45114cb0.png

I tried to use the UDataAsset directly in c++ but without success (it seems I can’t use the blueprints nodes in it :frowning: )

So I create an “Item” class extending Object (like in your screenshot) and so far so good. Unfortunatly it seems that there isn’t any construct in it, so I created one function to replace it

Here is how I create the object:

1bbb2890e8ff23d315c64b44e5e4ee03c3c95fc8.jpeg

In the “Contruct Loot” function I will be able to generate the attributes for the loot, and spawn the “Pickup” actor (only a name + mesh) to view it in the game’s world

Thank you !