Forum

> > CS2D > Scripts > Tricks in CS2D Scripting that you might not know.
Forums overviewCS2D overview Scripts overviewLog in to reply

English Tricks in CS2D Scripting that you might not know.

49 replies
Page
To the start Previous 1 2 3 Next To the start

Poll Poll

How many tricks have you already know?

Only registered users are allowed to vote
All of them
29.23% (19)
None of them
40.00% (26)
A few of them
30.77% (20)
65 votes cast

old Re: Tricks in CS2D Scripting that you might not know.

Dousea
User Off Offline

Quote
Creating your own iterator.
1
2
3
4
5
6
7
8
9
local function iterator(index, table)
	index = index + 1
	
	return table[index], index
end

function each(table)
	return iterator, 0, table
end
Now you can use it like
1
2
3
for id in each(player(0, "table")) do
	print(id)
end
Note: each function works like ipairs but it returns the value first.

old Re: Tricks in CS2D Scripting that you might not know.

The Gajos
BANNED Off Offline

Quote
Name/Password generator in Lua.
Maybe it's not hard but useful
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
path = 'names.txt'
file = io.open(path,'w')

for i = 1,100 do
	local str = ''
	local maxx = 6
	for j = 1,maxx do
		local item = math.random(1,chars:len())
		str = str..chars:sub(item,item)
	end
	file:write(str..'\n')
end

file:close()
print('©000255000Names has been generated to '..path)

old Re: Tricks in CS2D Scripting that you might not know.

Marcell
Super User On Online

Quote
user The Gajos: Maybe if user DC could integrate LuaJIT well, then MD5 module would be great to us to generate passwords or something like that. But since it was discarded we cannot do it.

Btw, may your generetor can be nice for understood things, but as a real password generator it's very bad. Sorry.

old Re: Tricks in CS2D Scripting that you might not know.

MikuAuahDark
User Off Offline

Quote
@user The Gajos: You can use math.random to generate random number from 65 to 90 and transform it into character with string.char
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
local path="names.txt"
local file=io.open(path,"w")
local maxx=6

-- Method 1 - Write directly
for i=1,100 do
	for j=1,maxx do
		file:write(string.char(math.random(65,90)))
	end
	file:write("\n")
end

-- Method 2 - table.concat
for i=1,100 do
	local t={}
	for j=1,maxx do
		t[j]=string.char(math.random(65,90))
	end
	file:write(table.concat(t,"").."\n")
end

file:close()
print("©000255000Names has been generated to "..path)

Replace 65 with 97 and 90 to 122 to generate lowercase character instead

old Re: Tricks in CS2D Scripting that you might not know.

Dousea
User Off Offline

Quote
This one might really intrigue you, but some of you might know this.
1
2
3
4
5
6
7
8
9
10
11
local t = {
	v = true
}

function f(l)
	l.v = false
end

print(tostring(t.v))
f(t)
print(tostring(t.v))
So what are the 2 values that CS2D console will print?
Maybe you are expecting
1
2
true
true
well it's not actually. These are the values:
1
2
true
false
Yes, tables in Lua will send their address. You can modify a table's value within a function, within another file, within another module like ease. What can you do with this feature? Well a lot actually. I can't name you one. You might want to figure it out yourself.

old Re: Tricks in CS2D Scripting that you might not know.

Livia
User Off Offline

Quote
You can register player click events by giving him landmines and returning 1 on buildattempt hook whenever he tries to place one. It only works for left click but it's completely silent unlike knife setups.

Edit:
You can also register click events with mouse locations by equipping the player a portal gun and doing some packet inspection on server side. Whenever player shoots the portal gun it will include the cursor location data. Didn't test satchel/grenades though.
edited 2×, last 15.09.16 11:48:30 pm
To the start Previous 1 2 3 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview