Forum

> > CS2D > Scripts > Need Example of "Kill" hook.
Forums overviewCS2D overview Scripts overviewLog in to reply

English Need Example of "Kill" hook.

9 replies
To the start Previous 1 Next To the start

old Need Example of "Kill" hook.

Rainoth
Moderator Off Offline

Quote
Couldn't find what I was looking for so I need one little thing. Can anyone made a hook example of "on kill" hook ?
The main idea I have in my mind is to give player random amount of money for killing enemy which ranges from 600 to 800 (default 300 included). Hope you help me, thanks.

old Re: Need Example of "Kill" hook.

VADemon
User Off Offline

Quote
1
2
3
4
5
6
7
8
addhook("kill", "money_kill")

function money_kill(id, victim, weapon, x, y)
	msg(player(id,"name").. " killed " ..player(victim, "name").." with weapon ID "..weapon)
	msg(player(victim, "name") .. " was on coordinates: X:"..x.." Y:"..y)

	parse("setmoney "..id.." "..player(id, "money")+math.random(1,600))
end
Fixed code!
edited 1×, last 23.02.13 11:13:28 pm

old Re: Need Example of "Kill" hook.

Rainoth
Moderator Off Offline

Quote
If I were to change "..player(killer,"money") to "..player(victimc "money") at line 7. Would it work and if it did on killing the VICTIM would get money. Correct ?

EDIT : It appears it's not working...
The message things work correctly but I do not get any bonus gold at all..

old Re: Need Example of "Kill" hook.

Rainoth
Moderator Off Offline

Quote
I see no need for me to make tons of threads for things I don't understand so I might as well ask this question here. It's about building. I want to make similar building system like in SuperHero script but is there a faster way (or shorter) to make check if entity exists or not.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
---------------------------
--- BUILDINGS MENU 1st ----
---------------------------
addhook("menu","Buildings")
function Buildings(id,title,button)
sx = player(id,"x")
sy = player(id,"y")
st = player(id,"team")
	if title=="Make Buildings" then
	  if button==1 then
	    if (player(id,"health")>0 and player(id,"team")>0) then 
		if not entity((sx/32)+1,(sy/32)+1,"exists") then
               		parse("spawnobject 1 "..((sx/32)-1).." "..((sy/32)+1).." 0 0 "..st.." "..id) -- Left Corner Top
			parse("spawnobject 1 "..((sx/32)-1).." "..((sy/32)-1).." 0 0 "..st.." "..id) -- Left Corner Bot
			parse("spawnobject 1 "..((sx/32)+1).." "..((sy/32)+1).." 0 0 "..st.." "..id) -- Right Corner Top
			parse("spawnobject 1 "..((sx/32)+1).." "..((sy/32)-1).." 0 0 "..st.." "..id) -- Right Corner Bot
			parse("spawnobject 6 "..((sx/32)-1).." "..((sy/32)).." 0 0 "..st.." "..id) -- Player Position X - 1
			parse("spawnobject 6 "..((sx/32)).." "..((sy/32)-1).." 0 0 "..st.." "..id) -- Player Position Y - 1
			parse("spawnobject 6 "..((sx/32)+1).." "..((sy/32)).." 0 0 "..st.." "..id) -- Player Position X + 1
			parse("spawnobject 6 "..((sx/32)).." "..((sy/32)+1).." 0 0 "..st.." "..id) -- Player Position Y + 1
			parse("spawnobject 6 "..((sx/32)).." "..((sy/32)+).." 0 0 "..st.." "..id) -- Where Player is Standing
          	end
	     end
     	  end

Don't mind the missing IFs because it's just a part of whole menu (only button one particulary) I'd really wish to be able to have something to help me shorten this. At MOST it would be a total of 49 entities (49 buildings) so I'd have to write this so many times
1
if not entity((sx/32)+1,(sy/32)+1,"exists")
this one just checks if there is entity at Top Right corner, is there any way to shorten this ? (So I wouldn't have to Check X/Y as +3/+3 ; +3/+2 ; +3/+1 ; +3/0 ; +3/-1 (then -2 and -3 and that's only right side one line )

old Re: Need Example of "Kill" hook.

VADemon
User Off Offline

Quote
1
if not entity((sx/32)+1,(sy/32)+1,"exists") then
You only check the entity on ONE tile but we have multiple buildings.

cs2d lua cmd player has also "tilex" and "tiley"

I would make a table with predefined coordinates and then check everyone of them and place the building if there's no entity.

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
coordinates = {
	x={},
	y={}
}

function add_coordinates(x,y)
	coordinates.x[ (#coordinates.x+1) ] = x --add to the table as the last entry
	coordinates.y[ (#coordinates.y+1) ] = y
end

add_coordinates(-1,-1)
add_coordinates(1,1)
add_coordinates(1,-1)
add_coordinates(-1,0)
add_coordinates(0,-1)
add_coordinates(1,0)
add_coordinates(0,1)
add_coordinates(0,0)

--... many, many lines of code later
local sTileX = player(id,"tilex")
local sTileY = player(id,"tiley")
for i=1,3 do
	if not entity(sTileX + coordinates.x[i], sTileY + coordinates.y[i], "exists") then
		parse("spawnobject 1 "..sTileX + coordinates.x[i].." "..sTileY + coordinates.y[i].." 0 0 "..st.." "..id)
	end
end

for i=4,9 do --because 1-3 are occupied
	if not entity(sTileX + coordinates.x[i], sTileY + coordinates.y[i], "exists") then
		parse("spawnobject 6 "..sTileX + coordinates.x[i].." "..sTileY + coordinates.y[i].." 0 0 "..st.." "..id)
	end
end
^ this shit is pretty hardcoded, else you would need an another table for every object type (so you don't have to mess up with for loops and storing everything in a single table)

3)
1
2
3
local sx = player(id,"x")
local sy = player(id,"y")
local st = player(id,"team")

ALWAYS, make variables you will only use in the current function as local ones. It's faster, it doesn't create trash.
edited 1×, last 24.02.13 12:23:37 am

old Re: Need Example of "Kill" hook.

Rainoth
Moderator Off Offline

Quote
That code is kinda hard for me to understand. Questions :
• If from 49 tiles (provided the building I want is 7x7) at least ONE is an entity - would it Make buildings appear everywhere beside entity tile or it wouldn't appear at all ?
• At line 23 I think there is a loop. So what does for 1,3 do ? Make 3 buildings in different tiles ?
• For how many types of buildings does this work ? Because I'd like to use Wire,GateField,Lvl 1/2/3 turret,dispenser,Lvl 1/2/3 Wall,Barricade...

Honestly. I read the whole code you pasted and understood somewhat 1-4 and all others after 21... What is that "[ (#coordinates.x+1) ] = x" ??? And what it does..?

old Re: Need Example of "Kill" hook.

VADemon
User Off Offline

Quote
I knew I'd be a crappy code

1) No, every building will appear as long as you're not trying to build it on an entity or wall
2) Yep. Forgot the two "end"
3) You have to hardcode it. It means, you add 3 different positions with add_coordinates() as first, then you will need a for i=1,3 to get the first three coordinates. If you add more, you need an another for loop to add them

1
2
3
4
-- added 11 coordinates to table
for i=1,3 do
	print(coordinates.x[i] .." "..coordinates.y[i]) --print the first 3 added positions
end
And so on. You decide what will be done with the added coordinates. In my last reply all you have to do is to execute lines 21-33.

More >


4) Nothing's bad about. It just adds the entries to table in a different way. Just skip this part.

old Re: Need Example of "Kill" hook.

Rainoth
Moderator Off Offline

Quote
user VADemon has written
In my last reply all you have to do is to execute lines 21-33.


I just have to paste those lines in my code to check if there's an entity at one tile away from me ? Still very hard for me to understand
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview