Hello iwalidza,
since you do not provide any design wishes and I have no experience with inventory system design, I will limit my contribution to the technical side of inventory systems. You may want to create new kinds of items for roleplay such as keys or identity certificates that players have to collect and treasure.
An inventory system is defined as a set of items that belong to peds or players. Items can be taken out or put in at wish. Thus we want to attach a table of items for at least each player on the server and synchronize the items with the clients.
server.Lua
-- We want to create items definitions here.
local item_definitions = {};
local function register_item(id, name, description)
local def = {
["name"] = name,
["id"] = id,
["description"] = description
};
item_definitions[id] = def;
end
register_item(1, "Knife", "Sharp item to cut things");
register_item(2, "Sword", "Sharp weapon for slashing");
register_item(3, "Key", "Used to access secret areas in the game");
-- Item
local player_inventories = {};
addEventHandler("onPlayerQuit", root, function()
player_inventories[source] = nil;
end
);
local function find_existing_player_inv_slot(inv_items, item_id)
for m,n in ipairs(inv_items) do
if (n.item_id == item_id) then
return n;
end
end
return false;
end
local function give_item_to_player(player, item_id, count)
local inv = player_inventories[player];
local item_carry = find_existing_player_inv_slot(inv, item_id);
if (item_carry) then
item_carry.count = item_carry.count + count;
return true;
end
local new_carry = {
["item_id"] = item_id,
["count"] = count
};
table.insert(inv, new_carry);
return true;
end
addEventHandler("onPlayerJoin", root, function()
local inventory = {};
player_inventories[source] = {};
give_item_to_player(source, 1, 1);
give_item_to_player(source, 2, 1);
give_item_to_player(source, 3, 1);
end
);
(code has not been tested)
This is a sample system I just created. There is a knife, sword and key. At join time each player does receive a copy of any item. Now imagine that you have a marker that you can only enter with a key.
server.Lua
local function does_player_have_item(player, item_id)
local inventory = player_inventories[player];
local slot = find_existing_player_inv_slot(inventory, item_id);
if (slot) and (slot.count > 0) then
return true;
end
return false;
end
local function remove_player_item(player, item_id, count)
local inventory = player_inventories[player];
for m,n in ipairs(inventories) do
if (n.item_id == item_id) and (n.count > 0) then
local can_remove = math.min(count, n.count);
n.count = n.count - can_remove;
count = count - can_remove;
if (count == 0) then
break;
end
end
end
end
local marker = createMarker(0, 0, 3, "cylinder");
addEventHandler("onMarkerHit", marker, function(hitElement)
if not (getElementType(hitElement) == "player") then return; end;
if not (does_player_have_item(hitElement, 3)) then
outputChatBox("You cannot enter this zone because you have no key.");
return;
end
outputChatBox("Using key to enter the zone.");
remove_player_item(hitElement, 3, 1);
end
);
This script creates a marker in the middle of the map. If you hit the marker then you use a key. Once you have used your starting key then you cannot successfully hit the marker anymore. The message should change the second time you hit the marker. The marker does only work with players.
I hope I could help you with this