- Home /
About Classes in js
hello i am making an inventory system and id like to know a little bit more about classes.
so here is my question i have an item scripts i got from tutorials and im trying to understand exactly how to use class .
i have this script here
item
public class Item
{
var Name : String;
var Description : String;
var icon : Texture2D;
var equipped : boolean;
var Stamina : int;
var Power : float;
var attackSpeed : float;
var rarity : Rarity;
var itemType : ItemType;
function ApplyStats ()
{
if(equipped)
{
Debug.Log("Add Stats");
}
else
{
Debug.Log("Remove Stats");
}
}
}
enum Rarity
{
uncommon,
common,
rare,
epic
}
enum ItemType
{
helm,
shoulders,
gloves,
pants,
boots,
ring,
neckless
}
so how would i call this to instantiate a new object.then the new object would have those variables right ? and wtv on the class.
also feel free to post any relevant informative links ive looked up alot of tutorials and not many talks about classes in javascript they are pretty much all in c# is there a reason for that ? or both are as good ?
Answer by devcutters · Oct 20, 2014 at 11:19 PM
ok i get that things i want to know heres the situation i understand how the script works but here i have my inventory script. If you see in my function start i add my object to the maininventory but i have my drops that spawn when a tree is cut with the anim and everything thing is how would i make my actual logs and branch and coconut and actual item out of the item script like i know i can manipulate my class by calling it with class.variablename if im right but yeah id like to know a bit more about this or information about it.
i know i would have to add more item types and everythings like i said i know how the script work just that part is greyish :S
would i have to basicly make a function inside my inventory like item pickup and then make it if i go over the colider tagged item lets say with another colider tag player then add to inventory ?
import System.Collections.Generic;
//holding all items
var items : Item[];
//inventory
var MainInventory : List.<Item> = new List.<Item>();
//Equip menu 0 = helm 1=shoulder 2=gloves 3=pants 4=boots 5=ring 6=neckless
var EquipMenu : Item[];
//Stats
var Power : int = 0;
function Start()
{
MainInventory.Add(items[0]);
MainInventory.Add(items[1]);
MainInventory.Add(items[2]);
EquipMenu[0] = null;
}
function OnGUI()
{
for(var x = 0; x < MainInventory.Count; x++)
{
if(GUI.Button(Rect(Screen.width/2, Screen.height/2 + (20 * x), 100, 20),GUIContent ( MainInventory[x].Name,
"Stamina: " + MainInventory[x].Stamina + "\n" +
"Power: " + MainInventory[x].Power + "\n" +
"Attack Speed: " + MainInventory[x].attackSpeed + "\n" +
"Rarity: " + MainInventory[x].rarity + "\n" )))
{
if(MainInventory[x].itemType == MainInventory[x].itemType.helm)
{
if(EquipMenu[0] != null)
{
MainInventory.Add(EquipMenu[0]);
}
EquipMenu[0] = null;
EquipMenu[0] = MainInventory[x];
Power += EquipMenu[0].Power;
MainInventory.RemoveAt(x);
Debug.Log(Power);
}
else if(MainInventory[x].itemType == MainInventory[x].itemType.shoulders)
{
EquipMenu[1] = MainInventory[x];
MainInventory.RemoveAt(x);
}
}
GUI.Label(Rect(Screen.width/2 + 150, Screen.height/2 + (20 * x), 300, 300), GUI.tooltip);
var p : int = 0;
if(MainInventory[x].itemType == MainInventory[x].itemType.helm)
{
p = 0;
}
var tooltip2 =
"Stamina: " + EquipMenu[p].Stamina + "\n" +
"Power: " + EquipMenu[p].Power + "\n" +
"Attack Speed: " + EquipMenu[p].attackSpeed + "\n" +
"Rarity: " + EquipMenu[p].rarity;
//second tooltip to compare to equipped item
if(GUI.tooltip != "")
{
GUI.Box(new Rect(Screen.width/2 + 350, Screen.height/2 + (20 * x), 200, 300),"");
GUI.Label(new Rect(Screen.width/2 +350, Screen.height/2 + (20 * x), 300, 300), tooltip2);
}
}
for(var y = 0; y < EquipMenu.length; y++)
{
if(EquipMenu[y] != null)
{
if(GUI.Button(Rect(Screen.width/2 - 150, Screen.height/2 + (20 * y), 100, 20)," " + EquipMenu[y].Name))
{
if(EquipMenu[y] != null)
{
MainInventory.Add(EquipMenu[y]);
Power -= EquipMenu[y].Power;
EquipMenu[y] = null;
}
}
}
}
}
here a nice tutorial i found out there that explain and complete jesseEtzler https://www.youtube.com/playlist?list=PLxLNqnnCshwm9ayxq$$anonymous$$peWH4P1SLzrN92-
here is the link to the tutorial : https://www.youtube.com/watch?v=$$anonymous$$6gUSw5wl3Q
alot of information about classes figured i post it here since theres not alot of view on this guy video and that pretty much handled all the questions i had after jesse tutorials
Answer by Phantomized · Oct 20, 2014 at 06:48 AM
It's important to understand how unity treats the whole class system. Unity scripts inherit from something called MonoBehaviour which is basically the base class at the top. In JavaScript the fact that you inherit from that is hidden, but if you open a C# script you can see that your class inherit from it.
Monobehaviour Works so that it treats the script you create in the unity engine, as something that resembles a class. So you don't instantiate classes usually in unity. If you put that script on a gameobject in the hierarchy, you would actually be having an instance of your class as a Component on your gameobject. It's a very "physical" approach to instantiating the generic way.