- Home /
Too subjective and argumentative
Trading and cargo management data structures
Woo for my first question!
I'm experimenting with setting up an automatic trading network in my game, where trade ships load up with a cargo produced at a port, check for the best price of it at nearby ports, go there to sell it and repeat.
My trouble is I don't know the best way to hold the inventory at each port (assuming each port has a full list of items in varying levels of demand, and one item it produces for sale), and how to get the ship to be able to check the inventories.
At the moment I've got the inventory as a list of keyed items; item name then attached values paired with a keyword:
public var items = [];
//load each inventory item
items['Food'] = {
'obj' : Resources.LoadAssetAtPath("Assets/InvItems/Food.prefab", typeof(GameObject)) as GameObject,
'price' : 5, //intrinsic value of item
'buyFactor' : 0.9,
'sellFactor' : 1.1,
'desiredStock' : 1000,
'currentStock' : 1000
};
The issue is when I try to query it in my trader script, I don't know how to match the name of the cargo the ship is carrying against the inventory list:
public var produce : GameObject;
private var docks;
function CheckDock() {
var productName : String = produce.gameObject.name;
if (docks == null)
GameObject.FindGameObjectsWithTag("TradeDock");
for (var dock : GameObject in docks) {
var dockInv = dock.GetComponent(InventoryManager).items;
for (var name : String in dockInv) {
if (name == productName) {
var productRef = dockInv.name; //but of course, 'name' isn't a value in the inventory, so it throws up an error.
}
}
}
Obviously not completed, but the key point there is how can I get the key with the produce name? Is it possible to use a string in this way? Or am I going about it the wrong way - any alternative would be worth considering, if it retains the functionality.
Looks like a good question for the Scripting forum. Your code looks like an attempt to turn disgusting web JavaScript into code that will compile for Unity usage. You should look into learning some new object-oriented paradigms; LINQ can likely turn CheckDock into a single line of code.
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
I'm trying to add items to an inventory. 1 Answer
A node in a childnode? 1 Answer
Inventory Item preview 1 Answer
Problem with script 1 Answer