A Data Driven Factory
- I am still not happy with my factory.
- I don't like editing things in game
- Especially creating large lists.
- Like the different magic items I might have.
- I added the Megascans Vegetables pack to my project
- They were free last year so I grabbed them.
-
- I have had several people tell me that you should grab the free content each month.
- They are $5.00 right now, but you don't need them.
- I have no artistic talent, so I have several of these that I use.
- So I am going to create a data driven factory.
- This will rely on a CSV file that lists
- The Name of the item.
- The Description of the item.
- The price of the item
- The Type (EN_PickupType) of the item
- The Path to the mesh
- The path to the material the mesh uses.
- I did this in excel.
-
- Make sure that there are column headings
- Spelled correctly.
- I also ran the spell checker, but perhaps not enough.
- Adding the Mesh and Material Data
- Just find the asset
- And right click
- Select Copy Reference
-
- The save-as csv somewhere in the project area
-
- A data table
- Unreal has a tool to read csv files directly into an array of structures.
- This is called a data table.
- To star with, It needs a struct
- So I built a ST_VeggieList
- Note it contains a Static Mesh and a Material Instance.
-
- Next I created Data Table
Veggie_Data
- A Basic Reference
- Right click in the content browser and select Miscellaneous -> DataTable
-
- It wants to know the associated struct, so give it the one your just created.
-
- You will need to associate the csv file with this object, so click on Data Table Details
-
- and browse to the file
-
- You may need to click on reimport
- And you certainly must do this every time you change the file.
-
- Now we need to add the new type, BP_Pickup_Veggie
- Three variables
- A Name and Description of type String
- A Price of type int.
- A fairly standard Pickup function
-
- I added a
SetupVeggie
function to set all of the values.
- This takes a ST_VeggieList as input
- And returns the veggie for convenience.
-
- I added this type to the enumerator BP_PickupType
- Which instantly broke the factory
- I needed to update the ClassListArray (I dislike this)
- And all of the switches and selects had a new, unconnected item.
- Since constructing a veggie is complex
- And a new factory is the right way to do this, I added a veggie factory.
- I really should have a base class ItemFactory that I am deriving these all from.
- But I did not do that
- So I just created a new BP_VeggieFactory
- Two Functions
- NewVeggie
- GetRandomVeggie
-
- Note, this calls GetDataTableRowNames
- Which returns the names of the rows in an array.
- Silly me, I found the Random ArrayItem function
- And the Get Data Table Row function
- I added a reference to one of these factories and created it in the main factory
- And called it in the NewItem method
- Finally,
- I couldn't stand the bug introduced when a new enumerator is added
- So I added a check at the end of begin play in the base factory
- And a warning in the enum list.
- I made two more "stores"
- One that asks for an item from a list of items.
- One that places items in fixed positions and restocks after the player exits.