[Help] Loop in array
#1

Hello, i have two-dimensional array and enum, in array i have an integer and positions for trailers, i need compare first number in array(1,2,3...) with number which i set player and after all take once random positions and spawn trailer.

But when i wrote command on server, it write: "Server: Unknown command" and trailer is not spawned...I think that error is in if..please help
Код:
enum CorpProp
{
	id_firma,
	Float: tX,
	Float: tY,
	Float: tZ,
	Float: tA,
	Float: cpX,
	Float: cpY,
	Float: cpZ,
	Float: cpS,
}

new trailers[][CorpProp] =
{
	{1, 2875.0837, 938.3555, 11.2461, 90.0000},
	{2, 2874.9983, 934.0735, 11.2461, 90.0000},
	{3, 2874.8254, 929.4396, 11.2461, 90.0000},
	{4, 2874.8284, 924.6432, 11.2461, 90.0000},
	{5, 2874.7375, 920.9322, 11.2461, 90.0000},
	{6, 2874.6543, 917.0178, 11.2461, 90.0000},
	{7, 2874.5366, 912.9464, 11.2461, 90.0000},
	{8, 2873.8799, 908.7018, 11.5861, 90.0000}
};

//in callback
new n, a[MAX_VEHICLES], b;
for(new v = 0; v < sizeof(trailers); v++)
{
	if(trailers[v][id_firma] == Info[i][firma])
	{
		a[b] = v;
		b++;
	}
}
n = a[random(b)];
Info[i][naves] = CreateVehicle(435, trailers[n][tX], trailers[n][tY], trailers[n][tZ], trailers[n][tA], random(126), random(126), -1);
Sorry for my bad engilsh
Reply
#2

Most of the times, it displays the unknown command due to run time error such as Array index out of bounds (most common).

Load crashdetect plugin and compile with debug info. Re-compile your scripts and run the server again. Type that command and if it displays the unknown command again, post the server log.
Reply
#3

Код:
----------
Loaded log file: "server_log.txt".
----------

SA-MP Dedicated Server
----------------------
v0.3x-R2, ©2005-2013 SA-MP Team

[13:03:47] 
[13:03:47] Server Plugins
[13:03:47] --------------
[13:03:47]  Loading plugin: crashdetect
[13:03:47]   CrashDetect v4.13 is OK.
[13:03:47]   Loaded.
[13:03:47]  Loaded 1 plugins.

[13:03:47] 
[13:03:47] Filterscripts
[13:03:47] ---------------
[13:03:47]   Loading filterscript 'fsdebug.amx'...
[13:03:47] 
  *********************
  * SA:MP DEBUG 0.2   *
[13:03:47]   * By Simon Campbell *
  *********************
[13:03:47]   * Version: 0.5d      *
  *********************
[13:03:47]   * -- LOADED         *
  *********************

[13:03:47]   Loaded 1 filterscripts.

----------------------------------
[13:03:47]  Test Mode
[13:03:47] ----------------------------------

[13:03:47] Number of vehicle models: 6
[13:04:30] Incoming connection: 127.0.0.1:63883
[13:04:31] [join] Mattsy has joined the server (0:127.0.0.1)
[13:04:54] [debug] Run time error 4: "Array index out of bounds"
[13:04:54] [debug]  Accessing element at index 8 past array upper bound 7
[13:04:54] [debug] AMX backtrace:
[13:04:54] [debug] #0 0000b548 in dcmd_p (playerid=0, params[]=@0x00009cd8 "") at D:\samp03x_svr_R2_win32\gamemodes\bow.pwn:729
[13:04:54] [debug] #1 0000a54c in public OnPlayerCommandText (playerid=0, cmdtext[]=@0x0000ae2c "/p") at D:\samp03x_svr_R2_win32\gamemodes\bow.pwn:656
From Pawno:

Код:
Pawn compiler 3.2.3664	 	 	Copyright © 1997-2006, ITB CompuPhase

Header size:           2496 bytes
Code size:            52328 bytes
Data size:            44588 bytes
Stack/heap size:      16384 bytes; estimated max. usage=2760 cells (11040 bytes)
Total requirements:  115796 bytes
Reply
#4

Yes, it's caused because of a run time error: Index out of bounds.

What's the line 729? It's this one, isn't it?
pawn Код:
Info[i][naves] = CreateVehicle(435, trailers[n][tX], trailers[n][tY], trailers[n][tZ], trailers[n][tA], random(126), random(126), -1);
trailers array's valid index are 0-7 and n is probably 8 which is an out of bounds index.
Reply
#5

Its if
Код:
if(trailers[v][id_firma] == Info[playerid][firma])
Reply
#6

The weird thing is that v is 8 however the loop must only execute 8 times (0-7).

Are you sure it's:
pawn Код:
for(new v = 0; v < sizeof(trailers); v++)
and not:
pawn Код:
for(new v = 0; v <= sizeof(trailers); v++)
? Using <= would cause that problem.
Reply
#7

No....same problem :-/
Reply
#8

Debug it.

Use print messages and print the values of v (inside the loop) and sizeof (trailers).
Reply
#9

Код:
for(new v = 1; v <= sizeof(trailers); v++)
				{
				    printf("v = %d | sizeof = %d", v, sizeof(trailers));
Код:
[13:43:17] v = 1 | sizeof = 8
[13:43:17] v = 2 | sizeof = 8
[13:43:17] v = 3 | sizeof = 8
[13:43:17] v = 4 | sizeof = 8
[13:43:17] v = 5 | sizeof = 8
[13:43:17] v = 6 | sizeof = 8
[13:43:17] v = 7 | sizeof = 8
[13:43:17] v = 8 | sizeof = 8
Reply
#10

Using <= will cause the run time error. It should be:
pawn Код:
for(new v = 0; v < sizeof(trailers); v++)
because it starts from 0 and it ends at 7.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)