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 Poll Tricks in CS2D Scripting that you might not know.

Infinite Rain
Reviewer Off Offline

Quote
1st - Disabling right click on knife (attack 2 on knife)
1
2
3
4
5
6
addhook('always', 'alw')
function alw()
	for id = 1, 32 do
		parse('strip '.. id ..' 50')
	end
end


2nd - The short version of the following code:
1
2
3
4
5
6
7
function func(x)
	if x == nil then
		local y = 0
	else
		local y = x
	end
end

It's:
1
2
3
function func(x)
	local y = x or 0
end

3rd - Making TRULY random number from math.random function
1
2
3
4
5
6
function trullynumber(n1, n2, SEED)
	math.randomseed(SEED)
	return math.random(n1, n2)
end

msg(trullynumber(n1, n2, os.time())
(that's why minecraft mode generates ALWAYS same map)



----



I guess that the Lua scripting proffesionals already know this... but well, atleast newbies can learn!
edited 2×, last 25.05.12 12:47:49 pm

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

EngiN33R
Moderator Off Offline

Quote
user Infinite Rain has written
1
2
3
4
5
6
addhook('always', 'alw')
function alw()
	for id = 1, 32 do
		parse('strip '.. id ..' 50')
	end
end

That doesn't disable right click, it removes the knife completely if the player has other weapons.

user Infinite Rain has written
2nd - The short version of the following code:
1
2
3
4
5
6
7
function func(x)
	if x == nil then
		local y = 0
	else
		local y = x
	end
end

It's:
1
2
3
function func(x)
	local y = x or 0
end

Partially correct, although in the latter code, y will be set to 0 both if x is nil and if it's false - it's a detail, but sometimes it's crucial.

The third is true, although often just math.random suffices.

Also fixed the poll.

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

Infinite Rain
Reviewer Off Offline

Quote
user EngiN33R has written
user Infinite Rain has written
1
2
3
4
5
6
addhook('always', 'alw')
function alw()
	for id = 1, 32 do
		parse('strip '.. id ..' 50')
	end
end

That doesn't disable right click, it removes the knife completely if the player has other weapons.

user Infinite Rain has written
2nd - The short version of the following code:
1
2
3
4
5
6
7
function func(x)
	if x == nil then
		local y = 0
	else
		local y = x
	end
end

It's:
1
2
3
function func(x)
	local y = x or 0
end

Partially correct, although in the latter code, y will be set to 0 both if x is nil and if it's false - it's a detail, but sometimes it's crucial.

The third is true, although often just math.random suffices.

Also fixed the poll.


Noppee... It disables rightclick EVEN if you have another weapon I tested it, I had claws and knife

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

omg
User Off Offline

Quote
user tom282f3 has written
Easier way to disable AltAttack of a knife:
1
parse("mp_wpndmg_z1 knife 0")


thats not disabled, it just does 0 dmg -.- they mean u cant use right knife at all. and it doenst even work, it needs to be case sensitive

interesting random number generation

user EngiN33R has written
Partially correct, although in the latter code, y will be set to 0 both if x is nil and if it's false - it's a detail, but sometimes it's crucial.


too bad theres no xor in lua
edited 2×, last 25.05.12 06:44:16 pm

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

Infinite Rain
Reviewer Off Offline

Quote
user EngiN33R has written
Well, I tested it with pistol and knife - the knife got stripped and I obviously couldn't switch back to it, my only weapon being the pistol. You might want to test it again.

Oh probably gonna realise that you got disabled it only when there is no weapons in any slot except of the third, thanks.

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

Lee
Moderator Off Offline

Quote
For the 3rd example, you do not want to reseed the random number generator every time you call it, especially if you are going to call it more than once within a single second. Can you figure out why?

Hint: Why does the following code seem to not work?
1
2
3
4
for i=1,100 do
	math.randomseed(os.time())
	print(math.random(100))
end

Variable number of arguments

1
2
3
4
5
6
7
function whoa(func, ...)
	local args = {...}
	for i,v in ipairs(args) do
		args[i] = func(v)
	end
	return args
end

Replacing an old function, but still using part of its old functionalities

1
2
3
4
5
local old_print = print
function print(text)
	old_print("Debug: In print")
	old_print(text)
end

Convert hexadecimal in a string to a number

1
tonumber("7f", 16) -- returns 127

Semi-regular expressions in searches

1
2
3
str = "12345555555555555567890"
str:find("5+") -- outputs 5,18, the first 5 starts at the 5th character, and ends at the 18th
str:sub(str:find("5+")) -- you can use this to actually see what string.find finds.

The : (colon) operator.

If you have a table T, then T:field() is equvalent to T.field(T)

Function calls with constants

You don't need parenthesis with function calls with a single argument that is either a constant table or string

1
2
print "Hello World"
table.sort {10,9,8,7,6,5,4,3,2,1}

The global table

1
2
3
4
5
6
for global_name in pairs(_G) do
	print(global_name)
end

_G["hello"] = "goodbye"
print(hello)

Functions that can "remember"

This is very useful for things like timers.

1
2
3
4
5
6
7
8
function square(x)
	return function()
		return x*x    
	end
end

f = square(10)
print(f())

This can be extremely handy if you know how to use it correctly

"Persistent" functions

Coroutines can preserve states throught function calls.

1
2
3
4
5
6
7
8
9
10
11
counter = coroutine.wrap(function()
	local x = 1
	while true do
		coroutine.yield(x)
		x = x + 1
	end
end)

print(counter()) -- 1
print(counter()) -- 2
--etc

Adding libraries written in C (such as network) to CS2D

Say you put the dlls into the lib folder

1
package.cpath = package.cpath.."./lib/?.dll;../lib/?.dll;../../lib/?.dll;../../../lib/?.dll"

Copying a list

1
the_copy = {unpack(list)}

Printing out a list of strings

1
print(unpack(list))

and most importantly

Metatables

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

mafia_man
User Off Offline

Quote
Executing lua files like functions
(Try to modify myprint.lua when server is running)
File: sys/lua/server.lua
1
2
3
4
5
addhook("ms100", "everyMs100")
function everyMs100()
	local r = assert(loadfile("sys/lua/myprint.lua"))("this is", "sparta")
	msg("©128128128Returned: " .. r)
end
File: sys/lua/myprint.lua
1
2
3
4
local text, text2 = ...

msg("©255000000" .. text .."©255255255|©000000255" .. text2)
return math.random(0, 100)

Second example
File: sys/lua/server.lua
1
2
3
4
addhook("ms100", "everyMs100")
function everyMs100()
	local r = assert(loadfile("sys/lua/autoprint.lua"))("©255000000This", "©000255000Is", "©000000255Sparta!");
end
File: sys/lua/autoprint.lua
1
2
3
4
5
6
local strs = {...};
local str = ""
for _, i in pairs(strs) do
	str = str .. tostring(i) .. " "
end
msg(str)

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

EngiN33R
Moderator Off Offline

Quote
This went from CS2D scripting tricks to general Lua tips.

To elaborate on one example by user Lee, namely variable number of arguments:

1
2
3
4
5
6
function whoa(func, ...)
     for i,v in ipairs(arg) do -- arguments passed inside ... are already stored in a table called 'arg' already
          arg[i] = func(v)
     end
     return arg
end

Here's a short example of variable amount of arguments. An opposite to Lua's unpack() function:
1
2
3
function pack(...)
	return arg
end
To the start Previous 1 2 3 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview