Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Jun 17, 2013 at 01:20 PM by Jessy for the following reason:

Too subjective and argumentative

avatar image
0
Question by Ninjars · Jun 17, 2013 at 01:15 PM · listinventory

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.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Jessy · Jun 17, 2013 at 01:27 PM 0
Share

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.

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

15 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges