English Table class, message to team, array explanation

10 replies
Goto Page
To the start Previous 1 Next To the start
Up
Ace Howl
User
Offline Off
Hi there.

How do I:

1. group weapon as weapon class (I need explanation)
Code:
1
2
3
4
5
6
7
8
9
10
weapon.primary = {10,11,20,..,24,30,..,49,90,91}
weapon.secondary = {1,..6}

function hitplayer(id,weapon,source)
     if source == weapon.primary then
          parse("flashplayer "..id.." 10")
     elseif source == weapon.secondary then
          parse("flashplayer "..id.." 5")
     end
end


2. proceed if in the list
Code:
1
2
3
4
5
usgn.list = {66153,109296,44801}

if usgn.list then
     proceed()
end


3. temporarily slowed upon hit
Code:
1
2
3
4
5
6
7
8
9
function hitplayer2(id,weapon)
     if weapon == weapon.primary then
          parse("speedmod "..id.." -10")
          timer(100,.. ? ..)
     elseif weapon == weapon.secondary then
          parse("speedmod "..id.." -5")
          timer (200,.. ? ..)
     end
end


4. message visible to a team only
Code:
1
msg(team1,"You must kill Counter-Terrorists.")


5. Array explanation (sorry, I forgot it although was told understood)
Code:
1
2
3
4
5
6
7
function initArray(m)     -- what is m?
     local array = {}
     for i = 1, m do      -- what is i? does it related to (m)?
          array[i]=0     -- ??
     end
     return array          -- what?
end


Note: Question 1 and 3 are same thing, but separated. This is to ease you for solution findings.

That's the only questions I want to ask, for now.
23.01.16 05:39:26 am
Up
Cure Pikachu
User
Offline Off
Q1 >

Q2 >

Q3 >

Q4 >

5. It creates a table with m entries, each with 0 as their values. So
Code:
1
2
test = initArray(5)
-- is the same as test = {0,0,0,0,0}

And if you search deep enough, you find this too:
user ohaz, thread cs2d Lua Scripts/Questions/Help has written:
it creates an array with 'm' zeros.
so that you can create a 0 for every player on the server!
if now a player joins he has 0, you can now assign him another number. (like the class he choses)
edited 1×, last 23.01.16 11:13:04 am
IMG:https://i.imgur.com/uUrye71.png
IMG:https://i.imgur.com/xpsyQRX.png
23.01.16 10:51:00 am
Up
Ortimh
User
Offline Off
For question 2, I suggest you to use ipairs since it iterates over numerical indexes orderly, which means faster, but the speed will not be noticed anyway. And a fix for user Cure Pikachu's answer.
Code:
1
2
3
4
5
6
7
local usgnlist = {66153, 109296, 44801}

for index, usgn in ipairs(usgnlist) do
     if (player(id,"usgn") == usgn) then
          -- Proceed here!
     end
end


For question 3, a better method of user Cure Pikachu's answer, by using local.
Code:
1
2
3
4
5
6
7
8
9
10
11
function hitplayer2(id, source, weapon)
     local speedmod = player(id, "speedmod") -- Get their original speed first
     
     if (itemtype(weapon, "slot") == 1) then
          parse("speedmod " .. id .. " -10")
          timer (100, "parse", "speedmod " .. id .. " " .. speedmod)
     elseif (itemtype(weapon, "slot") == 2) then
          parse("speedmod " .. id .. " -5")
          timer (200, "parse", "speedmod " .. id .. " " .. speedmod)
     end
end


For question 4, here is the code. It's not error-safe.
Code:
1
2
3
4
5
6
7
function msg3(team, message, living)
     local string = "team" .. team .. (living and "living" or "")
     
     for index, id in ipairs(player(0, string)) do
          msg2(id, message)
     end
end
I'm awesome ... and I really like cookies.
23.01.16 11:17:42 am
Up
Cure Pikachu
User
Offline Off
@user Ortimh: You'll never figure out your mistakes until someone points it out, thanks.
IMG:https://i.imgur.com/uUrye71.png
IMG:https://i.imgur.com/xpsyQRX.png
23.01.16 11:33:47 am
Up
Yates
Reviewer
Offline Off
Both or your speedmod on hit are bugged. Not by code but by functionality.
23.01.16 12:02:37 pm
Up
Bowlinghead
User
Offline Off
If you modify the speed of the player twice in a row then the script will reset the 2nd change on the 1st timer event.

I think this should fix it. You can stack the slow down effects with it.
Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
function hitplayer2(id, source, weapon)
     local speedmod
     
     if (itemtype(weapon, "slot") == 1) then
          parse("speedmod " .. id .. " -10")
          speedmod = player(id, "speedmod")
          timer (100, "parse", "speedmod " .. id .. " " ..speemod+10) -- "speedmod" ~= speedmod

     elseif (itemtype(weapon, "slot") == 2) then
          parse("speedmod " .. id .. " -5")
          speedmod = player(id, "speedmod")
          timer (200, "parse", "speedmod " .. id .. " " .. speedmod+5) -- "speedmod" ~= speedmod
     end
end
edited 1×, last 23.01.16 01:04:39 pm
Share time limited free games here
23.01.16 12:37:30 pm
Up
Ace Howl
User
Offline Off
Big cheers @user Cure Pikachu for solving most of the questions. That's nice @user Ortimh, improvising his codes.

About array, does that have the same function as this? If not, what differs them?
Code:
1
2
3
4
classtype = {}
for id = 1,32 do
     classtype[id] = 0
end


About Question 1 and 3, how for specific table content?
Code:
1
2
3
item.StealthAssassin = {85,84,52}
item.ISIS = {47,48,89,77}
item.Engineer = {10,4,74}




EDIT:
@user Yates: Why did you think it was bugged?
@user Bowlinghead: Great idea! But, I don't prefer stacked though. It makes the victim look like stuck from tight hole :ugly .
23.01.16 12:47:28 pm
Up
Cure Pikachu
User
Offline Off
@user Ace Howl: Yeah the initArray function and your code do the same thing.
For specific items, the same technique used to answer your Q2 applies here:
Code:
1
2
3
4
5
6
7
8
function hitplayer(id,source,weapon)
     for _, wpn in ipairs(tab) do -- where tab is the table name
          if weapon == wpn then
               parse("flashplayer "..id.." 10")
               break -- Here you can safely break out of the loop
          end
     end
end

Again, item.StealthAssassin is the same as item["StealthAssassin"], so you'll need to call item = {} first. But cs2d lua cmd item is already a CS2D function, so I wouldn't use that namespace if I were you.

The speedmod code is bugged because of what user Bowlinghead mentioned:
user Bowlinghead has written:
If you modify the speed of the player twice in a row then the script will reset the 2nd change on the timer event.

And the code he provided won't theoretically let you hit targets to a screeching halt, so don't worry about that.
edited 2×, last 23.01.16 01:02:24 pm
IMG:https://i.imgur.com/uUrye71.png
IMG:https://i.imgur.com/xpsyQRX.png
23.01.16 01:00:52 pm
Up
Ace Howl
User
Offline Off
Oh I see! The duration difference from victim result in diversed output.

Thanks user Cure Pikachu. I would be in darkness if I have not known all of those especially the array one. Shorter one is convenient compared to initArray stuff . break function, I didn't see that from most script.
23.01.16 01:19:21 pm
Up
Cure Pikachu
User
Offline Off
Actually the initArray function used in the samples isn't complete.
user Lee, thread cs2d Lua Scripts/Questions/Help has written:
This is a simplified version to create a table with 'm' number of elements in there, and each of them will have a 0. This was originally a sample I submitted to DC, a revised version is as follows:
Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
function initArray(i,x,d)
     local array = {}
     if type(i) == "table" then
          array = i
          i = x
          x = d
     end
     if (x == nil) then x = 0 end
     for m = 1 , i do
          if (array[m] == nil) then
               array[m] = x
          end
     end
     return array
end

Use as follows:
Code:
1
2
3
4
5
6
7
old_table = initArray(4,1)

-- old_table now = {1,1,1,1}

old_table = initArray(old_table,10)

-- old_table now = {1,1,1,1,0,0,0,0,0,0}
IMG:https://i.imgur.com/uUrye71.png
IMG:https://i.imgur.com/xpsyQRX.png
23.01.16 06:43:34 pm
Up
omg
User
Offline Off
the badly written "slow on hit" code is making me facepalm. every single version of it will result in unwanted behavior. you either have to assume everyone starts at speedmod 0 or only let hits slow u by -10. though in my opinion this shouldnt stack because at speedmod -20 u basically stop moving unless u have a knife out
will code for food
To the start Previous 1 Next To the start