Forum

> > CS2D > Scripts > Hitzone not work in some map.
Forums overviewCS2D overview Scripts overviewLog in to reply

English Hitzone not work in some map.

11 replies
To the start Previous 1 Next To the start

old Hitzone not work in some map.

-DIE Wolf-
User Off Offline

Quote
Ok ! This is simple image and hit zone scripts :

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
function initArray(m,v)
	local array = {}
	for i = 1, m do
		array[i]=v
	end
	return array
end

monster=initArray(32,0)


addhook("startround","start")
function start()
	i=1
	monster[i]=image("gfx/cs2d.bmp",320,320,0)--------Image at 320 320
	imagehitzone(monster[i],104,-92/2,-92/2,92,92)
end
start()

addhook("second","second")
function second()
	tween_rotate(monster[i],1000,math.random(-50,50))
end

addhook("hitzone","hz")
function hz(i,player,obj,wpn,x,y)
	freeimage(monster[i])
end

If u run this scripts in official map ( Or any map with no Env_Building) Image will disappear if u shot it.
But if u run this scripts in any maps has turrets(Env_building), It will not.

So weird.
Why ?

old Re: Hitzone not work in some map.

ohaz
User Off Offline

Quote
Quote
addhook("startround","start")
function start()
i=1
monster[i]=image("gfx/cs2d.bmp",320,320,0)--------Image at 320 320
imagehitzone(monster[i],104,-92/2,-92/2,92,92)
end
start()

First of all: You only give player 1 the image?
Second: Why do you call "start()" yourself?
Third: You read the documentation, but you didn't think about it anyways.
Quote
function hz(i,player,obj,wpn,x,y)
freeimage(monster[i])
end
. You used the first parameter (i), which is the image id, not the player ID. Use freeimage(monster[player]) instead

old Re: Hitzone not work in some map.

-DIE Wolf-
User Off Offline

Quote
user ohaz:
The image spawn at 320 320 and not follow anyone.
I call start() because for some reason, hook startround doesnt work.

The image not follow any player. So use freeimage(monster[i]) is right.

old Re: Hitzone not work in some map.

EngiN33R
Moderator Off Offline

Quote
user -DIE Wolf- has written
user Masea Yw. Also, problem solved if i change freeimage(monster[i]) to freeimage(monster[i-5]). And i have no idea why.
Can anyone explain it ?


This is because the i argument that the hitzone hook provides is the image ID. If there are env_building entities on the map, then there also exist dynamic objects which have image IDs assigned to them. It works with -5 because there are 5 dynamic objects on the map to which image IDs from 1 to 5 are assigned to.

1
2
3
4
5
6
addhook("startround","start")
function start()
	monster[1]=image("gfx/cs2d.bmp",320,320,0) --Image at 320 320
	imagehitzone(monster[1],104,-92/2,-92/2,92,92)
end
start()

First of all, there's no need to set a global i to 1 to assign something to monster[1]. I won't question why you hardcode one entry, I suppose you'll create a spawning algorithm later. Also, the startround hook doesn't work for you on a listen server because you have to restart a round for it to actually start, and for the hook to be called. You could leave the function call in, or you could just execute restart in the console.

1
2
3
4
5
6
7
8
addhook("hitzone","hz")
function hz(img,player,obj,wpn,x,y)
	for i = 1, #monster do -- check all monster entries
		if (img == monster[i]) then
			freeimage(img)
		end
	end
end

The keys in monster don't actually depend on anything, not player, not object, nothing - they're separate, which is why you simply have to compare the image that a person hit with all monster images. If a match is found then you hit one of the monster images and you can freely remove it.

A couple more additions:

1
2
3
4
5
6
addhook("second","second")
function second()
	for i = 1, #monster do -- Again, don't hardcode values
		tween_rotate(monster[i],1000,math.random(-50,50))
	end
end

And you don't have to use initArray() for your monster table. You could simply replace lines 1 to 9 in your original code with this:

1
monster = {}

Complete code >

old Re: Hitzone not work in some map.

EngiN33R
Moderator Off Offline

Quote
Well, you could use a loop and spawn them at random positions on the map. Here's a simple spawning algorithm:

1
2
3
4
5
6
7
8
9
10
11
12
function spawnmonster(img)
	local x, y = math.random(0, map("xsize")), math.random(0, map("ysize"))
	if (tile(x, y, "walkable")) then
		monster[#monster+1] = image(img, x*32+16, y*32+16, 0)
	else
		spawnmonster(img)
	end
end

for i = 1, 3 do -- As many as you want really
	spawnmonster("gfx/cs2d.bmp")
end
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview