- Home /
Js , classes, and #pragma strict
I'm working on a simple Inventory system and I was creating a class for each item that shares certain variables.
I created a script called ItemClass.js which has the following code.
// #pragma strict
class ItemClass {
var name : String;
var amount : int;
var desc : String;
var type : int;
function ItemClass() {
this.name = 'NoName';
}
function ItemClass(name : String, amount : int,desc : String,type : int) {
this.name = name;
this.amount = amount;
this.desc = desc;
this.type = type;
}
function GetName() {
return this.name;
}
}
Then in another script, Inventory.js, I created a Hashtable with the values based on the ItemClass.
//#pragma strict
static var inventory : Hashtable;
function Awake(){
inventory = new Hashtable();
}
function Start() {
inventoryRaw['Iron Ore'] = new ItemClass('Iron Ore', 10, 'This is a lump of unrefined iron.', 0);
Debug.Log(inventoryRaw['Iron Ore']);
Debug.Log(inventoryRaw['Iron Ore'].name);
Debug.Log(inventoryRaw['Iron Ore'].GetName());
Debug.Log(inventoryRaw['Iron Ore'].amount);
Debug.Log(inventoryRaw['Iron Ore'].desc);
Debug.Log(inventoryRaw['Iron Ore'].type);
}
This works as the console would print out:
ItemClass
Iron Ore
Iron Ore
10
This is a lump of unrefined iron.
0
But this only works if I remove #pragma strict from both scripts. As I understand it, it's best to keep and follow #pragma strict as it'll help you in the long run.
What would be the correct syntax for my code to work with #pragma strict? Additionally, please feel free to comment how you would go about doing this.
Your answer
Follow this Question
Related Questions
How to store data in script and attach it later? 0 Answers
Accessing a variable inside a class inside other script... 2 Answers
I need help converting this script from zilch to js 0 Answers
Is jslib in Assetstore package allowed? 1 Answer
Array of custom class objects all return the same value? 1 Answer