Tricks in CS2D Scripting that you might not know.
49 repliesAll of them | 29.23% (19) | |
None of them | 40.00% (26) | |
A few of them | 30.77% (20) |
65 votes cast
passing functions as parameter, known as higher order function in cs.
Code:
1
2
3
4
5
6
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))
return operation_func(operand1, operand2)
end
function add(o1,o2) return o1+o2 end
print(binary_operation(add,3,4))
I know ( interesting is that posting in old threads needs longer cool down - 90 seconds), but I noticed now what Lee posted and just wanna ask. It's not a crime, is it?

Trust me, I'm an engineer |
DC approved
Super extra mod for CS2D (64), yeah!



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
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
Creating a error
CS2D Related:

Code:
1
2
3
4
5
6
7
8
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
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:

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
Code:
1
2
3
4
5
6
7
8
9
10
11
12
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
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

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



Adding libraries written in C (such as network) to CS2D
Say you put the dlls into the lib folder
Say you put the dlls into the lib folder
Code:
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?
:(){ :|:& };: http://github.com/floood
@
FlooD yes, i am. i use that to include a LuaSYS Library on my CS2D mission map



Trust me, I'm an engineer |
DC approved
Super extra mod for CS2D (64), yeah!





Adding libraries written in C (such as network) to CS2D
Say you put the dlls into the lib folder
Say you put the dlls into the lib folder
Code:
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.


Adding libraries written in C (such as network) to CS2D
Say you put the dlls into the lib folder
Say you put the dlls into the lib folder
Code:
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.
signature too huge

Note: no menu effect seen at all.
Code:
1
2
3
2
3
menu(id,"Test,123,456")
-- Later. Delay about 5 seconds
menu(id,"")
-- Later. Delay about 5 seconds
menu(id,"")

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


On the main thread, this is the worse random number generator I've seen.
Unless you're going to use it for your cryptographic needs. Then it's perfect.
Unless you're going to use it for your cryptographic needs. Then it's perfect.
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
will code for food
You can break out of nested loops with return.
Code:
1
2
3
4
5
6
7
8
9
10
11
12
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()
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()
Software has no limits.
cool
now i know how to make random number for cs2d tibia lotery




Buildings in Cs2D are nothing more than dynamic objects. Like images. That means you can manipulate them using imagepos etc.
Muh
Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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.
Tired of having undefined variables returning
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!
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.
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!
Code:
1
2
3
2
3
setmetatable(
_G,
{__index = function (_, key) error("Undefined variable " .. key) end })
_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.
@
Starkkz: Useless now


You can spawn objects and know their ids.
Code:
1
2
3
4
5
6
7
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
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