Getting string from Array
#1

I've tried looking around for how to do this, but I don't think that I can word it right..

I've got this array which contains 7 items.

pawn Код:
enum VehicleListEnum {
    VehModel,
    VehName[64],
    VehType,
    VehWindows,
    VehTrunk,
    VehDoors,
    VehPrice
}

new VehicleList[212][VehicleListEnum] = {
//  {ID, NAME, TYPE, WINDOWS, TRUNK, DOORS, PRICE},
    {400, "Landstalker", VEHICLE_TYPE_CAR, 4, 1, 4, 000000},
    {401, "Bravura", VEHICLE_TYPE_CAR, 2, 1, 2, 000000},
    {402, "Buffalo", VEHICLE_TYPE_CAR, 2, 1, 2, 000000},
    {403, "Linerunner", VEHICLE_TYPE_TRUCK, 2, 0, 2, 000000},
    {404, "Perenniel", VEHICLE_TYPE_CAR, 4, 1, 4, 000000},
}
I need a GetVehicleNameFromID function that uses this array. An example or explanation would be appreciated.
Thanks.
Reply
#2

Just do this:

PHP код:
//%0 = the id
#define GetVehicleNameFromID(%0) VehicleList[%0][VehName] 
Reply
#3

new Model = GetVehicleModel(GetPlayerVehicleID(playerid));
format(string,sizeof(string),"You are in %s",VehicleList[Model-400][VehName]);
Reply
#4

Quote:
Originally Posted by Kaliber
Посмотреть сообщение
Just do this:

PHP код:
//%0 = the id
#define GetVehicleNameFromID(%0) VehicleList[%0][VehName] 
When I use this under OnPlayerStateChange, the value is wrong. For example getting into an Infernus(411), returns 66.
Reply
#5

PHP код:
// Function to get the model index from VehicleList array
GetVehicleModelIndex(model)
{
    for (new 
isizeof(VehicleList); i++)
    {
        if (
VehicleList[i][VehModel] == model)
        {
            return 
i;
        }
    }
    return -
1;
}
// Local variable to get the index...
new index GetVehicleModelIndex(GetVehicleModel(yourvehicleidhere));
// And while formatting a string to get the vehicle's name
VehicleList[index][VehName
Try this?
Reply
#6

pawn Код:
COMMAND:veh(playerid, params[])
{
    new vid = strval(params);
    new modelid = GetVehicleNameFromID(vid), Float:Pos[3], Float:Angle;

    if(sscanf(params, "d", modelid)) return SendClientMessage(playerid, -1, "Error. Usage: /veh [modelid]");

    if(IsPlayerInAnyVehicle(playerid)) {
        RemovePlayerFromVehicle(playerid);
    }
    else {
        GetPlayerPos(playerid, Pos[0], Pos[1], Pos[2]);
        GetPlayerFacingAngle(playerid, Angle);
       
        new veh = CreateVehicle(modelid, Pos[0], Pos[1], Pos[2], Angle, -1, -1, 0);
        PutPlayerInVehicle(playerid, veh, 0);
    }
    return 1;
}
Should work in my eyes.. Only spawns a vehicle when using the id instead of the name.
Reply
#7

Quote:
Originally Posted by Kaliber
Посмотреть сообщение
Just do this:

PHP код:
//%0 = the id
#define GetVehicleNameFromID(%0) VehicleList[%0][VehName] 
You're not going to subtract 400 from the model ID? You should probably check if the model ID is valid too. Therefore you'd probably be better off using a standard function.

Now I'm not sure whether your "GetVehicleNameFromID" function uses a vehicle ID or a model ID, because you haven't shown it to us here, so I'm going to assume that you mean vehicle ID.

From a Vehicle ID:
pawn Код:
GetVehicleNameFromID(vehicleid)
{
    new mod = GetVehicleModel(vehicleid), str[64] = "Unknown";
    if(400 <= mod <= (sizeof(VehicleList) + 399)) format(str, sizeof(str), "%s", VehicleList[(mod - 400)][VehName]);
    return str;
}
From a Model ID:
pawn Код:
GetVehicleNameFromModel(modelid)
{
    new str[64] = "Unknown";
    if(400 <= modelid <= (sizeof(VehicleList) + 399)) format(str, sizeof(str), "%s", VehicleList[(modelid - 400)][VehName]);
    return str;
}
Also, I don't think you need '64' cells for a Vehicle Name, but that's just me :>
Reply
#8

That is exactly what I need but it returns "error 033: array must be indexed (variable "-unknown-")" when used in
pawn Код:
new modelid
new modelname = GetVehicleNameFromModel(modelid);
Reply
#9

Vehicle names are a string... not an integer.

pawn Код:
new modelname[64];
strcat(modelname, GetVehicleNameFromModel(modelid));
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)