Help
Remember Me?
Home
Forum
Download
Scripting docs
Wiki
Main
Special Pages
Books
Recent Changes
Blogs
Wiki
Main
Saving items 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 (2)
source
history
Viewing source for:
Saving items of players
This script saves the players inventory. The inventory 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 <algorithm> using namespace vaultmp; template <typename V> using mBaseHash = std::unordered_multimap<Base, V, _hash_Base>; struct ItemData { UCount count; Value condition; State equipped; }; std::unordered_map<String, mBaseHash<ItemData>> playerItems; Void VAULTSCRIPT OnPlayerDisconnect(ID player, Reason reason) noexcept { // Get player name (the key for the item store) String name = GetName(player); // Clear player item store playerItems.erase(name); // Get all items IDVector items = Item::GetList(); for (const auto& id : items) { Item item(id); // baseID of the item Base base = item.GetBase(); static const Base PipBoy = static_cast<Base>(0x00015038); static const Base PipBoyGloves = static_cast<Base>(0x00025B83); // If the item belongs to the player, save it. Don't save PipBoy stuff because that's equipped by default if (base != PipBoy && base != PipBoyGloves && item.GetItemContainer() == player) { UCount count = item.GetItemCount(); Value condition = item.GetItemCondition(); State equipped = item.GetItemEquipped(); // Find an existing item for stacking auto it = find_if(playerItems[name].begin(), playerItems[name].end(), [&](const std::pair<const Base, ItemData>& itemData) { return itemData.first == base && itemData.second.condition == condition && itemData.second.equipped == False;}); // Either use the existing item data or create a new entry ItemData& data = it != playerItems[name].end() ? it->second : playerItems[name].emplace(base, ItemData())->second; // Store the data data.count += count; data.condition = condition; data.equipped = equipped; } } } Base VAULTSCRIPT OnPlayerRequestGame(ID player) noexcept { // Player object Player pplayer(player); // Get player name (the key for the item store) String name = pplayer.GetName(); for (const auto& item : playerItems[name]) { // Add the item pplayer.AddItem(item.first, item.second.count, item.second.condition); // Equip the item if it was equipped if (item.second.equipped) pplayer.EquipItem(item.first, True, False); } return static_cast<Base>(0x00000000); } [/highlight] [category]Script examples[/category]