Download

Help with Data Tables

Hi,

I have an inventory, crafting and building system in my game and right now I am working on the inventory part. I have all the items that could be added to the inventory in a data table and basically I want certain items in the data table to call different actions. So basically, when you add an item to the inventory and you click on that item it will do a certain action for example if I have an apple in my inventory and I click it I want it to add a value to a float variable. The only thing I need help with is determining what item I am clicking on this way I can set up different actions for different items. Thank you.

This is not good idea;
You could build the Apple data read (once) from the DataTable as a new class/struct, then from there you already have all information needed by the Apple type, inside the struct; whenever clicked in the inventory you query the Apple struct what should it do.
Otherwise you’d have to search loop the DataTable IDs one by one to query again which one is Apple and what does it do, every click.

I’m confused lol. So your saying every item I have in my data table that has an action attached to it I must create a struct for it? Providing screenshots would be very helpful as I am still a beginner to blueprints.

This is what I have right now for the drop action.

58eaee00156e769dc274a492def1e67560cefb47.png

When I click the drop button it doesn’t do anything and when I exit the game it says accessed none trying to read property inventory. At this point I know I need to cast but the inventory variable is an inventory component reference and I don’t know how I would cast to that. Any ideas? Thank you.

You are trying to use the class instead of the object reference to do something, that’s why it’s failing. I think what Bruno is saying is that you should back up and not do it this way at all.

DataTables are fine for look ups and initializing things, but they should probably not represent what is in your inventory at runtime, that should be references on your character. If you have a base InventoryItem object that can handle drop and pick up, you can either do the OOP way and extend it with your Inventory subtypes, or use a Component design and have it contain data and methods.

Either way can be initialized from a DataTable, it is a design pattern called Factory. But whatever the way, you need the object in memory, not a field in a DataTable which looks to be your problem.

Yeah, the reason DataTables exist is because if you store Text data inside game code as strings, it increases a loooot compilation time and also it becomes a mess to debug over time…
But you don’t use them as a ‘container’, you use DataTables as a base template of information from where your classes and structs will be built from in game startup (or custom asset creation with a custom UFactory class or UDataAsset to store classes as Assets in Asset Browser). They are just a starting point to create your containers, do not use them as UObjects.

To be honest I have no idea what you guys are talking about xD. I downloaded the inventory, crafting and building system I am working with off of another forum post on here. I tried contacting him about it but, he won’t answer so I am really lost on what to do right now. I am more than 50% complete my game and this is a major part of the game so it’s not like I can just give up on this project or forget about implementing this into my game. I can send you the project if that helps.

I hope you don’t mind a related question, since I’ve been implementing something similiar lately, but how do you handle letting different objects have different categories of functionality (e.g. Eat vs. Wear) with this pattern? Ordinarily I would do this with inheritance, where a character’s inventory is an array of some FBaseItem, and each unique category of item overrides FBaseItem.Use() in a different way, but since structs can’t be cast this doesn’t seem like it would work. Yanking out the struct entirely, and doing that logic with AActors (or AInfo, or whatever) would work, but then suddenly I’m spawning and retaining dozens of actors in the world to represent item data, which feels like an antipattern.

@HInoue

A pattern has not been given, only suggested to stop doing it the way it’s currently being done. AActors aren’t required, but the objects should live in memory. It is your choice to do OOP and override a Use() function or to do component based and have a list of functions that can be selected from and assigned to a delegate.

Hmm okay, thank you :slight_smile:

You actually *can *cast Structs if you know what you’re doing, but feels dangerous to do it indiscriminately.

http://en.cppreference.com/w/cpp/language/reinterpret_cast

There is also examples of this at work inside the AI modules of UE4 source code.
Also, Items can be UObjects spawned and kept by the Inventory class, instead of going full blown Actors for each Item.