Help
Remember Me?
Home
Forum
Download
Scripting docs
Wiki
Main
Special Pages
Books
Recent Changes
Blogs
Wiki
Main
Saving position of players
This site has been archived and made available for preservation purposes. No edits can be made.
If this is your first visit, be sure to check out the
FAQ
by clicking the link above. You may have to
register
before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.
article
discussion
source
history
Viewing source for:
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). [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 { // 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); } } } [/highlight] [category]Script examples[/category]