English Is tables shared?

12 replies
Goto Page
To the start Previous 1 Next To the start
25.10.13 02:07:45 am
Up
Dovahkin
User
Offline Off
I'm a little bit curious if tables are exactly shared to everyone on the map or only for me. Example :
Code:
1
Tbl = {"ex","x"}


Is it shared to everyone? Because I'm making an inventory system made from tables. Thank you.
IMG:http://i561.photobucket.com/albums/ss59/killerboy_04/76561198036665859.png
25.10.13 04:28:01 am
Up
Starkkz
Moderator
Offline Off
It's not. Only you can access to the script data unless you added something that lets the players control your lua script, if you meant that by "shared".
lol
25.10.13 05:56:21 am
Up
cortz
Super User
Offline Off
If you meant if it was a global table or not then yes it is.
it belongs to everything in the server
25.10.13 06:10:28 am
Up
Dovahkin
User
Offline Off
@user Starkkz: No, Must be in a game. Example like when I add something on the table and I can access the table by pressing f2, but How do you do it for only me to see the added item to the table?
IMG:http://i561.photobucket.com/albums/ss59/killerboy_04/76561198036665859.png
25.10.13 09:34:43 am
Up
EngiN33R
Moderator
Offline Off
A table is a value, and values can't be "shared to everyone on the map". The table exists in the script, and the script can access it. It's up to you to decide whether it's accessible by everyone or not.

I understand what you mean and what you want - you want a "personal" table for each player on the server. You can achieve that by basically creating a master table that in turn contains one table for each player.

Here's some simple example code:

Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
inv = {} -- Create the master table

addhook("join","createinv")
function createinv(id)
     inv[id] = {"ex","x"} -- Create the inventory table that's exclusive to player 'id'
end

addhook("serveraction","displayinv")
function displayinv(id,a)
     if a == 1 then
          menu(id,"Inventory,"..inv[id][1]..","..inv[id][2]) -- Show the player their inventory
     end
end

addhook("leave","destroyinv")
function destroyinv(id)
     inv[id] = nil -- Destroy a player's inventory when they leave the server
end


Spoiler >
I code, therefore I exist. | Visit my blog for Lua tips and other interesting info
25.10.13 01:33:03 pm
Up
Dovahkin
User
Offline Off
Thank you. But how can you add items to the table for only me to see it?

For example when somebody bought something on a shop then it will be stored on his/her inventory. But only he/she can see his personal inventory and the item he/she bought will be added to the inventory.
IMG:http://i561.photobucket.com/albums/ss59/killerboy_04/76561198036665859.png
25.10.13 02:42:51 pm
Up
EngiN33R
Moderator
Offline Off
I have already written how to display a player's personal inventory to them. This is how to add something to their inventory:

Code:
1
2
3
4
addhook("attack","additemtest")
function additemtest(id)
     table.insert(inv[id],"item") -- Inserts an item into a player's inventory every time they attack
end
I code, therefore I exist. | Visit my blog for Lua tips and other interesting info
25.10.13 03:20:57 pm
Up
M_
User
Offline Off
You can create an array by doing this so:

Code:
1
2
3
4
5
6
7
8
function init_array(length,mode) 
     local array={} 
     for i=1,length do 
          array[i]=mode end 
     return array 
end

myowntable=init_array(32,"")


So, everyone will have their own var space, with value "".
You can access your table by doing this:

Code:
1
myowntable[1]="Testing"


So the player with ID 1 will have the value "Testing".
25.10.13 07:14:26 pm
Up
KimKat
GAME BANNED
Offline Off
@user Dovahkin: The players don't literally own the array/table itself. It kind of just can be manipulated on the server-side however the clients connected to the server allows that manipulation to happen sort of say. A typical example for this is let's say a experience bar, the clients XP will be sent to the server-side Lua script hooks which will handle the experience seperately for each player. That's the best way I could describe it.

@user EngiN33R: It'd be user-friendly to also add a check for if table exists or not and if tables doesn't exist the Lua function will create new table or if table exists it will simply tell it exists.

I'll provide code for it as it's really useful.
Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
tbl={}
function c_table(t,c)
     if type(t)=="table" then
          print(string.format("%s exists.",t))
     else
           print(string.format("%s doesn't exist.",t))
          if type(c)=="number" and c==1 then
               local t={};return t
          else
               print(string.format("Wrong parameter value %q. Try %q instead.",c,1))
          end
     end
end
--[[ This creates new table based on checked table if checked table doesn't exist. ]]--
tbl2=c_table(tbl2,1) -- Create table if table doesn't exist.
--[[ This checks if table exists. ]]--
c_table(tbl) -- Check if table exists.
The c parameter has to be blank if you want to check a table and 1 if you want to create a table if the table happens to not exist.
26.10.13 02:49:29 am
Up
Dovahkin
User
Offline Off
@user KimKat: Thank you for helping. But how do you make a shop on that table. Seems a little bit complicated at newbie's
IMG:http://i561.photobucket.com/albums/ss59/killerboy_04/76561198036665859.png
26.10.13 03:08:19 am
Up
KimKat
GAME BANNED
Offline Off
I'm sorry but I won't code for you. You are supposed to code the socalled shop and tell me where you receive a error. Hence I don't know exactly what your definition of shop is. I'll provide a little snippet of how to create a table based on the information you've given though. Trying be as helpful as I can be but I won't do everything for you as it'll take the fun out of the challenge of being successful with this.
Code:
1
2
3
4
5
6
7
8
9
10
11
shop=c_table(shop,1) -- Checks if table exists already and if it don't. It creates table with name shop.
print(shop) -- The table named shop is now usable.

tbl={} -- Otherwise use this to create a table, whatever is preferred. There's many ways to accomplish things.

shop[1]="Value" -- Storing strings.
shop[2]=1234 -- Storing integers (signed or unsigned).
shop[3]=1 -- Storing objects (such as images).
shop[4]=tbl -- Storing tables within specific table indexes.

-- Learn Lua by watching some tutorials and you will comprehend it.
26.10.13 06:59:57 pm
Up
Dovahkin
User
Offline Off
@user EngiN33R: How do you make the inserted item function like a button just like in a menu with buttons in it. Because the inserted items goes every button numbers from 1-9 and I can't make all
Code:
1
If button == 1
because the inserted item are much likely has different names which is more hard to make as a button because the inserted item goes every button on the menu. @user EngiN33R: a little help? or @user KimKat: one more favor. Please and Thank you
IMG:http://i561.photobucket.com/albums/ss59/killerboy_04/76561198036665859.png
26.10.13 07:16:12 pm
Up
EngiN33R
Moderator
Offline Off
I don't entirely understand what you want. Do you want a menu that will act like a shop and add items to a player's inventory? Here's the (untested) code for what I think you want.

Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
pocinventory = {}

function table.createNested(s)
     local tbl = {}
     for k = 1, s do
          tbl[k] = {}
     end
     return tbl
end

pocinventory.shop_items = {
     [1] = "Item 1",
     [2] = "Item 2",
}

pocinventory.inventory = table.createNested(32)

addhook("leave","pocinventory.leave")
function pocinventory.leave(id)
     for k in pairs(pocinventory.inventory[id]) do pocinventory.inventory[id][k]=nil end -- Clear inventory
end

addhook("serveraction","pocinventory.serveraction")
function pocinventory.serveraction(id, a)
     if (a == 1) then -- Open shop (F2)
          menu(id, "Shop,"..table.concat(pocinventory.shop_items,",")) -- Display shop items in a menu (the first 9)
     end
     if (a == 2) then -- Open inventory (F3)
          menu(id, "Inventory,"..table.concat(pocinventory.inventory[id],",")) -- Display inventory in a menu (the first 9)
     end
end

addhook("menu", "pocinventory.menu")
function pocinventory.menu(id, menu, button)
     if (menu == "Shop") then
          if (pocinventory.shop_items[button]) then -- Just in case
               table.insert(pocinventory.inventory[id],pocinventory.shop_items[button]) -- Add item to inventory
     end
end
I code, therefore I exist. | Visit my blog for Lua tips and other interesting info
To the start Previous 1 Next To the start