Problem loops.
#1

Hi.

I actually have a dynamic object system which uses MySQL. When I put down an object using a command, it's saved in my MySQL tables. When I delete it, its row is deleted from the MySQL table ofc.

I have a loop in my code (see below) but entry (my loop variable) is incrementing itself a weird way.

Instead of incrementing normally (1 per 1), it's incrementing 2 per 2.

Anyone knows ? Thanks !

pawn Код:
for(entry = 0; entry < sizeof(ObjetsInfo); entry++)
{
    printf("%d", entry);
    if(!ObjetsInfo[entry][PosX] && !ObjetsInfo[entry][PosY] && !ObjetsInfo[entry][PosZ]) break;
}
Logs for 2 objects:

pawn Код:
[20:12:36] 0
[20:12:39] 0
[20:12:39] 1
[20:12:39] 2
Reply
#2

Edit: Nevermind, I didn't read it correctly.

I'd suggest using something like

pawn Код:
#define     MAX_OBJECTS      (NUMBER}
and
for(new i = 0; i < MAX_OBJECTS; i ++) // I've preferred using this, rather than 'sizeof'
Reply
#3

Okay.

But the problem is still present..
Reply
#4

pawn Код:
for(entry = 0; entry < MAXIMUM_OBJ; entry++)
{
    printf("Loop: %d", entry);
    if(ObjetsInfo[entry][PosX] == 0) break;
}
       
printf("Id: %d", entry);
For 4 objets (logs):

Код:
[21:37:43] Loop: 0
[21:37:43] Id: 0
[21:37:53] Loop: 0
[21:37:53] Loop: 1
[21:37:53] Loop: 2
[21:37:53] Id: 2
[21:38:14] Loop: 0
[21:38:14] Loop: 1
[21:38:14] Loop: 2
[21:38:14] Loop: 3
[21:38:14] Loop: 4
[21:38:14] Id: 4
[21:38:18] Loop: 0
[21:38:18] Loop: 1
[21:38:18] Loop: 2
[21:38:18] Loop: 3
[21:38:18] Loop: 4
[21:38:18] Loop: 5
[21:38:18] Loop: 6
[21:38:18] Id: 6
Reply
#5

Is this loop, inside a loop?
pawn Код:
for(entry = 0; entry < MAXIMUM_OBJ; entry++)
{
    printf("Loop: %d", entry);
    if(ObjetsInfo[entry][PosX] == 0) break;
}
       
printf("Id: %d", entry);
Reply
#6

This is a loop.

One in my cmd.
Reply
#7

Currently my code:

pawn Код:
for(new i = 1; i < MAXIMUM_OBJ; i++)
{
    printf("Loop: %d", i);
    if(ObjetsInfo[i][PosX] != 0.000000 || ObjetsInfo[i][PosY] != 0.000000 || ObjetsInfo[i][PosZ] != 0.000000)
    { // Object created.
        printf("Id: %d - X: %f - Y: %f - Z: %f", i, ObjetsInfo[i][PosX], ObjetsInfo[i][PosY], ObjetsInfo[i][PosZ]);
        continue;
    }
    else if(ObjetsInfo[i][PosX] == 0.000000 || ObjetsInfo[i][PosY] == 0.000000 || ObjetsInfo[i][PosZ] == 0.000000)
    { // No object, it's good.
        entry = i;
        break;
    }
}

printf("Id use:", entry);
In server_log.txt:

Код:
[22:55:56] Loop: 1 <- Here it's good.
[22:55:56] Id use: 1 <- Here it's good.
[22:55:58] Loop: 1 <- Here it's good.
[22:55:58] Id: 1 - X: 1743.147460 - Y: -1854.234130 - Z: 13.414062 <- Here it's good.
[22:55:58] Loop: 2 <- Here it's good.
[22:55:58] Id: 2 - X: 0.000000 - Y: 0.000000 - Z: 0.000000 <- Here, he should have assigned the id 2, but it has continued and is assigned id 3 ... 
[22:55:58] Loop: 3
[22:55:58] Id use: 3
And it does it all the time, it's 2 in 2

Can you help me please?
Reply
#8

Quote:
Originally Posted by zT KiNgKoNg
Посмотреть сообщение
pawn Код:
// I've preferred using this, rather than 'sizeof'
You're only making it harder on yourself. sizeof is an operator, not a function. Its value is calculated at compile time and therefore has no impact on performance at all.
Reply
#9

Quote:
Originally Posted by Baltimore
Посмотреть сообщение
And it does it all the time, it's 2 in 2

Can you help me please?
What does the database data look like? Post a screen cap including the fields. This should help deduce what is wrong with your command. Include your SQL select statement that you use to populate your variables.
Reply
#10

We need to know how you store the values into the objectinfos enum, only then we can know what's wrong with it.

Not only that, but why do you start a loop in 1 when you deal with arrays? All arrays start in 0 not 1.

Also you don't need that continue inside a if else statement. Not only that I do not understand why you break the loop like that.

So basically show us the whole cmd and how you set the ObjectsInfo values.
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)