SA-MP Forums Archive
Loops once only? - 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: Loops once only? (/showthread.php?tid=463373)



Loops once only? - Riddick94 - 11.09.2013

pawn Код:
new weapid = 1;
public OnFilterScriptInit()
{
    while(weapid <= 2)
    {
        WeaponObjects[weapid] = CreateObject(WeaponData[weapid][WEAPON_MODEL], WeaponData[weapid][WEAPON_X], WeaponData[weapid][WEAPON_Y], WeaponData[weapid][WEAPON_Z], WeaponData[weapid][WEAPON_RX], WeaponData[weapid][WEAPON_RY], WeaponData[weapid][WEAPON_RZ], 300.0);
        printf("ID: %d | ID2: %d", WeaponObjects[weapid], weapid);
        weapid += 1;
    }
    return true;
}
Should do it twice, debug prints only once.


Re: Loops once only? - Macluawn - 11.09.2013

whats the size of the WeaponData and WeaponObject array?
If it gets out of bounds, it will stop executing.


Re: Loops once only? - Riddick94 - 11.09.2013

enum with a table (WeaponData)

pawn Код:
new WeaponData[MAX_CUSTOM_WEAPONS][E_WEAPON_INFO] =
{
Size of MAX_CUSTOM_WEAPONS is 2.

WeaponObject array is the same.


Re: Loops once only? - Konstantinos - 11.09.2013

When it's declared with size 2, it means 0,1. When the element at the index is 2 will result to an array index out of bounds like Macluawn already said.

You should start from 0, not from 1. Indexes at Pawn starts from 0.


Re: Loops once only? - Riddick94 - 11.09.2013

Quote:
Originally Posted by Konstantinos
Посмотреть сообщение
When it's declared with size 2, it means 0,1. When the element at the index is 2 will result to an array index out of bounds like Macluawn already said.

You should start from 0, not from 1. Indexes at Pawn starts from 0.
Yeah, but when you create vehicle/object/pickup or anything and assign it to the array, it will start from 1 for some reason. That's why I started from 1.

I tried bigger numbers or anything, and its still the same.


Re: Loops once only? - Konstantinos - 11.09.2013

Quote:
Originally Posted by Riddick94
Посмотреть сообщение
Yeah, but when you create vehicle/object/pickup or anything and assign it to the array, it will start from 1 for some reason. That's why I started from 1.

I tried bigger numbers or anything, and its still the same.
It will assign a value to it which might start from 1, but the first index will be 0.

pawn Код:
WeaponObjects[ 0 ] = 1;
WeaponObjects[ 1 ] = 2;
Like the example above, it will start with index 0, but the value will start with number 1 as you said.

There's absolutely no reason to start from index 0, when it only assigns a value that starts from 1 to it.


Re: Loops once only? - Riddick94 - 11.09.2013

pawn Код:
new WeaponData[MAX_CUSTOM_WEAPONS][E_WEAPON_INFO] =
{
    {"Micro-SMG", 1200, 28, 50, 352, 310.905181, -166.905059, 999.661987, -89.199905, 42.499996, 176.358154},
    {"Desert Eagle", 2000, 24, 14, 348, 312.696197, -166.850097, 999.641113, -89.799957, 0.0000, 183.354507}
};

public OnFilterScriptInit()
{
    new weapid = 1;
    while(weapid < 3)
    {
        WeaponObjects[weapid] = CreateObject(WeaponData[weapid][WEAPON_MODEL], WeaponData[weapid][WEAPON_X], WeaponData[weapid][WEAPON_Y], WeaponData[weapid][WEAPON_Z], WeaponData[weapid][WEAPON_RX], WeaponData[weapid][WEAPON_RY], WeaponData[weapid][WEAPON_RZ], 300.0);
        printf("ID: %d | ID2: %d", WeaponObjects[weapid], weapid);
        weapid += 1;
    }
    return true;
}
So, any idea how to fix it then, because I get really confused right now about it. I know arrays starts assigning from 0, but I'm stuck right now, I admit it. Sorry if I sound really retarted about that.


Re: Loops once only? - Konstantinos - 11.09.2013

Quote:
Originally Posted by Riddick94
Посмотреть сообщение
I know arrays starts assigning from 0
No, no. That's the part you get confused. You can assign any number you want (-2.1 billions to 2.1 billions), but inside the [ ], you need to start from 0 to MAX_CUSTOM_WEAPONS-1.

pawn Код:
new WeaponData[MAX_CUSTOM_WEAPONS][E_WEAPON_INFO] =
{
    {"Micro-SMG", 1200, 28, 50, 352, 310.905181, -166.905059, 999.661987, -89.199905, 42.499996, 176.358154},
    {"Desert Eagle", 2000, 24, 14, 348, 312.696197, -166.850097, 999.641113, -89.799957, 0.0000, 183.354507}
};

public OnFilterScriptInit()
{
    for(new weapid = 0; weapid < 2; weapid++)
    {
        WeaponObjects[weapid] = CreateObject(WeaponData[weapid][WEAPON_MODEL], WeaponData[weapid][WEAPON_X], WeaponData[weapid][WEAPON_Y], WeaponData[weapid][WEAPON_Z], WeaponData[weapid][WEAPON_RX], WeaponData[weapid][WEAPON_RY], WeaponData[weapid][WEAPON_RZ], 300.0);
        printf("ID: %d | ID2: %d", WeaponObjects[weapid], weapid);
    }
    return true;
}
That should work fine.


Re: Loops once only? - Riddick94 - 11.09.2013

Quote:
Originally Posted by Konstantinos
Посмотреть сообщение
No, no. That's the part you get confused. You can assign any number you want (-2.1 billions to 2.1 billions), but inside the [ ], you need to start from 0 to MAX_CUSTOM_WEAPONS-1.

pawn Код:
new WeaponData[MAX_CUSTOM_WEAPONS][E_WEAPON_INFO] =
{
    {"Micro-SMG", 1200, 28, 50, 352, 310.905181, -166.905059, 999.661987, -89.199905, 42.499996, 176.358154},
    {"Desert Eagle", 2000, 24, 14, 348, 312.696197, -166.850097, 999.641113, -89.799957, 0.0000, 183.354507}
};

public OnFilterScriptInit()
{
    for(new weapid = 0; weapid < 2; weapid++)
    {
        WeaponObjects[weapid] = CreateObject(WeaponData[weapid][WEAPON_MODEL], WeaponData[weapid][WEAPON_X], WeaponData[weapid][WEAPON_Y], WeaponData[weapid][WEAPON_Z], WeaponData[weapid][WEAPON_RX], WeaponData[weapid][WEAPON_RY], WeaponData[weapid][WEAPON_RZ], 300.0);
        printf("ID: %d | ID2: %d", WeaponObjects[weapid], weapid);
    }
    return true;
}
That should work fine.
I had this that way done, but the IDs are swapped. I mean, have a look:
Код:
[20:55:41] ID: 1 | ID2: 0
[20:55:41] ID: 2 | ID2: 1
What I'm doing is, checking if player is standing in 1 meter range of the object and display a message. It does work for Desert Eagle only. So, to make it work I need that like:

Код:
[20:55:41] ID: 1 | ID2: 1
[20:55:41] ID: 2 | ID2: 2
Am I right?


Re: Loops once only? - Konstantinos - 11.09.2013

pawn Код:
new WeaponData[MAX_CUSTOM_WEAPONS][E_WEAPON_INFO] =
{
    {"Micro-SMG", .. }, // WeaponData[0]
    {"Desert Eagle", .. } // WeaponData[1]
};
The value you assign is not same as the index.

WeaponObjects[0] is 1 (weapid is 0)
WeaponObjects[1] is 2 (weapid is 1)


What you need to check is the value of WeaponObjects[weapid], not the index (in your case: weapid).