Faulty loop?

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:


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.
Posted By: EvilSOB

Re: Faulty loop? - 01/19/09 03:00

I THINK the problem is in your arrays.
Your definition var inventorySell[20]; will only give you an array going from
inventorySell[0] to inventorySell[19] but your first IF is looking for inventorySell[20]
which will contain an unknown random value.

Everything else looks OK but I didnt look that hard.
Posted By: PlaystationThree

Re: Faulty loop? - 01/19/09 03:05

Thanks for the fast reply smile but it's still not working. I think the problem is that the loop goes all through the previous slots as well, changing them. I can't find the reason however.
Posted By: EvilSOB

Re: Faulty loop? - 01/19/09 05:15

Two lines to replace, brightly marked and easily for you to spot.
You were creating one set of invalid string structures,(in the addInv function)
and one set on fixed-length strings that were locked to zero length.(in the 'main' function)

Easy to do.

PS if you see any other changes in the code, try to remove them as they were
un-intentional changes...

Code:
TEXT* inventory[20];
TEXT* tempTxt = {
    string ("");
}
var inventorySell[20];
TEXT* namess[20];
var tempNum = 1;

function addInv(STRING* itemName,var itemAtk, var itemDef, var itemSell){
    if(inventorySell[20] == 0){
        var i;    for(i=0; i<20; i++){
            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]);<<<<<<<<<<<<<<<<<<<<<<<<<<Ditch this
                str_cpy((inventory[i].pstring)[0], (tempTxt.pstring)[0]);    //<<<<<<<<<<<<<<<<<<<<<Replace with this
                inventorySell[i] = itemSell;
                return;
            }
        }
    }
    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] = "";<<<<<<<<<<<<<<<<<<<<<<<<<<<<<Also Ditch this
        ((inventory[i])->pstring)[0] = str_create("");    //<<<<<<<<<<<<<<<<<<<and replace with this
    }
    
    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(13)),0,1);    wait(-0.5);        tempNum++;    }
        wait(1);
    }

Posted By: PlaystationThree

Re: Faulty loop? - 01/20/09 05:34

Dude you the man! Big help, learned lots about strings. Thanks real big.
Posted By: EvilSOB

Re: Faulty loop? - 01/20/09 06:00

Anytime, glad to help.


PS I know where Taiwan is... Its on Google-Earth!
© 2024 lite-C Forums