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.

gotya2
GAME BANNED Off Offline

Quote
passing functions as parameter, known as higher order function in cs.

1
2
3
4
5
6
function binary_operation(operation_func, operand1, operand2)
        return operation_func(operand1, operand2)
end
function add(o1,o2) return o1+o2 end

print(binary_operation(add,3,4))

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

MikuAuahDark
User Off Offline

Quote
Im sorry that im reviving old thread, but here is a some LUA tips & tricks from me

CS2D Related:
> Spawn player without any weapons at all
1
2
3
4
5
6
7
8
condition=true
addhook("die","Miku_Hatsune")
function Miku_Hatsune(id)
	if conditional then
		local x,y=randomentity(player(id,"team")-1)
		parse("spawnplayer "..id.." "..(x*32+16).." "..(y*32+16))
	end
end

And some general LUA tricks:
> Getting LUA filename that call the function
Info: Because debug library still exist in CS2D, this LUA also can be used on Counter-Strike 2D. It just uses a debug.traceback() function
1
2
3
4
5
6
7
8
9
10
11
12
function GetCurrentLUAFilename()
	local traceback_string=debug.traceback():gsub("\t",""):sub(18)
	local index=0
	for line in traceback_string:gmatch("[^\n]+") do
		index=index+1
		if index==2 then
			local t=line:find(":")
			return line:sub(1,t-1)
		end
	end
end
print(GetCurrentLUAFilename()) -- Output "stdin" when you use it at interpreter. If it used on another file, then it output the LUA filename

> Creating a error
1
2
error("Your string here")
-- LUA ERROR: filename.lua:1: Your string here

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

FlooD
GAME BANNED Off Offline

Quote
user Lee has written
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"


has anyone ever used this (or a similar) technique to make something useful in cs2d?

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

Avo
User Off Offline

Quote
Some boolean "tricks".

More >
edited 5×, last 20.11.13 08:05:15 pm

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

gotya2
GAME BANNED Off Offline

Quote
user FlooD has written
user Lee has written
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"


has anyone ever used this (or a similar) technique to make something useful in cs2d?


Inter server communication with sockets and
a high precision time function in linux.

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

Tobey
User Off Offline

Quote
user FlooD has written
user Lee has written
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"


has anyone ever used this (or a similar) technique to make something useful in cs2d?


uPrate6 did, played around with it too.

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

MikuAuahDark
User Off Offline

Quote
> Deleting all selection of menu but make the menu remains.
Note: no menu effect seen at all.
1
2
3
menu(id,"Test,123,456")
-- Later. Delay about 5 seconds
menu(id,"")

> player(id,"weapon") and player(id,"weapontype") is same
Source: Lua Lag Compensation
1
print(player(id,"weapon")==player(id,"weapontype") and "Same" or "Not Same") -- Same

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

omg
User Off Offline

Quote
lol yeah, now that i know more about random numbers...that "truly" random number generator is absolute ass wipe. at least in c++, if you try to get reseeded numbers every time, the numbers will actually just be incrementing slowly instead of being random. this is worse than just seeding it once. you actually have to code this and run it to see what im talking about

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

Livia
User Off Offline

Quote
You can break out of nested loops with return.

1
2
3
4
5
6
7
8
9
10
11
12
prepare()
do
    for k,v in pairs(var1) do
        for k2,v2 in pairs(var2) do
            if v == v2 then
                match()
                return
            end
        end
     no_match()
end
continue()

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

Starkkz
Moderator Off Offline

Quote
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
local BinaryFormat = package.cpath:match("%p[\\|/]?%p(%a+)")
if BinaryFormat == "dll" then
	function os.name()
		return "Windows"
	end
elseif BinaryFormat == "so" then
	function os.name()
		return "Linux"
	end
elseif BinaryFormat == "dylib" then
	function os.name()
		return "MacOS"
	end
end
BinaryFormat = nil
Since everyone was using command line functions to know the operating system, I though it'd be better to post this easier way.

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

Lee
Moderator Off Offline

Quote
Tired of having undefined variables returning
nil
s everywhere? Are you suffering from spats of unfotunate speeling mistekes? Then you'll understand how annoying it is to track down one small typo within your goliath of a codebase. Seriously, that's probably the solution to about 30% of all of the questions that are asked in this forum.

I'm not here to debate language decisions; but if I were, I would definitely rank this as one of the most egregious issue with Lua. However, we can fix this!

1
2
3
setmetatable(
  _G,
  {__index = function (_, key) error("Undefined variable " .. key) end })

This line tells Lua to route all undefined global indexing (aka variable resolution for global variables, which is the fallback routine in case local variable resolution fails) through our own function (__index), which just raises an error.

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

Starkkz
Moderator Off Offline

Quote
You can spawn objects and know their ids.
1
2
3
4
5
6
7
function spawnobject(type, x, y, rot, mode, team, player)
	if type and x and y then
		rot, mode, team, player = rot or 0, mode or 0, team or 0, player or 0
		parse("spawnobject "..type.." "..x.." "..y.." "..rot.." "..mode.." "..team.." "..player)
		return objectat(x, y, type)
	end
end
To the start Previous 1 2 3 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview