English Tricks in CS2D Scripting that you might not know.

49 replies
Goto 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
06.03.13 02:21:13 pm
Up
gotya2
GAME BANNED
Offline Off
passing functions as parameter, known as higher order function in cs.

Code:
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))
06.03.13 03:53:26 pm
Up
Avo
User
Offline Off
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 | user DC approved file cs2d Super extra mod for CS2D (64), yeah!
29.08.13 12:31:11 am
Up
MikuAuahDark
User
Online On
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
Code:
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
Code:
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
Code:
1
2
error("Your string here")
-- LUA ERROR: filename.lua:1: Your string here
file cs2d LuaJIT for Dedicated Server (13) JIT POWER! | Know your Lua errors! | Part of LÖVE development team since 11.3
29.08.13 02:01:22 am
Up
FlooD
GAME BANNED
Offline Off
user Lee has written:
Adding libraries written in C (such as network) to CS2D

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
29.08.13 07:24:46 am
Up
MikuAuahDark
User
Online On
@user FlooD yes, i am. i use that to include a LuaSYS Library on my CS2D mission map
file cs2d LuaJIT for Dedicated Server (13) JIT POWER! | Know your Lua errors! | Part of LÖVE development team since 11.3
29.08.13 10:29:56 am
Up
Avo
User
Offline Off
Some boolean "tricks".

More >
edited 5×, last 20.11.13 08:05:15 pm
Trust me, I'm an engineer | user DC approved file cs2d Super extra mod for CS2D (64), yeah!
29.08.13 10:34:35 am
Up
Pickabu
BANNED
Offline Off
I would liked that thread guys, but I don't know how to do lua skripts .-.
29.08.13 05:21:34 pm
Up
gotya2
GAME BANNED
Offline Off
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

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.
30.08.13 09:26:42 pm
Up
Tobey
User
Offline Off
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

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
30.08.13 11:47:29 pm
Up
MikuAuahDark
User
Online On
> Deleting all selection of menu but make the menu remains.
Note: no menu effect seen at all.
Code:
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
Code:
1
print(player(id,"weapon")==player(id,"weapontype") and "Same" or "Not Same") -- Same
file cs2d LuaJIT for Dedicated Server (13) JIT POWER! | Know your Lua errors! | Part of LÖVE development team since 11.3
09.09.13 02:52:42 pm
Up
sixpack
User
Offline Off
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.
09.09.13 04:22:50 pm
Up
omg
User
Offline Off
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
22.09.13 06:15:43 pm
Up
Livia
User
Offline Off
You can break out of nested loops with return.

Code:
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()
Software has no limits.
29.09.13 02:04:57 pm
Up
Kristian Lim
User
Offline Off
cool now i know how to make random number for cs2d tibia lotery
i dont know what is it but.. im happy
29.09.13 02:09:43 pm
Up
TimeQuesT
User
Offline Off
Buildings in Cs2D are nothing more than dynamic objects. Like images. That means you can manipulate them using imagepos etc.
Muh
21.06.15 02:18:51 am
Up
Starkkz
Moderator
Offline Off
Code:
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.
lol
22.06.15 07:48:39 pm
Up
Lee
Moderator
Offline Off
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!

Code:
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.
22.06.15 10:11:28 pm
Up
Starkkz
Moderator
Offline Off
You can spawn objects and know their ids.
Code:
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
lol
To the start Previous 1 2 3 Next To the start