Select random
#1

So, i have this array:

pawn Код:
new const g_LocationData[][e_LocData] =
{
    {"Bank", 2315.952880, -1.618174, 26.742187},
    {"LS Atrium", 1710.433715, -1669.379272, 20.225049}
};
Enum:

pawn Код:
enum e_LocData
{
    e_LocName[32],
    Float:e_LocX,
    Float:e_LocY,
    Float:e_LocZ
};
I want to select a random destionation and create a checkpoint if i type /destination, but i don't know how to loop the array i created, help?

And if possible, show the random name destionation, ex: "You teleported to Bank"
Reply
#2

pawn Код:
new randomloc = random(sizeof(g_LocationData));
new Msg[48];
format(Msg, sizeof(Msg), "Teleported to: %s", g_LocationData[randomloc][e_LocName]);
SetPlayerPos(playerid, g_LocationData[randomloc][e_LocX], g_LocationData[randomloc][e_LocY], g_LocationData[randomloc][e_LocZ]);
SendClientMessage(playerid, -1, Msg);
https://sampwiki.blast.hk/wiki/Random
Reply
#3

Thank you, and how to store generated random destination name in a pvar to recall it in a second moment in another function? (not the command)
Reply
#4

pawn Код:
SetPVarString(playerid,"RandomLocation",g_LocationData[randomloc][e_LocName]);
RandomLocation is the name of the variable, you can change it.
Reply
#5

Ok thanks, to retrieve it in show in a message, will work in this way?

pawn Код:
new string[80];
GetPVarString(playerid, "RandomLocation",g_LocationData[randomloc][e_LocName]);
format(string, sizeof(string), "Last teleport was to: %s" GetPVarString(playerid, "RandomLocation",g_LocationData[randomloc][e_LocName]));
Reply
#6

Passing GetPVarString directly in format will return the length of the string. This is how it should be used: https://sampwiki.blast.hk/wiki/GetPVarString

You can also store the index of the array instead of the name which will be easier to use (pass it directly in g_LocationData).
Reply
#7

Do u mind giving me an example?
Reply
#8

Quote:
Originally Posted by KinderClans
Посмотреть сообщение
Do u mind giving me an example?
Do you mind keeping this thread up? I've struggled with this sort of thing in the past, and it would be good to have it simply shown and remain for others.
Reply
#9

Did in this way:

pawn Код:
new const g_LocationData[][e_LocData] =
{
    {"Bank", 2315.952880, -1.618174, 26.742187},
    {"LS Atrium", 1710.433715, -1669.379272, 20.225049}
};

enum e_LocData
{
    e_LocName[32],
    Float:e_LocX,
    Float:e_LocY,
    Float:e_LocZ
};

CMD:tele(playerid, params[])
(
    new randomloc = random(sizeof(g_LocationData));
    new Msg[48];
    format(Msg, sizeof(Msg), "Teleported to: %s", g_LocationData[randomloc][e_LocName]);
    SetPlayerPos(playerid, g_LocationData[randomloc][e_LocX], g_LocationData[randomloc][e_LocY], g_LocationData[randomloc][e_LocZ]);
    SendClientMessage(playerid, -1, Msg);
    SetPVarString(playerid,"RandomLocation",g_LocationData[randomloc][e_LocName]);
    return 1;
)

CMD:last(playerid, params[])
{
    new string[80];
    GetPVarString(playerid, "RandomLocation",g_LocationData[randomloc][e_LocName]);
    format(string, sizeof(string), "Last teleport was to: %s" GetPVarString(playerid, "RandomLocation",g_LocationData[randomloc][e_LocName]));
    return 1
}
But i'm getting "undefinel symbol: randomloc" at /last command.
Reply
#10

/tele
pawn Код:
SetPVarInt(playerid, "RandomLocation", randomloc);
/last
pawn Код:
if (GetPVarType(playerid, "RandomLocation")) // PVar has been set
{
    new string[80];
    format(string, sizeof(string), "Last teleport was to: %s", g_LocationData[GetPVarInt(playerid, "RandomLocation")][e_LocName]);
    SendClientMessage(playerid, -1, string);
}
// else SendClientMessage(playerid, -1, "You did not teleport anywhere.);
Reference: https://sampwiki.blast.hk/wiki/GetPVarType
Reply
#11

Does SetPVarInt saves only integrers and not strings?
Reply
#12

Quote:
Originally Posted by KinderClans
Посмотреть сообщение
Does SetPVarInt saves only integrers and not strings?
Yes. But if you know the index, you know the name of the teleport.
Reply
#13

What are you doing?


Do you guys even know what PVars and SVars are actually for?


The const, and random are all good... But using PVars for anything that is in the script itself, and not shared across any other scripts, is just a waste.
Reply
#14

It's not a waste if you want to retrirve a random array name (which is my case)..
Reply
#15

Dosen't matter whether its Pvar or normal variables, all you need to do is random(sizeof array) and save it in a pvar/normal variable and use if further wherever you want to and with having the index number, you have access to all the data members in that array.
Reply
#16

Quote:
Originally Posted by Calisthenics
Посмотреть сообщение
//code
Thank you it works perfectly.
Reply


Forum Jump:


Users browsing this thread: 5 Guest(s)