- Home /
Class Programming to build an inventory system
I am doing inventory system, and try to implement it in Classes way. But there are some problems.
class Inventory{ private var items:Item[]=new Item[GameData.player_max_slots];
function Inventory(){ print("1"); for(var i=0;i<GameData.player_max_slots;i++) items[i]=null; }
function Print_Item_Names(){ for(var i=0;i<GameData.player_max_slots;i++) print(items[i].GetName);
print("2");
}
function Add_Item(_item:Item) { for(var i=0;i<GameData.player_max_slots;i++) items[i]=_item; print("3"); }
}
class Item{ private var name:String;
function Item(_name:String) { name=_name; }
function GetName():String{ return name; }
}
And I try to test it:
if (Input.GetKeyDown ("q"))
{
var player_inventory:Inventory=new Inventory();
player_inventory.Add_Item(new Item("ff"));
player_inventory.Print_Item_Names();
}
Print_Item_Names() doesn't work. And even print("1"); print("2"); print("3"); in methods of the inventory Class are not called.
Answer by TheDemiurge · Apr 28, 2011 at 09:29 AM
Classes that don't derive from MonoBehaviour don't properly recognize the print
function, though I don't remember why anymore. For whatever reason they don't throw an error on it, but it just doesn't do anything. You can use Debug.Log
and it will work, or you can make your class derive from MonoBehaviour - depending on your needs.
Yes, you got the point. I found print() not applicable in class methods, other methods function normal.
Answer by jahroy · Apr 28, 2011 at 06:19 AM
Sounds like the constructor is not getting called.
If you put a print("0"); in front of the first line, does IT print?
Is your script attached to an object that's enabled?
Is it in an OnGUI or Update function (or something like it)?
If you put a print("0"); in front of the first line, does IT print? No, it doesn't print.
Is your script attached to an object that's enabled? 1. The 2 classes scripts doesn't attach to anything, do I need to? 2. The script of press "q" and print result is attached to an empty object, this function will execute. I have tested.
Is it in an OnGUI or Update function (or something like it)? Yes, the press "q" function is in Update();
Thanks
err. why no line break...maybe need to press shift+enter? I try try 1 more time
I typically attach scripts with OnGUI() or Update() functions to the GameObjects they interact with. In your case I would attach that script to an empty, enabled GameObject for testing purposes. Or to anything in your scene.
Answer by CHPedersen · Apr 28, 2011 at 06:26 AM
First of all, you don't need to initialize reference type variables to null. I.e., in your Inventory constructor, don't loop through the array, setting items[i]=null;. When you create an empty static array of objects, the elements are automatically null.
That said, I'm not sure why at least print("1") wouldn't get called. All other errors aside, when you hit q, the first thing that happens is that the Inventory constructor is invoked, which calls print("1") as its very first action. Therefore, I suspect there's a problem with this code's location in your program. Where have you put the if-statement testing for the q-key? Is it in some script's Update? Is that script enabled?
if statement will be executed, I have tested. It's in Update.
I agree constructor is invoked, theory print will run first. Very wired ahhhh~~~