[Tutorial] [SA:MP] Save spawn location [NOOB-FRIENDLY!] [GM]

Results 1 to 7 of 7
  1. #1
    Super Moderator Biesmen is online now
    Super ModRank
    Apr 2007 Join Date
    2,431Posts

    [Tutorial] [SA:MP] Save spawn location [NOOB-FRIENDLY!] [GM]

    [Tutorial] [SA:MP] Save spawn location [NOOB-FRIENDLY!] [GM]
    I think this tutorial will be useful for many people. Especially for the new "coders". I added this spawn location save to my own RPG Gamemode. So it's working, don't worry.

    Tutorial:

    Step 0.
    We will have to define dini, so we can use it.
    Add this line at the top of your script, below #include <a_samp> :
    Code:
    #include <dini>
    Done? Head to step 1.

    Step 1.
    Let's use the public function public OnPlayerDisconnect(playerid, reason).
    Find that function. CTRL+F.
    If you found it, we can continue.
    First we will have to define some things.
    Add this at the top of the public function:
    Code:
    new file[128], pname[MAX_PLAYER_NAME];
    new Float:x, Float:y, Float:z;
    Step 2.
    Now we will have to make clear what pname is. And we will have to make sure a file will be created to save the data.
    Add this below the line we added at Step 1.
    Code:
    GetPlayerName(playerid, pname, sizeof(pname));
    format(file, sizeof(file), "\\SavePos\\%s.ini", pname);
    Step 3.
    We made the "script" clear it will have to load/use a file called "playername.ini" for the data. But it doesn't know if that file exists yet. If it exists, it will save the data. If it doesn't exist, it won't save the data. So, let's make it add the file if it doesn't exist.
    Add this below the lines we added at Step 2.
    Code:
    if(!dini_Exists(file))
    dini_Create(file);
    PAUSE
    Let's check the script.
    This is what you currently have:
    Code:
    public OnPlayerDisconnect(playerid, reason)
    {
    public OnPlayerDisconnect(playerid, reason)
    {
    new file[128], pname[MAX_PLAYER_NAME];
    new Float:x, Float:y, Float:z;
    GetPlayerName(playerid, pname, sizeof(pname));
    format(file, sizeof(file), "\\SavePos\\%s.ini", pname);
    if(!dini_Exists(file))
    dini_Create(file);
    return 1;
    }
    If everything is right, head to Step 4.

    Step 4.
    We will have to get the player position of the player.
    We can do this by adding this line below dini_create(file);
    Add this line below dini_create(file);:
    Code:
    GetPlayerPos(playerid, x, y, z);
    Done? Go to Step 5.
    Don't worry about the
    "return 1;
    }".

    Step 5.
    Now we will have to make it set the X,Y,Z position in the "username.ini" file.
    We can do that by adding this below the line we added at Step 4.
    Code:
    dini_FloatSet(file, "posX", x);
    dini_FloatSet(file, "posY", y);
    dini_FloatSet(file, "posZ", z);
    This will set the X, Y, and Z position of the player.

    Now we are done with the public function OnPlayerDisconnect.
    We are not done with the script! We still have to make the player spawn at the saved location.

    PAUSE
    This is how the function should look like!
    Code:
    public OnPlayerDisconnect(playerid, reason)
    {
    new file[128], pname[MAX_PLAYER_NAME];
    new Float:x, Float:y, Float:z;
    GetPlayerName(playerid, pname, sizeof(pname));
    format(file, sizeof(file), "\\SavePos\\%s.ini", pname);
    if(!dini_Exists(file))
    dini_Create(file);
    GetPlayerPos(playerid, x, y, z);
    dini_FloatSet(file, "posX", x);
    dini_FloatSet(file, "posY", y);
    dini_FloatSet(file, "posZ", z);
    return 1;
    }
    Is everything right?
    Head to Step 6.

    Step 6.
    We will have to use the public function "OnPlayerSpawn(playerid)".
    For some of you this function exists, for some of you it doesn't.
    Let's find this function. CTRL + F then type "OnPlayerSpawn". If you can't find it, add it manually:
    Code:
    public OnPlayerSpawn(playerid)
    {
    return 1;
    }
    If you have this function, add this line below the bracket "{".
    We will have to define some things, again. I will not explain these lines one by one, we already used them at the OnPlayerDisconnect function.
    Code:
    new file[128], pname[MAX_PLAYER_NAME];
    new Float:x, Float:y, Float:z;
    GetPlayerName(playerid, pname, sizeof(pname));
    format(file, sizeof(file), "\\SavePos\\%s.ini", pname);
    Done? head to Step 7.

    Step 7.
    This will be new for you.
    We will have to make the script look for the lines "posX, posY, posZ".
    We can do that by adding these lines below the liens we added at Step 6.
    Code:
    x = dini_Float(file, "posX");
    y = dini_Float(file, "posY");
    z = dini_Float(file, "posZ");
    Done? Head to Step 8.

    Step 8.
    Okay, the script checked for these lines. Now we will have to make the player spawn at the coordinates we saved.
    Add this line below the latest line we added at Step 7.
    Code:
    SetPlayerPos(playerid, x, y, z);
    We are ALMOST done.
    We're done with the script, but we still have to do something.

    This is how the script should look like:

    Code:
    public OnPlayerDisconnect(playerid, reason)
    {
    new file[128], pname[MAX_PLAYER_NAME];
    new Float:x, Float:y, Float:z;
    GetPlayerName(playerid, pname, sizeof(pname));
    format(file, sizeof(file), "\\SavePos\\%s.ini", pname);
    if(!dini_Exists(file))
    dini_Create(file);
    GetPlayerPos(playerid, x, y, z);
    dini_FloatSet(file, "posX", x);
    dini_FloatSet(file, "posY", y);
    dini_FloatSet(file, "posZ", z);
    return 1;
    }
    public OnPlayerSpawn(playerid)
    {
    new file[128], pname[MAX_PLAYER_NAME];
    new Float:x, Float:y, Float:z;
    GetPlayerName(playerid, pname, sizeof(pname));
    format(file, sizeof(file), "\\SavePos\\%s.ini", pname);
    x = dini_Float(file, "posX");
    y = dini_Float(file, "posY");
    z = dini_Float(file, "posZ");
    SetPlayerPos(playerid, x, y, z);
    return 1;
    }
    Now, go to Step 9.

    Step 9.
    The file wants to create at the folder "SavePos". But that folder doesn't exist. So we will have to add it manually.
    Go to your scriptfiles folder (Default: C:\Program Files\Rockstar Games\GTA San Andreas\Samp Server\scriptfiles).
    Then add a new folder called:
    SavePos.

    Now we are done!

    Thank you and good luck.
    Questions? Problems? bugs? Post them here.
    Last edited by Biesmen; 14-10-10 at 08:23 PM.
    Forum Rules | Account Support | Subscribe
    RaGEZONE Facebook

    "The reason why people give up so fast is because they tend to look at how far they still have to go, instead of how far they have gotten."


  2. #2
    Account Inactive Juunior is offline
    InactiveRank
    Apr 2005 Join Date
    EarthLocation
    629Posts

    Re: [Tutorial] [SA:MP] Save spawn location [NOOB-FRIENDLY!] [GM]

    Great :) Pretty easy to follow, and useful.

    Maybe it would be a good idea though to check if file exists at OnPlayerSpawn as well? In case player visits the server for the first time. If it doesn't then spawn at default coords.

  3. #3
    Super Moderator Biesmen is online now
    Super ModRank
    Apr 2007 Join Date
    2,431Posts

    Re: [Tutorial] [SA:MP] Save spawn location [NOOB-FRIENDLY!] [GM]

    Quote Originally Posted by Juunior View Post
    Great :) Pretty easy to follow, and useful.

    Maybe it would be a good idea though to check if file exists at OnPlayerSpawn as well? In case player visits the server for the first time. If it doesn't then spawn at default coords.
    You don't need to. Everyone has an "AddPlayerClass" function at the callback "OnGameModeInit".
    Forum Rules | Account Support | Subscribe
    RaGEZONE Facebook

    "The reason why people give up so fast is because they tend to look at how far they still have to go, instead of how far they have gotten."

  4. #4
    Alpha Member bramdebouvere is offline
    Alpha MaleRank
    Aug 2006 Join Date
    BelgiumLocation
    2,409Posts

    Re: [Tutorial] [SA:MP] Save spawn location [NOOB-FRIENDLY!] [GM]

    Very nice guide :) i hope we get some new developers :p

  5. #5
    Account Inactive ILitex is offline
    InactiveRank
    Oct 2010 Join Date
    TorontoLocation
    91Posts

    Re: [Tutorial] [SA:MP] Save spawn location [NOOB-FRIENDLY!] [GM]

    Nice Tut. Helped Me! XD

  6. #6
    Alpha Member Aksel is offline
    Alpha MaleRank
    Mar 2009 Join Date
    1,909Posts

    Re: [Tutorial] [SA:MP] Save spawn location [NOOB-FRIENDLY!] [GM]

    Lol true noob friendly

  7. #7
    Account Inactive deadpool1991 is offline
    InactiveRank
    May 2007 Join Date
    12Posts

    Re: [Tutorial] [SA:MP] Save spawn location [NOOB-FRIENDLY!] [GM]

    i'm starting under the map for some reason :O



Advertisement