Forum

> > CS2D > Scripts > Is tables shared?
Forums overviewCS2D overview Scripts overviewLog in to reply

English Is tables shared?

12 replies
To the start Previous 1 Next To the start

old Is tables shared?

Dovahkin
User Off Offline

Quote
I'm a little bit curious if tables are exactly shared to everyone on the map or only for me. Example :
1
Tbl = {"ex","x"}

Is it shared to everyone? Because I'm making an inventory system made from tables. Thank you.

old Re: Is tables shared?

Starkkz
Moderator Off Offline

Quote
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".

old Re: Is tables shared?

cortz
Super User Off Offline

Quote
If you meant if it was a global table or not then yes it is.
it belongs to everything in the server

old Re: Is tables shared?

Dovahkin
User Off Offline

Quote
@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?

old Re: Is tables shared?

EngiN33R
Moderator Off Offline

Quote
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:

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 >

old Re: Is tables shared?

Dovahkin
User Off Offline

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

old Re: Is tables shared?

EngiN33R
Moderator Off Offline

Quote
I have already written how to display a player's personal inventory to them. This is how to add something to their inventory:

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

old Re: Is tables shared?

mozilla1
User Off Offline

Quote
You can create an array by doing this so:

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:

1
myowntable[1]="Testing"

So the player with ID 1 will have the value "Testing".

old Table check

KimKat
GAME BANNED Off Offline

Quote
@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.
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.

old Re: Is tables shared?

KimKat
GAME BANNED Off Offline

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

old Re: Is tables shared?

Dovahkin
User Off Offline

Quote
@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
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

old Re: Is tables shared?

EngiN33R
Moderator Off Offline

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

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
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview