- Home /
[SOLVED] First array slot blocking second array slot
Hello everyone,
Am having a weird little issue. In my pause menu I want to display the number of items the character has, but if the number is 0, then I want the item to not appear. so if "Health Potion = 1" 1 health potion would appear in the inventory, but if health potion = 0, then the line disappears. Same goes for bread, (hard tack) water, etc. Things were actually working until I added a second item. (first, if you want to get technical, array slot 1 worked fine, did that first, then went back and added array slot 0. The problem is that array slot 1 seems to be tied to slot 0 somehow, meaning that I can have 10,000 hard tack crackers, but they won't appear in my inventory guitext until I pick up 1 health potion. (array slot 0) Further, once I've picked up a health potion, I can never use the last one, meaning that the player needs 2 or more at all times, and if they have 1, (or less) then they can't use anything out of slots 0 or 1. I figure it's an issue with line placement in the code, but thought I'd throw it out to the community while I bang my head against the keyboard. Thanks, and God bless.
//inventory
static var inventoryArray : int[] = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
var inventoryText : GameObject;
var inventoryText2 : GameObject;
var inventoryText3 : GameObject;
var inventoryText4 : GameObject;
static var PrisonKey : int = 0;
static var TempleKey : int = 0;
static var HealthPotion : int = 0;
static var HardTack : int = 0;
static var Water : int = 0;
static var AppleBrew : int = 0;
//inventoryText.guiText.text = "Health Potion " + "[" + inventoryArray[0] + "]" + "\n" + "Hard Tack " + "[" + inventoryArray[1] + "]" + "\n" + "Water " + "[" + inventoryArray[2] + "]" + "\n" + "Apple Brew " + "[" + inventoryArray[3] + "]" + "\n";
function Start () {
inventoryText.guiText.enabled = false;
inventoryText2.guiText.enabled = false;
inventoryText3.guiText.enabled = false;
inventoryText4.guiText.enabled = false;
}
function Update () {
if(inventoryArray[0] > 0) {
inventoryText.guiText.enabled = true;
inventoryText.guiText.text = "Health Potion " + "[" + inventoryArray[0] + "]" + "\n";
if(inventoryArray[0] > 0) {
if(Input.GetButtonDown("Stuff"))
healthPotion();
}
if(inventoryArray[1] > 0) {
inventoryText2.guiText.enabled = true;
inventoryText2.guiText.text = "Hard Tack " + "[" + inventoryArray[1] + "]" + "\n";
if(inventoryArray[1] > 0) {
if(Input.GetButtonDown("Sword Slash"))
hardTack();
}
if(Input.GetButtonDown("Jump"))
if(Water > 0) {
water();
}
}
}
}
function healthPotion () {
Playerhealth.curHealth += 15;
inventoryArray[0] -=1;
HealthPotion -=1;
}
function hardTack () {
Playerhunger.curHunger -= 5;
inventoryArray[1] -=1;
HardTack -=1;
if(inventoryArray[1] == 0) {
inventoryText2.guiText.enabled = false;
}
}
function water () {
Playerthirst.curThirst -= 100;
Water -=1;
}
Item Pick up Script (Health Potion)
var chestSound : AudioClip;
var treasureChest : GameObject;
function OnTriggerEnter (col : Collider) {
if(col.gameObject.tag == "Player") {
Inventory.inventoryArray[0]++;
Inventory.HealthPotion++;
AudioSource.PlayClipAtPoint(chestSound, transform.position);
treasureChest.animation.Play();
Destroy(gameObject);
}
}
not using arrays
You need to use arrays (or some sort of collection); it's not really an option, if you want your coding to be even remotely sane.
ok, have went the array route, but now with a new wrinkle tripping me up.
Answer by fafase · Oct 26, 2013 at 11:51 PM
Your code is not so well indented but I feel your second check, the one for inventory[1] is within the first which makes sense why you need one potion to see your crackers.
The reason why you cannot use the last one in your inventory is because you use:
if(inventoryArray[0] > 0)
and the Input is inside of that. To use down to 0 you need:
if(inventoryArray[0] >= 0)
By the way since you have all items as variables, there is no real need for the array. You should pick one or the other.
Yeah, I could definitely do better at indenting. As I'm learning code I haven't yet gotten to just doing that. Is there a rhyme or reason, or is it for ease of reading? Thanks on the >=, that makes sense. ah, didn't realize 1 was inside 0's array. Newb here, just trying to make my way :)
As for the array / static vars. I agree. I would love to do it one or the other. The reason I did it this way is actually Playerprefs. Had the hardest time trying to figure out how to save from an array, even tried using something called "PlayerprefsX" and it never worked, though I could save static vars, so did both; the array for displaying not only the Name of the product, but also the quantity, and the static var so the items could be carried over between game sessions.
In C# and most languages, the indentation just helps you read. See in your case, indentation would have told me right away that the second if is nested.
In some languages like Python and Boo, indentation is the code. There is no {} and you use indentation ins$$anonymous$$d
if(something):
Action();
ok, I have a stupid question. Really stupid, newbish question. How do I unnest the hard tack call, do I take it completely out of update and make it an entirely new function?
Nested:
if(something){
if(somethingElse)
{}
}
Not nested:
if(something){
}
if(somethingElse){
}
It is all about closing the matching braces
Ok here is what you should have:
function Update () {
if(inventoryArray[0] > 0) {
inventoryText.guiText.enabled = true;
inventoryText.guiText.text = "Health Potion " + "[" + inventoryArray[0] + "]" + "\n";
if(Input.GetButtonDown("Stuff"))
healthPotion();
}
if(inventoryArray[1] > 0)
{
inventoryText2.guiText.enabled = true;
inventoryText2.guiText.text = "Hard Tack " + "[" + inventoryArray[1] + "]" + "\n";
if(Input.GetButtonDown("Sword Slash"))
hardTack();
if(Input.GetButtonDown("Jump"))
if(Water > 0)
{
water();
}
}
}
But here is also what you are missing:
function healthPotion ()
{
Playerhealth.curHealth += 15;
inventoryArray[0] -=1;
HealthPotion -=1;
inventoryText.guiText.enabled = false; //Here
}