Forum

> > CS2D > Scripts > Id triggering
Forums overviewCS2D overview Scripts overviewLog in to reply

English Id triggering

16 replies
To the start Previous 1 Next To the start

old Id triggering

Masea
Super User Off Offline

Quote
Hello bros, again me with my another trouble that I do not know.
Probably everyone gets problem with deal with ids as like me.
I won't write here a problem, I just need to know few things.

1
2
for i = 1, 32 do
	if player(i,"exists") then

1
for _,i in pairs(player(0,"table")) do

1
for _,i in ipairs(player(0,"table")) do

These three things which I do not know where should I use them and what is the difference among those? Would be great if I get a knowledge about all.

Thanks.

old Re: Id triggering

THEMUD
User Off Offline

Quote
Those are used to have all the IDs for the players on the server in case if a hook/function doesn't have the ID parameter.
Like if you are using the always hook, you will not have any ID for the players unless you did that way.

There are a few more tables for the same purpose like:
"tableliving", to give all the IDs for the living players restored in a table.
"table1living", to give all the IDs for the living players in the Terrorists.
"table2living", the same intention but for the Counter-Terrorists.
And so on.

old Re: Id triggering

Masea
Super User Off Offline

Quote
@user THEMUD: Actually, I know all of them which you said. Strictly, I want to know what is the difference between them?

old Re: Id triggering

Hajt
User Off Offline

Quote
From what I know pairs is faster than ipairs but you will note difference only in a large table.

old Re: Id triggering

Masea
Super User Off Offline

Quote
@user Hajt: Thanks. How can I understand that where should I use which one? Pairs is faster, I know right but what does it mean?

old Re: Id triggering

THEMUD
User Off Offline

Quote
Oops, sorry for my misunderstanding.
Well, pretty much what @user Hajt said and ipairs loops through the tables in an orderly way. And pairs is the otherwise.

Take this example for instance:
1
2
3
4
5
6
table = {
	[1] = "Hey";
	[3] = "Hello";
	[4] = "Bye";
	[5] = "Welcome";
};

Now with pairs, it will loop through them flawlessly. Unlike ipairs, it will not go to 3 because 2 doesn't exist.

old Re: Id triggering

Bowlinghead
User Off Offline

Quote
These are loops also like while.
1
2
3
for i = 1, 32 do
if player(i,"exists") then
end
Executes the code inside 32 times where i is always counting 1 up. First run: i=1, Second run: i=2, ...
Dont do this to get all player ids as you always run it 32 times also when there are just 5 players.
1
2
for _,i in ipairs(player(0,"table")) do
end
Ipairs cycles through an array but only with numeric indexes. See below
1
2
for _,i in pairs(player(0,"table")) do
end
pairs cycles through everything in an array.

In this case there is no difference between the execution (maybe speed) because CS2D´s arrays are always with a normal numeric index.

But if you have an array like this then it makes a difference:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
--untested
array = {}
array[1] = 5 -- numeric index
array[2] = 8
array["hi"] = 9 -- special index

for _, i in pairs(array) do
	print(i)
end
-- output: 5 8 9

for _, i in ipairs(array) do
	print(i)
end
-- output: 5 8
edited 1×, last 02.08.16 02:11:15 pm

old Re: Id triggering

Rainoth
Moderator Off Offline

Quote
I did have the same question long long ago.
After having looked into it, if you have a table USGNs (an example)
1
usgn = {1,420,69,12345}
then ipairs will execute very slightly faster
It's pretty much the only time when you should be using ipairs when doing simple scripts
pairs will be used in all other cases.
Lua tables are great in that they have map part and array part. user Bowlinghead showed how ipairs will only cycle through array part while pairs will cycle through both
I wouldn't ever use
1
for i = 1, 32 do
if you want to do whatever with players since you'll be running loop needlessly for 'i's that dont exist. It's much easier to just loop player table.
Oh and don't listen to user THEMUD because there's no such tables as "table1living" and "table2living". He mixed them up with "team1living" and "team2living".

old Re: Id triggering

_Yank
User Off Offline

Quote
The normal loop method (for = 1, 32, etc) is faster than both pairs and ipairs (Well, it may not be in this case as it will need to run
player(id, "exists")
for each id). However it isn't as elegant and sophisticated as the pairs/ipairs method, in my opinion, which has been created for this purpose.

And there is some minimal differences between pairs and ipairs methods, while ipairs only iterates through number indexes, not ordered, the pairs method does an ordered iteration and also iterates through indexes that are not numbers.

Example >

old Re: Id triggering

Waldin
User Off Offline

Quote
Omg guys the ordened one is
ipairs()
, witch iterates at array! just by numbers (1,2,3,4 and not 1,3,4,"space" it will stop when no other number)
pairs is the random one
Just as user THEMUD said

@user _Yank: Loops uses
do
and not
then

old Re: Id triggering

Masea
Super User Off Offline

Quote
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
for _,i in pairs(player(0,"team2living")) do
if player(id,"tilex") == player(i,"tilex") and player(id,"tiley") == player(i,"tiley") and hp[i] == -1 and catch == 0 and catched[i] == 0 and catchid == 0 and hooked[i] == 0 then
				catch = 1
				catchid = i
				catched[catchid] = 1
				if yete3[id] == 1 then
					speed[id] = 5
					parse("speedmod "..id.." "..speed[id])
				end
			elseif catch == 1 then
				killerspeedsifir(id)
				if catchid > 0 then
					catched[catchid] = 0
					catchid = 0
				end
				catch = 0
			end
end

Terrorist team can take Counter-Terrorist players if they hitted for two times. Everything is ok so far but after that, I can only take person who has the most ID number in the Counter-Terrorist team.

Let me tell it with an example;

In the Counter-Terrorist team, there is four player and they've 1, 2, 4 and 7 number IDs. If a player who in Terrorist team, hit to no. 1 enemy for two times, he can't take him. Even he hitted for two times. But if Terrorist player hit to who has the most ID number which is 7 for now, he can take him.

And here's the question; why can't I take all of them?

This is why I opened this thread, if there is any problem(s) about that.

old Re: Id triggering

Rainoth
Moderator Off Offline

Quote
You're very unclear about your problem. Even with the provided example.

I'll try to guess from what I understood so I ask that you confirm if that indeed is your problem.

Goal: When Terrorists hit Counter-Terrorists the latter should be able to change their team to Terrorists.
The problem is that this works only for the Counter-Terrorist with highest ID and not every Counter-Terrorist.

Is that correct?

old Re: Id triggering

Masea
Super User Off Offline

Quote
@user Rainoth: Yes, it is. I would like to make it for every Counter-Terrorist player. Thanks for helping me!

old Re: Id triggering

Rainoth
Moderator Off Offline

Quote
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
change = {}

addhook("spawn","_s")
function _s(id)
	change[id] = {}
	change[id].hits = 0
	change[id].ability = false
end

addhook("team","_t")
function _t(id,team)
	if player(id,"team") == 2 and change[id].ability == false and team == 1 then
		return 1
	end
end

addhook("hit","_h")
function _h(id,source)
	if player(source,"team") == 1 then
		change[id].hits = change[id].hits + 1
		if change[id].hits == 2 then
			change[id].ability = true
		end
	end
end

addhook("startround","_sr")
function _sr()
	for _,v in pairs (player(0,"team2living")) do
		change[v].ability = false
		change[v].hits = 0
	end
end

Not enough information for all exceptions but it works as I described. The only question mark is on resetting this. Just in case I made it like so - if you can change from CT to T then you can freely hop between teams. When a new round starts, all CTs lose this ability (until they are hit twice again).

old Re: Id triggering

Masea
Super User Off Offline

Quote
@user Rainoth: Ahh, you didn't understand me. It's about my english, sorry. The real question was why I can take(Don't care the what is "take", it is only an example) only person who has the highest ID in Counter-Terrorist team. Look the code which I gave you. I did use this at there;
1
for _,i in pairs(player(0,"team2living")) do
I know, it takes all IDs of the Counter-Terrorist team players but it only works for who has the highest ID.

Again, thank you!

old Re: Id triggering

Rainoth
Moderator Off Offline

Quote
I cannot tell you properly because I do not know how certain variables work in the entirety of your script.
There's very likely one way that happens though: when you execute code or re-write your variables at the end of the loop.

So say if you had
1
2
3
4
for i = 1,5 do
	potato = i
end
print(potato)
So it will print out '5' because it's the last result you get from loop (just like the highest ID on the table I'm guessing).

// Actually the other way around would probably work too. Start the loop and make it useless after the first step.
This is what I got when I tabbed your code (it's one of the most important steps tbh when you show code to others - readability)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
for _,i in pairs(player(0,"team2living")) do
	if player(id,"tilex") == player(i,"tilex") and player(id,"tiley") == player(i,"tiley") and hp[i] == -1 and catch == 0 and catched[i] == 0 and catchid == 0 and hooked[i] == 0 then
		catch = 1
		catchid = i
		catched[catchid] = 1
		if yete3[id] == 1 then
			speed[id] = 5
			parse("speedmod "..id.." "..speed[id])
		end
	elseif catch == 1 then
		killerspeedsifir(id)
		if catchid > 0 then
			catched[catchid] = 0
			catchid = 0
		end
		catch = 0
	end
end
If your ct table is {1, 3, 15} and you're checking if 'catch' is equal to 1 (I'd advise true/false for binary IFs, you can just write 'if catch' and 'if not catch' instead of 'if catch == 1', it's more understandable for me at least)
then you assign some variables (where catchid is completely useless cause you could do catched[i] and remove the catchid) and you reset those variables if the ct doesn't meet all of those conditions but at least meets the single condition 'catch == 1'
if the IFs aren't done properly, it MAY be possible that you don't reset what you need which screws you over and you're left with the '15' in the ct table.

To be honest, the code is a complete mess. Of course that's mostly because I have no idea what you're trying to accomplish but I can guess from the commands.
If I were you, I wouldn't use global variables to determine something that applies to multiple people. You could use them fairly easily if you want just one person to work (technically you could script a monster that could make one global variable handle all players but that's what tables are for xd)

TL;DR hard to say what exactly is wrong when you provide 10% of your whole script or at the very least provide no context what you expect it to do.

If you're reluctant to post code, write what your script should do. We'll see what we could do.
edited 2×, last 04.08.16 01:53:44 am
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview