SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help

Reply
 
Thread Tools Display Modes
Old 27/10/2014, 08:19 PM   #1
Baltimore
Gangsta
 
Join Date: Jun 2014
Posts: 881
Reputation: 20
Default Problem loops.

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 Code:
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 Code:
[20:12:36] 0
[20:12:39] 0
[20:12:39] 1
[20:12:39] 2
Baltimore is offline   Reply With Quote
Old 27/10/2014, 09:15 PM   #2
zT KiNgKoNg
Gangsta
 
Join Date: Jun 2012
Location: $Location = $this->Location->Finder('United Kingdom');
Posts: 990
Reputation: 40
Default Re: Problem loops.

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

I'd suggest using something like

pawn Code:
#define     MAX_OBJECTS      (NUMBER}
and
for(new i = 0; i < MAX_OBJECTS; i ++) // I've preferred using this, rather than 'sizeof'
zT KiNgKoNg is offline   Reply With Quote
Old 27/10/2014, 09:24 PM   #3
Baltimore
Gangsta
 
Join Date: Jun 2014
Posts: 881
Reputation: 20
Default Re : Problem loops.

Okay.

But the problem is still present..
Baltimore is offline   Reply With Quote
Old 27/10/2014, 09:45 PM   #4
Baltimore
Gangsta
 
Join Date: Jun 2014
Posts: 881
Reputation: 20
Default Re : Problem loops.

pawn Code:
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):

Code:
[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
Baltimore is offline   Reply With Quote
Old 27/10/2014, 10:34 PM   #5
Crayder
High-roller
 
Crayder's Avatar
 
Join Date: Sep 2013
Location: Flames of Hell
Posts: 3,837
Reputation: 607
Default Re: Problem loops.

Is this loop, inside a loop?
pawn Code:
for(entry = 0; entry < MAXIMUM_OBJ; entry++)
{
    printf("Loop: %d", entry);
    if(ObjetsInfo[entry][PosX] == 0) break;
}
       
printf("Id: %d", entry);
__________________
Those who deserve reputation, do not need to beg for it.
Also, don't expect the help you need when offering reputation, you'll just be attracting Rep Hunters.
Join SA-MP Discord!
Crayder is online now   Reply With Quote
Old 27/10/2014, 10:36 PM   #6
Baltimore
Gangsta
 
Join Date: Jun 2014
Posts: 881
Reputation: 20
Default Re : Problem loops.

This is a loop.

One in my cmd.
Baltimore is offline   Reply With Quote
Old 27/10/2014, 11:30 PM   #7
Baltimore
Gangsta
 
Join Date: Jun 2014
Posts: 881
Reputation: 20
Default Re : Problem loops.

Currently my code:

pawn Code:
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:

Code:
[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?
Baltimore is offline   Reply With Quote
Old 27/10/2014, 11:41 PM   #8
Vince
Spam Machine
 
Vince's Avatar
 
Join Date: Sep 2007
Location: Belgium
Posts: 10,952
Reputation: 2651
Default Re: Problem loops.

Quote:
Originally Posted by zT KiNgKoNg View Post
pawn Code:
// 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.
__________________
Vince is offline   Reply With Quote
Old 28/10/2014, 01:40 AM   #9
nemesis-
Huge Clucker
 
Join Date: Sep 2009
Location: USA
Posts: 236
Reputation: 65
Default Re: Re : Problem loops.

Quote:
Originally Posted by Baltimore View Post

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.
nemesis- is offline   Reply With Quote
Old 28/10/2014, 02:30 AM   #10
Kimossab
Gangsta
 
Kimossab's Avatar
 
Join Date: Jul 2012
Location: Portugal
Posts: 492
Reputation: 108
Default Re: Problem loops.

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.
__________________
Who even cares about signatures?

ALL HAIL THE ASTERISK!
My Pastebinas.
Kimossab is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem loops. Baltimore Scripting Help 4 27/10/2014 04:24 PM
Problem with my command (And loops). (+rep) Baltimore Scripting Help 2 27/10/2014 11:58 AM
Problem with two for loops in a row: the first runs, the second doesn't WiredGuyX Scripting Help 1 29/03/2013 03:11 PM
Moderate/Advanced problem using loops, file operating (.txt) and arrays... knackworst Scripting Help 2 04/11/2011 12:43 AM
[Problem] loops don't work XePloiT Help Archive 1 25/11/2010 01:12 AM


All times are GMT. The time now is 01:15 AM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.