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

Saving position of players

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

[top]Code

Code 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
{
	// Get player name (the key for the position store)
	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 = player.GetName();
 
		// If there is position data for that name, set it
		if (playerPos.count(name))
		{
			// Using SetCell so it also works for interiors
			player.SetCell(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);
		}
	}
}