This site has been archived and made available for preservation purposes. No edits can be made.

Saving position of players

(Difference between revisions)
Return to current revision
  1.  
    This script saves the players position. The position data will be saved when the player leaves the server, and will be reassigned once a player with the same name joins (which is weak due to player name conflicts, etc.; however, you can easily change this script to use a combination of name / password).
    [h="2"] Code [/h]
    [highlight=cpp]
    #include "vaultscript.h"
    #include <unordered_map>
    #include <tuple>

    using namespace vaultmp;

    std::unordered_map<String, std::tuple<Cell, Value, Value, Value>> playerPos;

    Void VAULTSCRIPT OnPlayerDisconnect(ID player, Reason reason) noexcept
    {
  2. -
    // Get player name (the key for the item store)
  3. +
    // Get player name (the key for the position store)
  4.  
    String name = GetName(player);

    // Clear player position
    playerPos.erase(name);

    // Get the player position
    Value X, Y, Z;
    GetPos(player, X, Y, Z);

    // Get the player cell
    Cell cell = GetCell(player);

    // Save the player position
    playerPos.emplace(name, std::make_tuple(cell, X, Y, Z));
    }

    Void VAULTSCRIPT OnSpawn(ID object) noexcept
    {
    // Player object
    Player player(object);

    if (player)
    {
    String name = GetName(object);

    // If there is position data for that name, set it
    if (playerPos.count(name))
    {
    // Using SetCell so it also works for interiors
    SetCell(object, std::get<0>(playerPos[name]), std::get<1>(playerPos[name]), std::get<2>(playerPos[name]), std::get<3>(playerPos[name]));
    // Remove the data so it doesn't set the position on any further spawn
    playerPos.erase(name);
    }
    }
    }
    [/highlight]
    [category]Script examples[/category]