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
Up
Infinite Rain
Reviewer
Offline Off
1st - Disabling right click on knife (attack 2 on knife)
Code:
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:
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:
Code:
1
2
3
function func(x)
     local y = x or 0
end


3rd - Making TRULY random number from math.random function
Code:
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
A thousand may fall at your side, ten thousand at your right hand, but it will not come near you. You will only look with your eyes and see the recompense of the wicked. - Psalm 91:7-8 ESV
25.05.12 12:10:14 pm
Up
Navy Seal
User
Offline Off
"2 / 1 Of them"

correct it to 1 / 2 Of them
25.05.12 12:18:03 pm
Up
Infinite Rain
Reviewer
Offline Off
thanks, fixed already
A thousand may fall at your side, ten thousand at your right hand, but it will not come near you. You will only look with your eyes and see the recompense of the wicked. - Psalm 91:7-8 ESV
25.05.12 01:12:05 pm
Up
KiRa_3
User
Offline Off
none of them
25.05.12 01:53:11 pm
Up
MikuAuahDark
User
Offline Off
waw nice triks factis. i don't know all of them. with this help, i can add a random damage to my point system
edited 1×, last 25.05.12 02:03:15 pm
Hello? Are you there, Tae Hanazono?
25.05.12 01:56:13 pm
Up
Infinite Rain
Reviewer
Offline Off
Np lol. I know more but I forgot 'em
A thousand may fall at your side, ten thousand at your right hand, but it will not come near you. You will only look with your eyes and see the recompense of the wicked. - Psalm 91:7-8 ESV
25.05.12 02:16:26 pm
Up
EngiN33R
Moderator
Offline Off
user Infinite Rain has written:
Code:
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:
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:
Code:
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.
I code, therefore I exist. | Visit my blog for Lua tips and other interesting info
25.05.12 03:10:38 pm
Up
Infinite Rain
Reviewer
Offline Off
user EngiN33R has written:
user Infinite Rain has written:
Code:
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:
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:
Code:
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
A thousand may fall at your side, ten thousand at your right hand, but it will not come near you. You will only look with your eyes and see the recompense of the wicked. - Psalm 91:7-8 ESV
25.05.12 04:02:39 pm
Up
EngiN33R
Moderator
Offline Off
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.
I code, therefore I exist. | Visit my blog for Lua tips and other interesting info
25.05.12 04:02:44 pm
Up
PyKemis
User
Offline Off
Well didn't know about disabling right click. I think the only lua concept that I think is cool is metatables.
25.05.12 04:02:47 pm
Up
tom282f3
User
Offline Off
Easier way to disable AltAttack of a knife:
Code:
1
parse("mp_wpndmg_z1 knife 0")

cs2dbbs.gameclub.tw - LEGACY
25.05.12 06:09:31 pm
Up
omg
User
Offline Off
user tom282f3 has written:
Easier way to disable AltAttack of a knife:
Code:
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
will code for food
25.05.12 06:24:09 pm
Up
Infinite Rain
Reviewer
Offline Off
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.
A thousand may fall at your side, ten thousand at your right hand, but it will not come near you. You will only look with your eyes and see the recompense of the wicked. - Psalm 91:7-8 ESV
25.05.12 07:35:12 pm
Up
3RROR
User
Offline Off
user tom282f3 has written:
Easier way to disable AltAttack of a knife:
Code:
1
parse("mp_wpndmg_z1 knife 0")


Using this way only makes knife damage to 0, but you still can use attack2(Right Click) of it.
IMG:http://gamerswins.com/images/ins/logo.png
25.05.12 08:23:45 pm
Up
Lee
Moderator
Offline Off
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?
Code:
1
2
3
4
for i=1,100 do
     math.randomseed(os.time())
     print(math.random(100))
end


Variable number of arguments

Code:
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

Code:
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

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


Semi-regular expressions in searches

Code:
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

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


The global table

Code:
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.

Code:
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.

Code:
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

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


Copying a list

Code:
1
the_copy = {unpack(list)}


Printing out a list of strings

Code:
1
print(unpack(list))


and most importantly

Metatables
05.03.13 05:25:50 pm
Up
Avo
User
Offline Off
I like what user Lee wrote, especially "global table". user Lee, do you know more? I would like to see practical use of "Functions that can 'remember' ", too.
05.03.13 06:15:55 pm
Up
mafia_man
User
Offline Off
Executing lua files like functions
(Try to modify myprint.lua when server is running)
File: sys/lua/server.lua
Code:
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
Code:
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
Code:
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
Code:
1
2
3
4
5
6
local strs = {...};
local str = ""
for _, i in pairs(strs) do
     str = str .. tostring(i) .. " "
end
msg(str)
05.03.13 10:15:21 pm
Up
EngiN33R
Moderator
Offline Off
This went from CS2D scripting tricks to general Lua tips.

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

Code:
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:
Code:
1
2
3
function pack(...)
     return arg
end
I code, therefore I exist. | Visit my blog for Lua tips and other interesting info
06.03.13 10:55:56 am
Up
Infinite Rain
Reviewer
Offline Off
You guys do realise that this thread is 8 months old?
A thousand may fall at your side, ten thousand at your right hand, but it will not come near you. You will only look with your eyes and see the recompense of the wicked. - Psalm 91:7-8 ESV
06.03.13 11:22:12 am
Up
mafia_man
User
Offline Off
user Infinite Rain has written:
You guys do realise that this thread is 8 months old?

Ask user Avo
To the start Previous 1 2 3 Next To the start