- Home /
Question by
cariaga · Sep 09, 2013 at 02:47 PM ·
javascriptarraysdatabaseinventoryinventory system
Is this considered bad coding
db = new MonoSQLiteDB( "Model_D_Interaction.sqlite" );//name of database to access
//db.Result = db.ExecuteFreeQuery( "SELECT * FROM Player_inventory", true );
db.Result = db.ExecuteFreeQuery( "SELECT * FROM Player_inventory WHERE Item_Id", true );
if((db.Result[0]["Item_Name"])){
var other = Instantiate(ContentCollections[0], transform.position, Quaternion.identity);
other.name =db.Result[0]["Item_Name"];
other.transform.parent = transform;//attach the data
var script = other.GetComponent(Item);
db.Result = db.ExecuteFreeQuery( "SELECT * FROM Player_inventory WHERE Item_Stack", true );
script.stack = parseInt(db.Result[0]["Item_Stack"]);
AddItem(other);
yield WaitForSeconds(2);
//print((db.Result[0]["Item_Name"]))
}
if((db.Result[1]["Item_Name"])){
var other1 = Instantiate(ContentCollections[0], transform.position, Quaternion.identity);
other1.name =db.Result[1]["Item_Name"];
other1.transform.parent = transform;//attach the data
var script1 = other1.GetComponent(Item);
db.Result = db.ExecuteFreeQuery( "SELECT * FROM Player_inventory WHERE Item_Stack", true );
script1.stack = parseInt(db.Result[1]["Item_Stack"]);
AddItem(other1);
yield WaitForSeconds(2);
//print((db.Result[0]["Item_Name"]))
}
if((db.Result[2]["Item_Name"])){
var other2 = Instantiate(ContentCollections[0], transform.position, Quaternion.identity);
other2.name =db.Result[2]["Item_Name"];
other2.transform.parent = transform;//attach the data
var script2 = other2.GetComponent(Item);
db.Result = db.ExecuteFreeQuery( "SELECT * FROM Player_inventory WHERE Item_Stack", true );
script2.stack = parseInt(db.Result[2]["Item_Stack"]);
AddItem(other2);
yield WaitForSeconds(2);
//print((db.Result[0]["Item_Name"]))
}
if((db.Result[3]["Item_Name"])){
var other3 = Instantiate(ContentCollections[0], transform.position, Quaternion.identity);
other3.name =db.Result[3]["Item_Name"];
other3.transform.parent = transform;//attach the data
var script3 = other3.GetComponent(Item);
db.Result = db.ExecuteFreeQuery( "SELECT * FROM Player_inventory WHERE Item_Stack", true );
script3.stack = parseInt(db.Result[3]["Item_Stack"]);
AddItem(other3);
yield WaitForSeconds(2);
//print((db.Result[0]["Item_Name"]))
}
if((db.Result[4]["Item_Name"])){
var other4 = Instantiate(ContentCollections[0], transform.position, Quaternion.identity);
other4.name =db.Result[4]["Item_Name"];
other4.transform.parent = transform;//attach the data
var script4 = other4.GetComponent(Item);
db.Result = db.ExecuteFreeQuery( "SELECT * FROM Player_inventory WHERE Item_Stack", true );
script4.stack = parseInt(db.Result[4]["Item_Stack"]);
AddItem(other4);
yield WaitForSeconds(2);
//print((db.Result[0]["Item_Name"]))
}
if((db.Result[5]["Item_Name"])){
var other5 = Instantiate(ContentCollections[0], transform.position, Quaternion.identity);
other5.name =db.Result[5]["Item_Name"];
other5.transform.parent = transform;//attach the data
var script5 = other5.GetComponent(Item);
db.Result = db.ExecuteFreeQuery( "SELECT * FROM Player_inventory WHERE Item_Stack", true );
script5.stack = parseInt(db.Result[5]["Item_Stack"]);
AddItem(other5);
yield WaitForSeconds(2);
//print((db.Result[0]["Item_Name"]))
}
if((db.Result[6]["Item_Name"])){
var other6 = Instantiate(ContentCollections[0], transform.position, Quaternion.identity);
other6.name =db.Result[6]["Item_Name"];
other6.transform.parent = transform;//attach the data
var script6 = other6.GetComponent(Item);
db.Result = db.ExecuteFreeQuery( "SELECT * FROM Player_inventory WHERE Item_Stack", true );
script6.stack = parseInt(db.Result[6]["Item_Stack"]);
AddItem(other6);
yield WaitForSeconds(2);
//print((db.Result[0]["Item_Name"]))
}
Comment
I FIGURED IT OUT if anybody needs the code here you go
for (var i=0;i<db.Result.Length;i++) {
var x = 0;
if((db.Result[x]["Item_Name"])) {
var other = Instantiate(ContentCollections[x], transform.position, Quaternion.identity);
other.name =db.Result[i]["Item_Name"];
other.transform.parent = transform;//attach the data
var script = other.GetComponent(Item);
db.Result = db.ExecuteFreeQuery( "SELECT * FRO$$anonymous$$ Player_inventory WHERE Item_Stack", true );
script.stack = parseInt(db.Result[i]["Item_Stack"]);
AddItem(other);
// yield WaitForSeconds(2);
//print((db.Result[i]["Item_Name"]))
x++;
}
}
"Any time you repeat ANYTHING, it is bad coding..."
it's one way to look at things.
Another absolutely critical point .. is it true that you only WANT TO DO ONE OF THESE (the first one that "comes up"), or did you actually mean to do them all.
Best Answer
Answer by mattssonon · Sep 09, 2013 at 03:02 PM
Yes, it is. You should wrap it in, for instance, a loop:
for (int i = 0; i < Result.Length; i++) {
if((db.Result[i]["Item_Name"])) {
var other = Instantiate(ContentCollections[i], transform.position, Quaternion.identity);
other.name =db.Result[i]["Item_Name"];
other.transform.parent = transform;//attach the data
var script = other.GetComponent(Item);
db.Result = db.ExecuteFreeQuery( "SELECT * FROM Player_inventory WHERE Item_Stack", true );
script.stack = parseInt(db.Result[i]["Item_Stack"]);
AddItem(other);
yield WaitForSeconds(2);
//print((db.Result[i]["Item_Name"]))
}
}
Instantiate only occurred ones, And the result was IndexOutOfRangeException when i< reached 2 i was thinking a for loop will do before but still end up with IndexOutOfRangeException.
O$$anonymous$$ i figured it out THAN$$anonymous$$S for THE note about my "Code smell"