Forum

> > CS2D > Scripts > Local variables in need to know...
Forums overviewCS2D overview Scripts overviewLog in to reply

English Local variables in need to know...

12 replies
To the start Previous 1 Next To the start

old Local variables in need to know...

baRD
User Off Offline

Quote
Can someone teach me how to make variables that each players can own?...
like a custom command that have a delay time to use again?
and i need help about function names with dots like function menu.lel()

old Re: Local variables in need to know...

Rainoth
Moderator Off Offline

Quote
The best choice would be to read thread "[GUIDE] How to script".

To put it simply, you make a table
myTable = {}

then use one of the hooks which provide IDs (spawn for example)
myTable[id] = 10

so you give value 10 to every player.

The same thing applies to the "menu.lel()"
You'd make
1
2
3
4
5
myMenu = {
  lel = your function that calls the menu or does whatever
}
-- and then you call it with
myMenu.lel()

In short, learn how table system works in lua.

old Re: Local variables in need to know...

Masea
Super User Off Offline

Quote
1
2
3
4
5
6
7
8
9
players = {}

function _join(id)
	players[id] = {exp = 10}
end

function _addExp(id, exp)
	players[id].exp = players[id].exp + exp
end
Like this.

old Re: Local variables in need to know...

Talented Doge
User Off Offline

Quote
Basically it is called an array; In the tongue of programmers, they could still be called arrays, or tables.

You want to give a price to all the products in your shop, suppose you have the following three:
• A photo of Johnny Depp
• A copy of Microsoft Office
• A mysterious sweetroll

They are all the products of your shop, so they should be under the same father category:
shop = {}


Yet they have completely different properties, to identify them you have to give them an index:
1
2
3
4
5
shop = {
["pjd"] = "Photo of Johnny Depp", 
["mso"] = "Microsoft Office", 
["sr"] = "Sweetroll"
}
.

You wish to give them more values, so you change the values inside the indices to be another tables:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
shop = {
["pjd"] = {
			["Name"] = "Photo of Johnny Depp",
			["Price"] = 100
		}
,
["mso"] = {
			["Name"] = "Microsoft Office",
			["Price"] = 1000000000
		}
,
["sr"]	= {
			["Name"] = "Sweetroll",
			["Price"] = 99999
		}
}

Then when you run a program or function, you could simply call the values of the table to know what you want to. For example I want to know the price of a sweetroll:
1
2
3
4
5
function findsrprice(table)
	print("Price of Sweetroll: "..table["sr"]["Price"])
end

findsrprice(shop)

The outcome should be:
Price of Sweetroll: 99999


And by converting this concept a little bit, you will know how to allocate a dynamic individual variable for players:
1
2
3
4
5
6
7
number = {}

function giveafkingnumbertoplayers()
	for i = 1, #player(0, "table") do
		number[i] = math.random(1, 1000000)
	end
end

Some people advice that you set the length of the table before you ever really give any useful values to it. It should be of mere impact to the game in CS2D, but it is a good habit to keep, though that I do not:
1
2
3
4
5
6
7
8
9
10
11
12
13
function initarray(limit)
	local x = {}
	for i = 1, limit do
		x[i] = 0
	end
        return x
end

nonsense = initarray(3)

nonsense[2] = "FOR SKYRIIIIIIIIIIIIIIIM"

print(nonsense[2])

old Re: Local variables in need to know...

Yates
Reviewer Off Offline

Quote
They use that crap to initialize multi-dimensional tables mainly used for custom player information.

Here we define a table:
1
_player = {}

And here we define three tables within a table creating a multi-dimensional table.
1
2
3
4
5
_player = {
    [1] = {},
    [2] = {},
    [3] = {},
}

What they do is create a function to create these tables within a table automatically. But you can do this for players like so:

1
2
3
4
5
6
_player = {} -- Define the player table

function _spawn(id)
    _player[id] = {} -- Define the table within the player table using the player ID
end
addhook("spawn", "_spawn")
edited 1×, last 12.07.17 05:01:02 pm

old Re: Local variables in need to know...

Yates
Reviewer Off Offline

Quote
To add onto the above I recommend always sticking to the parameter names used in the documentation. This will make sure any programmer can easily read your code if they do the same (which they should).
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview