I am working on an inventory system right now using a method explained in this Youtube video:
What he basically does is he creates a script to be a class thats is used in the list on another script, so each item on the list is made up of all the variables in the class. Might be a little confusing, so here is some code that might make it easier to understand:
This is an example class script:
#pragma strict
enum RarityEnum { Common, Uncommon, Rare }
public class Item {
var name : String;
var damage : int = 0.0;
var rarity : RarityEnum;
}
This is an example list script:
import System.Collections.Generic; // Necessary.
var inventory : List.<Item> = new List.<Item>();
I am not too familiar with with lists, but so right here, every element on the list, has all of the variables the class has. Is there an easy way to ask something of all items in the list? I know in C#, there is ForEach, but I have read that that is bad to use, and I don’t really use C# anyway.
So, an example I would kind of like to have happen, would be something like:
if(inventory[ ANY/ALL ].damage == 2) {
// Do Something Here.
}
I have just figured out a way to tally up all of the damage from the items in my list (which was hard) and it was rather unpleasant to do. I really don’t wan’t to have to do this for everything:
//********************************************************
// HP Stats
appliedStats.hp = weaponHpX +weaponHpY +weaponHpZ + itemHpX + itemHpY + itemHpZ;
// ===========================
if(equipped.weapon.Count >= 1) {weaponHpX = equipped.weapon[0].itemStats.hp; } else {weaponHpX = 0; }
if(equipped.weapon.Count >= 2) {weaponHpY = equipped.weapon[1].itemStats.hp; } else {weaponHpY = 0; }
if(equipped.weapon.Count >= 3) {weaponHpZ = equipped.weapon[2].itemStats.hp; } else {weaponHpZ = 0; }
if(equipped.item.Count >= 1) {itemHpX = equipped.item[0].itemStats.hp; } else {itemHpX = 0; }
if(equipped.item.Count >= 2) {itemHpY = equipped.item[1].itemStats.hp; } else {itemHpY = 0; }
if(equipped.item.Count >= 3) {itemHpZ = equipped.item[2].itemStats.hp; } else {itemHpZ = 0; }
All of that just added up the damage from three possible weapon and item slots… Right now, I want to have a boolean on the class script: var equipped : boolean = false but I don’t want to go down the list of each element:
if(equipped.weapon[0].isEquipped == true) { __________; }
if(equipped.weapon[1].isEquipped == true) { __________; }
if(equipped.weapon[2].isEquipped == true) { __________; }
....
I don’t know, I guess overall, is there an easy way to ask something of every item in a list?
I am kind of new to programming just so you know.
Thanks!