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 /
avatar image
0
Question by chris gough · May 04, 2012 at 04:59 PM · javascriptguiinventorycrafting

Crafting system

I have created an inventory system in js using dictionary definitions and guis but I cant figure out how to work this in with a crafting system, so if you know any good tutorials or over people that have a working crafting system plz link it ...

I have put the script below for the inventory so you have an idea of what I am using, I have also placed an image below too show how I want the inventory to work.

 //----------
 
 var customSkin   : GUISkin;
 var Player       : Transform;
 var weaponHolder : Transform;
 var playerCamera : Camera;
 static var itemNum      : int;
 static var gotItem      : boolean;
 static var usingPick    : boolean;
 static var inInventory  : boolean;
 static var stackItem    : boolean;
 static var items = new System.Collections.Generic.Dictionary.<String,GameObject>();
 //Create the dictionary full of a name and a GameObject associated with it.
 static var curItem : System.Collections.Generic.KeyValuePair.<String, GameObject>;
 static var curItemKey;
 static var curWeapon : GameObject;
 static var destroyWeapon : boolean;
 //This will store the current item.
 
 public var buttonHeight = 50;
 public var width = 100;
 
 function AddItemToInventory (item : GameObject) {
     items.Add(item.name, item);
     itemNum++;
     Destroy(item);
 }
 
 function OnGUI() {
     var curIndex = 0;
     GUI.skin = customSkin;
     GUI.BeginGroup (new Rect (400, 500, 400, 120));
     for(var item : System.Collections.Generic.KeyValuePair.<String, GameObject> in items) {
         //iterate through all the items in the dictionary.
         var rect : Rect = new Rect((width+2) * curIndex, 0, width, buttonHeight);
         var customStyle  : GUIStyle;
         var itemModel    : GameObject;
         var itemIndex    : int;
         var btnToggled   : boolean;
         var crafting     : boolean;
         var stacking = GameObject.Find(item.Key+("_model")).GetComponent(stacking);
         itemIndex = curIndex;
         GUI.skin = customSkin;
         customStyle = customSkin.GetStyle("icon_"+item.Key);
         btnToggled = GUI.Toggle(rect, btnToggled, curIndex+1+")       x"+stacking.stackAmount, customStyle);
         if(Event.current.Equals (Event.KeyboardEvent (curIndex+1+""))) {
             curItem = item;
             curItemKey = curItem.Key;
         }
         if (curItem.Key == item.Key){
             itemModel = GameObject.Find(curItem.Key+"_model");
             btnToggled = true;
             GUI.Toggle(rect, btnToggled, curIndex+1+")       x"+stacking.stackAmount, customStyle);
             CreateWeapon(itemModel);
         }else{
             btnToggled = false;
             GUI.Toggle(rect, btnToggled, curIndex+1+")       x"+stacking.stackAmount, customStyle);
         }
         curIndex++;
     }
     GUI.EndGroup();
 }
 
 function CreateWeapon(weaponModel : GameObject){
     if (curWeapon){
         Destroy(curWeapon);
     }
     curWeapon = Instantiate(weaponModel,weaponHolder.position,weaponHolder.rotation);
     curWeapon.transform.parent = weaponHolder.transform;
     gotItem = true;
 }
 
 function Update(){
     if(curWeapon){
         var stacking = GameObject.Find(curItem.Key+("_model")).GetComponent(stacking);
     }
     if (itemNum < 0){
         itemNum = 0;
     }
     if(gotItem && Input.GetKeyUp("q")){
         if(stacking.stackAmount >1){
             stacking.stackAmount--;
             ObjNew(curWeapon);
         }else{
             stacking.stackAmount--;
             ObjNew(curWeapon);
             itemNum--;
             RemoveItem(curItem);
             destroyWeapon = true;
             gotItem = false;
         }
         if (curWeapon && Input.GetMouseButton(0)){
             swingWeapon(curWeapon);
         }
     }
     if(destroyWeapon){
         Destroy(curWeapon);
     }
     if(!curWeapon){
         destroyWeapon = false;
     }
 }
 
 function ObjNew(newItem:GameObject){
     var object = Instantiate(newItem,weaponHolder.position,weaponHolder.rotation);
     object.name = curItem.Key;
     object.transform.parent = null;
     object.AddComponent(Rigidbody);
     object.AddComponent(BoxCollider);
     object.AddComponent(Interaction);
 }
 
 function swingWeapon(weapon : GameObject){
     weapon.animation.Play("use");
 }
 
 function RemoveItem (item : System.Collections.Generic.KeyValuePair.<String, GameObject>) { 
     yield WaitForEndOfFrame;
     items.Remove(item.Key);
 }

i realise it is a lot of code but the main bit us under the OnGui function, thanks in advance for the help and I realise I am asking a lot, so I really appreciate the help

alt text

Crafting Image.jpg (133.4 kB)
Comment
Add comment · Show 6
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 Jacek_Wesolowski · May 04, 2012 at 07:14 PM 0
Share

Could you define a use case or a usage pattern that you would like to implement? (i.e. an example of how the system is supposed to work: "the player does this, this and this, and this is what happens")

avatar image chris gough · May 04, 2012 at 08:30 PM 0
Share

the idea is that the player will put items onto the crafting table, say a log and an axe head and make a weak axe, but I also want it so that if they put a log, axe head and another log, it would show everything the could be made and when they select it the resources that it requires disappear. so in the image on the left thats where the items would be placed and on the right that would show the list of what they can make

avatar image Bunny83 · May 04, 2012 at 08:43 PM 0
Share

Are you familiar with the $$anonymous$$ecraft crafting table? Usually it's better to require the user to put the ingredients in a certain pattern. This allows much more combinations. Like @jacek-wesolowski said some kind of use-case would be helpful how you imagine the system should work. How does a crafting recipe look like? Do you want a pattern or not. How do you identify an item. By some kind of ID or via name?

avatar image Bunny83 · May 04, 2012 at 08:50 PM 0
Share

btw. Usually when you use classes from a namespace frequently you should import that namespace to avoid those long classnames.

I don't use UnityScript, but it should be:

 import System.Collections.Generic;

at the top. In C# it's

 using System.Collections.Generic;
avatar image chris gough · May 04, 2012 at 09:00 PM 0
Share

With the crafting system I want it like $$anonymous$$e craft where you interact with the table to bring up the crafting UI, but more like terraria for the crafting where it shows you a list of items that you can make with your current items, but you have to place items onto the crafting UI first for it too show you what can be made. Its this way because some recipes produce multiple items.

also thanks for the coding advice as I am a bit of a noob to it.

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
-1

Answer by Jacek_Wesolowski · May 04, 2012 at 09:41 PM

This isn't a very tough problem in algorithmic terms, but it is fairly involved. I think it would be best to implement the solution in stages, so that you can ask specific questions about those, should you run into trouble. Otherwise, it's going to be too big a fish to swallow, so to speak. Try meeting the following criteria, one at a time.

You need an inventory of items.

You need to be able to display the inventory.

The player needs to be able to select an item. The selected item needs to be marked as such.

You need a recipe for a crafted item. It needs to specify the components and the result. There may be a varying number of components, but only one result. You're going to have to decide if there's an absolute maximum number of potential components in a recipe (if there is, then the implementation is going to be easier).

You need a collection of recipes. You're going to be searching it a lot, and you want to do that efficiently, so this piece is going to require a bit of thought.

You need a crafting table, which is basically a set of slots.

You need to be able to display the table (i.e. all of its slots).

When an item from the inventory is selected, it needs to appear on the table, in the first free slot (which you need to find automatically, unless you want to create a drag and drop interface for the player to do it manually).

The items on the table need to be marked as such (eg. displayed "on the table").

The player needs to be able to unselect the item. When that happens, the item needs to disappear from the table and the slot needs to be made free.

Every time the contents of the table change, you need to search the recipe list against the contents, in order to find all matching recipes. There are several way to do this, some of which are smarter than others, but simply going through the entire collection and checking each entry against the contents of the table will do at first.

You need to be able to display the matching recipes.

The player needs to be able to pick one recipe.

When player picks a recipe, the items that match recipe's components need to be removed from the inventory and from the table. The resulting item needs to be put in the inventory (note that this affects the following: the items being selected, the contents of the table, and, by extension, the list of matching recipes).

Comment
Add comment · Share
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
-1

Answer by TheAwsomeMan · Feb 02, 2013 at 09:44 AM

why mine when i create it on c# script it has alot of errors : like this -->

already change the using System.Collections to generic; it is still wrong. will some help me?

using UnityEngine; using System.Collections.Generic;

var customSkin : GUISkin; var Player : Transform; var weaponHolder : Transform; var playerCamera : Camera; static var itemNum : int; static var gotItem : boolean; static var usingPick : boolean; static var inInventory : boolean; static var stackItem : boolean; static var items = new System.Collections.Generic.Dictionary.(); //Create the dictionary full of a name and a GameObject associated with it. static var curItem : System.Collections.Generic.KeyValuePair.; static var curItemKey; static var curWeapon : GameObject; static var destroyWeapon : boolean; //This will store the current item. public var buttonHeight = 50; public var width = 100; function AddItemToInventory (item : GameObject) { items.Add(item.name, item); itemNum++; Destroy(item); } function OnGUI() { var curIndex = 0; GUI.skin = customSkin; GUI.BeginGroup (new Rect (400, 500, 400, 120)); for(var item : System.Collections.Generic.KeyValuePair. in items) { //iterate through all the items in the dictionary. var rect : Rect = new Rect((width+2) * curIndex, 0, width, buttonHeight); var customStyle : GUIStyle; var itemModel : GameObject; var itemIndex : int; var btnToggled : boolean; var crafting : boolean; var stacking = GameObject.Find(item.Key+(" model")).GetComponent(stacking); itemIndex = curIndex; GUI.skin = customSkin; customStyle = customSkin.GetStyle("icon"+item.Key); btnToggled = GUI.Toggle(rect, btnToggled, curIndex+1+") x"+stacking.stackAmount, customStyle); if(Event.current.Equals (Event.KeyboardEvent (curIndex+1+""))) { curItem = item; curItemKey = curItem.Key; } if (curItem.Key == item.Key){ itemModel = GameObject.Find(curItem.Key+"_model"); btnToggled = true; GUI.Toggle(rect, btnToggled, curIndex+1+") x"+stacking.stackAmount, customStyle); CreateWeapon(itemModel); }else{ btnToggled = false; GUI.Toggle(rect, btnToggled, curIndex+1+") x"+stacking.stackAmount, customStyle); } curIndex++; } GUI.EndGroup(); } function CreateWeapon(weaponModel : GameObject){ if (curWeapon){ Destroy(curWeapon); } curWeapon = Instantiate(weaponModel,weaponHolder.position,weaponHolder.rotation); curWeapon.transform.parent = weaponHolder.transform; gotItem = true; } function Update(){ if(curWeapon){ var stacking = GameObject.Find(curItem.Key+("_model")).GetComponent(stacking); } if (itemNum < 0){ itemNum = 0; } if(gotItem && Input.GetKeyUp("q")){ if(stacking.stackAmount >1){ stacking.stackAmount--; ObjNew(curWeapon); }else{ stacking.stackAmount--; ObjNew(curWeapon); itemNum--; RemoveItem(curItem); destroyWeapon = true; gotItem = false; } if (curWeapon && Input.GetMouseButton(0)){ swingWeapon(curWeapon); } } if(destroyWeapon){ Destroy(curWeapon); } if(!curWeapon){ destroyWeapon = false; } } function ObjNew(newItem:GameObject){ var object = Instantiate(newItem,weaponHolder.position,weaponHolder.rotation); object.name = curItem.Key; object.transform.parent = null; object.AddComponent(Rigidbody); object.AddComponent(BoxCollider); object.AddComponent(Interaction); } function swingWeapon(weapon : GameObject){ weapon.animation.Play("use"); } function RemoveItem (item : System.Collections.Generic.KeyValuePair.) { yield WaitForEndOfFrame; items.Remove(item.Key); }

Comment
Add comment · Share
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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

7 People are following this question.

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

Related Questions

How to display textures that are in an array? 1 Answer

Script Not Working 0 Answers

Real Simple Inventory and Vendor System Help 1 Answer

loop index error? JS 1 Answer

Basic Crafting system? 0 Answers


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