Gamestudio Links
Zorro Links
Newest Posts
Change chart colours
by 7th_zorro. 05/11/24 09:25
Data from CSV not parsed correctly
by dr_panther. 05/06/24 18:50
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
5 registered members (AndrewAMD, alibaba, Konsti, 2 invisible), 1,418 guests, and 2 spiders.
Key: Admin, Global Mod, Mod
Newest Members
Hanky27, firatv, wandaluciaia, Mega_Rod, EternallyCurious
19051 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Faulty loop? #247101
01/19/09 02:50
01/19/09 02:50
Joined: Jul 2008
Posts: 54
Taipei, Taiwan
PlaystationThree Offline OP
Junior Member
PlaystationThree  Offline OP
Junior Member

Joined: Jul 2008
Posts: 54
Taipei, Taiwan
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.

Last edited by PlaystationThree; 01/19/09 02:56.

Bet you don't know where Taiwan is lol.

"The Lord is my light and my salvation..." Psm 27:1
Re: Faulty loop? [Re: PlaystationThree] #247103
01/19/09 03:00
01/19/09 03:00
Joined: Feb 2008
Posts: 3,232
Australia
EvilSOB Offline
Expert
EvilSOB  Offline
Expert

Joined: Feb 2008
Posts: 3,232
Australia
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.


"There is no fate but what WE make." - CEO Cyberdyne Systems Corp.
A8.30.5 Commercial
Re: Faulty loop? [Re: EvilSOB] #247104
01/19/09 03:05
01/19/09 03:05
Joined: Jul 2008
Posts: 54
Taipei, Taiwan
PlaystationThree Offline OP
Junior Member
PlaystationThree  Offline OP
Junior Member

Joined: Jul 2008
Posts: 54
Taipei, Taiwan
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.


Bet you don't know where Taiwan is lol.

"The Lord is my light and my salvation..." Psm 27:1
Re: Faulty loop? [Re: PlaystationThree] #247112
01/19/09 05:15
01/19/09 05:15
Joined: Feb 2008
Posts: 3,232
Australia
EvilSOB Offline
Expert
EvilSOB  Offline
Expert

Joined: Feb 2008
Posts: 3,232
Australia
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);
    }



"There is no fate but what WE make." - CEO Cyberdyne Systems Corp.
A8.30.5 Commercial
Re: Faulty loop? [Re: EvilSOB] #247317
01/20/09 05:34
01/20/09 05:34
Joined: Jul 2008
Posts: 54
Taipei, Taiwan
PlaystationThree Offline OP
Junior Member
PlaystationThree  Offline OP
Junior Member

Joined: Jul 2008
Posts: 54
Taipei, Taiwan
Dude you the man! Big help, learned lots about strings. Thanks real big.


Bet you don't know where Taiwan is lol.

"The Lord is my light and my salvation..." Psm 27:1
Re: Faulty loop? [Re: PlaystationThree] #247318
01/20/09 06:00
01/20/09 06:00
Joined: Feb 2008
Posts: 3,232
Australia
EvilSOB Offline
Expert
EvilSOB  Offline
Expert

Joined: Feb 2008
Posts: 3,232
Australia
Anytime, glad to help.


PS I know where Taiwan is... Its on Google-Earth!


"There is no fate but what WE make." - CEO Cyberdyne Systems Corp.
A8.30.5 Commercial

Moderated by  HeelX, Lukas, rayp, Rei_Ayanami, Superku, Tobias, TWO, VeT 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1