Coords in an Array
#1

Hello, I am wondering how I can do this. Have an array full of coords and make the script load all those coords on OnGameModeInit, like this;

pawn Код:
new Float:gShopLocations[][] = {
    {1975.8096,-2036.7939,13.5469},
    {1929.6444,-1776.3248,13.5469},
    {1128.9231,-1271.5233,13.5469},
    {-1615.9480,1139.0419,7.1875},
    {-2419.4297,970.0163,45.2969},
    {-2664.9050,-7.7562,6.1328}
};
pawn Код:
new rand = random(sizeof(gShopLocations));
    CreateDynamicPickup(1239, 1, gShopLocations[rand][0], gShopLocations[rand][1], gShopLocations[rand][2], -1, -1, INVALID_PLAYER_ID, 150.0);
    CreateDynamicMapIcon(gShopLocations[rand][0], gShopLocations[rand][1], gShopLocations[rand][2], 38, COLOR_WHITE, -1, -1, INVALID_PLAYER_ID, 150.0);
    CreateDynamic3DTextLabel("SHOP", COLOR_RED, gShopLocations[rand][0], gShopLocations[rand][1], gShopLocations[rand][2], 150.0, INVALID_PLAYER_ID, INVALID_VEHICLE_ID, 0, -1, -1, -1, 100.0);
    printf("%f, %f, %f", gShopLocations[rand][0], gShopLocations[rand][1], gShopLocations[rand][2]);
As you can see it says "new rand = random(sizeof(gShopLocations));", this is wrong because it loads one randomly selected coord. How can I make it load all of them?
Reply
#2

you mean a loop through the entire array?
pawn Код:
for(new rand=0;rand<sizeof(gShopLocations);rand++)
{
    CreateDynamicPickup(1239, 1, gShopLocations[rand][0], gShopLocations[rand][1], gShopLocations[rand][2], -1, -1, INVALID_PLAYER_ID, 150.0);
    CreateDynamicMapIcon(gShopLocations[rand][0], gShopLocations[rand][1], gShopLocations[rand][2], 38, COLOR_WHITE, -1, -1, INVALID_PLAYER_ID, 150.0);
    CreateDynamic3DTextLabel("SHOP", COLOR_RED, gShopLocations[rand][0], gShopLocations[rand][1], gShopLocations[rand][2], 150.0, INVALID_PLAYER_ID, INVALID_VEHICLE_ID, 0, -1, -1, -1, 100.0);
    printf("%f, %f, %f", gShopLocations[rand][0], gShopLocations[rand][1], gShopLocations[rand][2]);
}
remember to assign each pickup to a variable for refering it later.
Reply
#3

Ah! That's it. Thanks for that mate.
Reply
#4

I made the code easier to read, and I used better namings for it. Here it is:
pawn Код:
new Float:x, Float:y, Float:z;
for(new shop = 0; shop < sizeof(gShopLocations); shop++) {
    x = gShopLocations[shop][0], y = gShopLocations[shop][1], z = gShopLocations[shop][2];
    CreateDynamicPickup(1239, 1, x, y, z, -1, -1, INVALID_PLAYER_ID, 150.0);
    CreateDynamicMapIcon(x, y, z, 38, COLOR_WHITE, -1, -1, INVALID_PLAYER_ID, 150.0);
    CreateDynamic3DTextLabel("SHOP", COLOR_RED, x, y, z, 150.0, INVALID_PLAYER_ID, INVALID_VEHICLE_ID, 0, -1, -1, -1, 100.0);
    printf("%f, %f, %f", x, y, z);
}
Reply
#5

I'm not bothered about how clean it is, just how it works. Thanks though
Reply
#6

Okay, but I would recommend that you use appropriate naming for your script, if you ever need help from other people, or work with another developer, it makes everything easier if your script is named appropriately.
Reply


Forum Jump:


Users browsing this thread: 5 Guest(s)