- Home /
Game Logic - How to Check if player has enough Coins before Instanstiating a House?
Okay - Currently, a Player runs around, and collects Coins, timber, etc... Its populated on a GUI (sort of like an Inventory system) It places each object (which are stack able) into the Inventory box.
What i want to do, is when the player goes to another GUI area, to say - Build House, or Plant Tree, - The Game checks to see if it has X available Coins. If not, it flashes a. YOU CANT DO THAT. if it does, then it allows me to Spawn a Prefab to the location of the players choice.
Question: How do i make it check to see whats available in the inventory?
Any scripts would be fantastic. something i can dissect and pick apart, to learn the inner workings, of course, i am completely happy with a "Here is a 200 line Script that does just that! written for you! for free! drop it on X object" :)
You have created an entire Inventory system but can't figure out how to check it's ArrayList contents?
He never said he was using an array list and honestly, he didn't say how is inventory system worked or if it is even set up in a way that would promote a good coding practices.
Answer by Peter G · Jul 18, 2010 at 12:28 AM
I'm not going to write a functional code for you. I will write a code missing your data, so that all's you have to do is plug it in. This way, you have to read through it, but have a guide.
Make sure your inventory system is setup in such a way that this won't be too hard. Your inventory system should include variables for the current amount of all your resources. If this isn't setup, determine how you want to add to your counter, maybe
OnCollisionEnter () { Resource++; }
Set up your box to grab the inventory script then read the variable whenever it is needed. Not sure what GUI you are using. It will work very similarly no matter which.
var inventoryScript : #NameOfInventoryScriptHere#;
function Start () {
FindObjectOfType(#NameOfInventoryScriptHere#); //needs cast in C# unless generic
//cache a link to the inventory.
}
function OnGUI () { //You could use GUI layout also if(GUI.Button(#buttonArguments#)) { //create a button to respond if the user has enough of a certain resource. //this button might check gold and another might check food or something.
if(inventoryScript.resource >= certainValue) {
//see if we have enough of that resource
inventoryScript.resource -= certainValue;
CreateObjectAssociatedWithButton();
}
}
}
function CreateObjectAssociatedWithButton () { }
Very nicely. I appreciate the code Peter G.
$$anonymous$$Y Inventory system has variables, tied to each unique object. The GUI spits out the number of For instance Gold coins stacked in the "Gui Box" or any other item that is "stackable"
I understand your code, and it was exactly was what i was looking for. Im not a programmer in any way. I take full scripts, and go through them figuring things out, modifying, or rewriting what i learned. (Best way i know how to learn)
Thanks again :P
Your answer
Follow this Question
Related Questions
How to display textures that are in an array? 1 Answer
tooltip to show from array C# 1 Answer
Item HUD help 0 Answers
Toggle GUITexture V.2 1 Answer
Hotbar and item pickups? 1 Answer