Forum

> > CS2D > Scripts > Explosion position
Forums overviewCS2D overview Scripts overviewLog in to reply

English Explosion position

8 replies
To the start Previous 1 Next To the start

old Explosion position

En-Kay
User Off Offline

Quote
Ok so I want to create a function when a class types "!spell" in chat, he creates an explosion. Question: How do I create and explosion few pixels from the player's position?

The code I tried but it creates explosion AT the player's location:

1
2
3
4
5
6
7
8
9
10
11
12
13
addhook ("say","rainoffire")
function rainoffire (id,text)

	if (sample.classes.class[id]==8) then
		if (text=="!spell") then
			if (player(id,"money")==16000) then
				parse ("explosion "..player(id,"x").."+96 "..player(id,"y").."+96 100 100 "..id.."")
			else
				msg2 (id, "You do not have enough mana!")
			end
		end
	end
end



edit:

I got it. Just thought of something and it worked. This thread can be closed i think.

old Re: Explosion position

DC
Admin Off Offline

Quote
cs2d lua cmd parse (click for a detailed description with examples) can't perform arithmetic operations (+, - ... etc). You have to do that in Lua. The change is trivial: Pass the calculated values to parse and not the formula!

Change line 7 to:
1
parse ("explosion "..(player(id,"x")+96).." "..(player(id,"y")+96).." 100 100 "..id)

Explanation:
Your code will pass this to parse (with the right values for X, Y and ID):
1
explosion X+96 Y+96 100 100 ID
× parse doesn't understand the +96!

The changed code will pass this to parse (with the correctly calculated values):
1
explosion X Y 100 100 ID
√ no +96 because the correct values are calculated in Lua


oh and by the way (unrelated to your actual problem):
Another common "mistake" you made (at the end of your line 7):
1
..""
This code snippet does nothing. It attachs (..) an empty string ("") to the end of another string. You never have to do this because - as already said - it does/changes absolutely nothing

old Re: Explosion position

En-Kay
User Off Offline

Quote
Thank you for your help. So this is the script at the end (probably something wrong but it works :D):

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
34
addhook ("say","rainoffire")
function rainoffire (id,text)

x=player(id,"x")
y=player(id,"y")
x1=player(id,"x")+128
x1b=player(id,"x")-128
y1=player(id,"y")+128
y1b=player(id,"y")-128
x2=player(id,"x")+256
y2=player(id,"y")+256
	
function expl1(id)
	parse ("explosion "..x1.." "..y.." 125 75 "..id)
	parse ("explosion "..x.." "..y1.." 125 75 "..id)
	parse ("explosion "..x1.." "..y1.." 125 75 "..id)
	parse ("explosion "..x1b.." "..y.." 125 75 "..id)
	parse ("explosion "..x.." "..y1b.." 125 75 "..id)
	parse ("explosion "..x1b.." "..y1b.." 125 75 "..id)
	parse ("explosion "..x1b.." "..y1.." 125 75 "..id)
	parse ("explosion "..x1.." "..y1b.." 125 75 "..id)
end


	if (sample.classes.class[id]==8) then
		if (text=="!spell") then
			if (player(id,"money")==16000) then
				expl1(id)
			else
				msg2 (id, "You do not have enough mana!")
			end
		end
	end
end

old Re: Explosion position

DC
Admin Off Offline

Quote
Yes, there are some things you could improve:

• You are declaring a function (expl1) within another function (rainoffire) which is probably not what you really want to do (also it's not efficient)

• You could use the variables x and y instead of calling the player command so many times (would be faster)

• Indentation is not 100% correct

• Advanced: You could make the code shorter / more beautiful with clever loops and arrays

old Re: Explosion position

En-Kay
User Off Offline

Quote
Yes, I know, But I am not so advanced in lua coding.

edit:

What do you mean "indentation is not 100% correct."?
edited 1×, last 30.08.14 07:40:00 pm

old Re: Explosion position

DC
Admin Off Offline

Quote
Your usage of tabs. This would be the right indentation which also clearly reveals that you are defining a function in a function:

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
34
addhook ("say","rainoffire")
function rainoffire (id,text)

	x=player(id,"x")
	y=player(id,"y")
	x1=player(id,"x")+128
	x1b=player(id,"x")-128
	y1=player(id,"y")+128
	y1b=player(id,"y")-128
	x2=player(id,"x")+256
	y2=player(id,"y")+256
	
	function expl1(id)
		parse ("explosion "..x1.." "..y.." 125 75 "..id)
		parse ("explosion "..x.." "..y1.." 125 75 "..id)
		parse ("explosion "..x1.." "..y1.." 125 75 "..id)
		parse ("explosion "..x1b.." "..y.." 125 75 "..id)
		parse ("explosion "..x.." "..y1b.." 125 75 "..id)
		parse ("explosion "..x1b.." "..y1b.." 125 75 "..id)
		parse ("explosion "..x1b.." "..y1.." 125 75 "..id)
		parse ("explosion "..x1.." "..y1b.." 125 75 "..id)
	end


	if (sample.classes.class[id]==8) then
		if (text=="!spell") then
			if (player(id,"money")==16000) then
				expl1(id)
			else
				msg2 (id, "You do not have enough mana!")
			end
		end
	end
end

old Re: Explosion position

DC
Admin Off Offline

Quote
It's simply not necessary to nest the functions
Here's a cleaner solution - I didn't test it though. I also replaced the ugly repetitive code with 2 loops (the condition is necessary to skip the center explosion directly at the player).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
addhook("say","rainoffire")
function rainoffire(id,text)
	if (sample.classes.class[id]==8) then
		if (text=="!spell") then
			if (player(id,"money")==16000) then
				expl1(id)
			else
				msg2(id, "You do not have enough mana!")
			end
		end
	end
end

function expl1(id)
	x=player(id,"x")
	y=player(id,"y")
	for ix = -1,1 do
		for iy = -1,1 do
			if ix ~= 0 or iy ~= 0 then
				parse("explosion "..(x + ix * 128).." "..(y + iy * 128).." 125 75 "..id)
			end
		end
	end	
end
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview