Hey everyone. This is going to sound insane but I’ve been searching EVERYWHERE for this particular tutorial I had years ago. It was a javascript tutorial but it was a lot more in-depth then what is normally offered. It started from no knowledge and moved to arrays and even tried to explain how an array can be used to make an inventory. I think it used apples, potions, and something else as the inventory example. It explained how you would declare apple in the array slot and such. I know it’s a really long shot but I’m wondering if anyone has any idea what tutorial I’m talking about. I’ve searched the internet and I was even scouring forum posts from four years ago trying to find this thing. Not being able to find this thing is driving me nuts.
var FoodArray[ ]:String;
Function Start ()
{
FoodArray = new String[2];
FoodArray[0] = “Apple”;
FoodArray[1] = “Orange”;
FoodArray[2] = “Pear”;
}
or
var FoodArray[ ]:int;
Function Start ()
{
FoodArray = new Int [5];
for ( var xiter:int = 0; xiter <6; xiter++ )
{
FoodArray[xiter] = Random.Range(5);
}
}
function Update () {
for (var xiter2 = 0; xiter2 < 6; xiter2++) {
if ( FoodArray[xiter2] == 2 ) {
doo Stuff;
}
}
}
Haven’t tested it but you should get the jist of it. Let me know if you need further help.
Which Kind of array or collection should i use
I have been using unity for nearly 3 years and I still have this open when I’m working, great little reference :).
Amon, that example was very helpful! I messed around with it for a couple hours and got rid of errors. I think I understand what is going on there. This is what I ended up with:
var FoodArray : String[ ];
function Start ()
{
FoodArray = new String[3];
FoodArray[0] = “Apple”;
FoodArray[1] = “Orange”;
FoodArray[2] = “Pear”;
}
function Update ()
{
print(“You ate an " + FoodArray[0] + " and an " + FoodArray[1] + " and a " + FoodArray[2] + " today!”);
if(FoodArray[2] == “Pear”)
{
print(“This is a pear”);
}
}
It took a lot of tweaking and experimentation but it finally works as it should. I get all the proper prints to show and there are no errors! Arrays have always been very difficult for me for some reason. There is just something about them that doesn’t click. I understand this though so that is progress.
timsk, thanks so much for that link! I had no idea there were so many different kinds of arrays. It is a little confusing to have so many but I was looking at that as well as the arrays documentation while playing with the simple array script and it was very useful. While looking at BergZurg’s game tutorial, it looks like if I want to convert the enemy selection code into javascript, I would need to use the GenericList arrays.
looks good. Yeah I wrote it without checking it, didn’t even have the IDE open; I’m glad you found it usefull though.
I’ll check out some javascript books tomorrow but before I do that, do you know if I can assign other values to the arrays? Say in the example of a typical item, an apple would recover 5 HP or something like that. Can arrays do that or is it a seperate array completely? If it’s possible then I’ll try to find that in a book somewhere.
Actually I’m not sure of that! If I were to hazard a guess I’d assume that you cannot mix types i.e. a string array will always be a string array, an int array will always be an int array.
I thin you would have to test it but I’m pretty sure it’s not possible. The best thing you could do is have a seperate var called FoodScore and change it according to what was picked from the food array.
So if the FoodArray == “Orange” then FoodScore would = 5.
You could add and take away from this seperate var according to other gameplay events; you could add 3 to it for example if the next item is an apple or deduct from it if rotten food was picked. You could even use the food score to modify a weight var. e.g var totalWeight = 0. If anything is picked up, an orange for example, it would add to the totalweight. If the weight was too much you could instuct the player to drop something.
I think that would be the best option, then you wouldn’t need to deail unecessarily with another array.
Try using a hashtable. You store each item as a “Key and value”. For example you could have this:
C#:
Hashtable myHashtable = new Hashtable();
myHashtable.Add("Apple", 5);
JS:
var myHashtable : Hashtable = new Hashtable();
myHashtable.Add("Apple", 5);
You can then change the health value of the apple by using:
myHashtable[“Apple”] = 10;
An even better way to do this might be to create a “pickup” class. This way you create a virtual “object” for all pickups and can assign an arbitrary amount of variables to each pickup. I can come back and make an example later if you like (busy right now).
I’d like to chime in with a question relevant to the OP’s recent question – is it a viable thing to do, create a struct/class and make an array/list of those? He could have a lot of items this way, each having their own properties. I vaugelly remember that something similar is possible, but didn’t try to poke around with it yet.
It’s a fundamental of Object Oriented Programming, defining your own object (a class, containing data and methods to manipulate that data). It’s a fundamental of programming (in my opinion). Have a read here
I would write you some sample code, but you code in dirty Javascript :). I’m sure someone else could pitch in.
Here’s some C# code, in case your interested:
[System.Serializable]
public class PickUp
{
public string pickupName;
public GameObject pickupPrefab
public int healthAdd;
public int speedBonus;
}
public PickUp[] pickUps = new PickUp[1];
Put that on an object and add your items in the inspector
Thanks for the example timsk! That cleared up a lot for me
Oddly enough, “dirty Javascript” would be functionally identical to your example code, except with less ugly syntax.
Speaking of dirty. Why use a hashtable when you can use Dictionary?
–Eric
When I think about making an inventory, I honestly don’t think I would use an array for it. It’s going to sound weird but I think the best system for me is to have an inventory that already has a slot for each item occupied, even if it shows a “0”. Part of the reason for this is all my games need to be blind accessible so it would be easier for my players to remember what items are where. The other part is I still don’t get arrays. I understand how to declare an array and I know how to call an array like I did in my earlier test, but I just don’t get how to use an array in a practical sense. I will be forced to make an array of some kind though because I have to be able to scroll the inventory system using the TAB button and be able to use individual items when selected. I already have the C# script written out from the BergZurg videos but I’ll work on converting it to javascript. I was reading the Javascript Bible about multidimensional arrays, which appear to be very similar to the Hashtable timsk posted.
I was also thinking about in-game cheat codes and I assume those would need some kind of array to execute properly. Either that or a short switch statement. I won’t worry about that until later though.