SA-MP Forums Archive
Select random - 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: Select random (/showthread.php?tid=657955)



Select random - KinderClans - 18.08.2018

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"


Re: Select random - EzeHermes - 19.08.2018

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


Re: Select random - KinderClans - 19.08.2018

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)


Re: Select random - EzeHermes - 20.08.2018

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


Re: Select random - KinderClans - 20.08.2018

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]));



Re: Select random - Calisthenics - 20.08.2018

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).


Re: Select random - KinderClans - 20.08.2018

Do u mind giving me an example?


Re: Select random - Sew_Sumi - 20.08.2018

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.


Re: Select random - KinderClans - 20.08.2018

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.


Re: Select random - Calisthenics - 20.08.2018

/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


Re: Select random - KinderClans - 20.08.2018

Does SetPVarInt saves only integrers and not strings?


Re: Select random - Calisthenics - 20.08.2018

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.


Re: Select random - Sew_Sumi - 20.08.2018

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.


Re: Select random - KinderClans - 20.08.2018

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


Re: Select random - Zeth - 20.08.2018

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.


Re: Select random - KinderClans - 20.08.2018

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