Posted By: PlaystationThree
Faulty loop? - 01/19/09 02:50
I'm working on a text-based inventory system for a game, but there seems to be a problem with my while loop. The first item I add works propperly, and when I add another item it goes to the second slot, but it replaces both slots with the second label. Anyone spot the problem? I made a test script pasted below:
Thanks in advance.
Code:
TEXT* inventory[20]; TEXT* tempTxt = { string (""); } var inventorySell[20]; TEXT* namess[20]; var tempNum = 1; function addInv(STRING* itemName,itemAtk, itemDef, itemSell){ if(inventorySell[20] == 0){ var i = 0; while(i<20){ if(inventorySell[i] == 0){ str_cpy( ((tempTxt.pstring)[0]) , itemName); str_cat( ((tempTxt.pstring)[0]), " ["); if(itemAtk) str_cat_num( ((tempTxt.pstring)[0]),"%.0f", itemAtk); if(itemDef) str_cat_num( ((tempTxt.pstring)[0]),"%.0f", itemDef); str_cat( ((tempTxt.pstring)[0]), "]"); ((inventory[i].pstring)[0]) = ((tempTxt.pstring)[0]); inventorySell[i] = itemSell; return; } i++; } } return; } function main(){ video_mode = 7; var i; for(i = 0;i<20;i++){ inventory[i] = txt_create(1,20); (inventory[i])->pos_x = 630; (inventory[i])->pos_y = 100+(i*10); (inventory[i])->flags = VISIBLE; ((inventory[i])->pstring)[0] = ""; } namess[1] = txt_create(1,20); ((namess[1])->pstring)[0] = "name1"; namess[2] = txt_create(1,20); ((namess[2])->pstring)[0] = "name2"; namess[3] = txt_create(1,20); ((namess[3])->pstring)[0] = "name3"; namess[4] = txt_create(1,20); ((namess[4])->pstring)[0] = "name4"; namess[5] = txt_create(1,20); ((namess[5])->pstring)[0] = "name5"; while(1){ if(key_space){addInv(((namess[tempNum])->pstring)[0],2+integer(random(3)),0,1);wait(-0.5);tempNum++;} wait(1); } }
Thanks in advance.