SA-MP Forums Archive
Help regarding a loop issue. - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Help regarding a loop issue. (/showthread.php?tid=600945)



Help regarding a loop issue. - Dokins - 14.02.2016

I'm making a 911 system for the police where they can view all the calls. It seems to constantly replace all 15 slots.

pawn Код:
if(PoliceCall[0][pol_rl] > 0)
            {
                for(new x = 0; x < MAX_POLICECALLS -1; x++)
                {
                    PoliceCall[x + 1][pol_rl] = PoliceCall[x][pol_rl];
                    PoliceCall[x + 1][pol_tih] = PoliceCall[x][pol_tih];
                    PoliceCall[x + 1][pol_tim] = PoliceCall[x][pol_tim];
                    format(PoliceCall[x + 1][pol_loc], 32, "%s", PoliceCall[x][pol_loc]);
                    format(PoliceCall[x + 1][pol_in], 64, "%s", PoliceCall[x][pol_in]);
                    format(PoliceCall[x + 1][pol_nm], 24, "%s", PoliceCall[x][pol_nm]);
                    printf("%s", PoliceCall[x][pol_in]);
                }
            }
            format(PoliceCall[0][pol_loc], 32, "%s", GetLocation(playerid));
            format(PoliceCall[0][pol_in], 64, "%s", PoliceCallP[playerid][pol_in]);
            PoliceCall[0][pol_rl] = PoliceCallP[playerid][pol_rl];
            PoliceCall[0][pol_tih] = THrs;
            PoliceCall[0][pol_tim] = TMins;
I create the new entry afterwards. I replace the [0] slot and move the 0 slot to the next slot, could anyone assist?

Thanks.


Re: Help regarding a loop issue. - Pottus - 14.02.2016

Yeah I mentioned you might need to re-work some logic now I am looking at it the problem is you are shifting the values in the wrong order. You need to start from the last entry and shift the previous entry.

Example

Код:
for(new x = MAX_POLICECALLS - 1; x > 0; x--)
{
	PoliceCall[x][pol_rl] = PoliceCall[x - 1][pol_rl];
	...
}
If you start at the start of the array then you are going to just copy all the data of the first index to every other index instead of shifting it. This will trash any data in the last index.


Re: Help regarding a loop issue. - Dokins - 14.02.2016

I thought the x++ part was where it increased the x and therefore adds one to it each time so it's copying the last?

I may be confused.

Will the example above work?

Thanks again. I'm seriously confused right now.


Re: Help regarding a loop issue. - Pottus - 14.02.2016

yeah it should be x-- my fault LOL!


Re: Help regarding a loop issue. - Dokins - 15.02.2016

Thank you, will give that a try.


Re: Help regarding a loop issue. - Dokins - 15.02.2016

It's fixed the order but I'm still having an issue with the Name, it copies to the first one (as in 0 and 1.) Everything else is correct, though.


Re: Help regarding a loop issue. - Dokins - 16.02.2016

Still unresolved.