Looping tween_move?
4 replies



23.08.14 12:47:59 am
I'm still a super noob. Give me all your constructive criticism and tips/help.
Code:
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
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
addhook("movetile","ctm")
function ctm(id,x,y)
if player(id,"team")==0 or 1 and x==0 and y==0 then
freehook("movetile","ctm")
msg2(id,"Take that gun!")
parse("spawnprojectile 0 30 64 64 160 135")
redb=image(gfx/noob/red.bmp",16,16,1)-- its just a red circle
imagehitzone(redb,102,-16,-16,32,32)
tween_move(redb,2000,200,0)--is there way to loop...
timer(2000,"first")
function first()
tween_move(redb,2000,0,0)
timer(2000,"second")
function second()
tween_move(redb,2000,500,0)
timer(2000,"third")
function third()
tween_move(redb,2000,0,0)--...these actions?
end
end
end
end
addhook("hit","bhit")--When I hit redb.bmp with AK47, nothing happens?
function bhit("redb,1,0,30,x,y,0")
msg2(id,"Hit!")
end
function ctm(id,x,y)
if player(id,"team")==0 or 1 and x==0 and y==0 then
freehook("movetile","ctm")
msg2(id,"Take that gun!")
parse("spawnprojectile 0 30 64 64 160 135")
redb=image(gfx/noob/red.bmp",16,16,1)-- its just a red circle
imagehitzone(redb,102,-16,-16,32,32)
tween_move(redb,2000,200,0)--is there way to loop...
timer(2000,"first")
function first()
tween_move(redb,2000,0,0)
timer(2000,"second")
function second()
tween_move(redb,2000,500,0)
timer(2000,"third")
function third()
tween_move(redb,2000,0,0)--...these actions?
end
end
end
end
addhook("hit","bhit")--When I hit redb.bmp with AK47, nothing happens?
function bhit("redb,1,0,30,x,y,0")
msg2(id,"Hit!")
end
First of all: Your indention seems to be a bit random/wrong which makes your code hard to read. Indention is one of the first things you should always get right to make your script actually readable. Best practice: Always indent directly and properly while writing your script.
What you see after fixing indention is:
An end is missing (it's obvious after proper indention because the last line is still indented)
You are nesting functions (declare functions within other functions) which is wrong here
Note: You do NOT have to declare functions for timers directly after using the timer command. It doesn't matter where you declare them. Just do not declare them within other functions! (same for hooks btw!)
Sample how to do it right:
Explanation:
the first function will call the function "doSomething" after two seconds using a timer
doSomething is declared OUTSIDE the first function (the only right way to do it), prints a message and CALLS ITSELF AGAIN after 2000 milliseconds = 2 secs. This causes an infinite loop so "looping..." will be printed every 2 seconds
You may also take a look at
timer
This for example will lead to exactly the same result and is a bit cleaner/better:
Note the additional parameters for
timer!
Code:
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
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
addhook("movetile","ctm")
function ctm(id,x,y)
if player(id,"team")==0 or 1 and x==0 and y==0 then
freehook("movetile","ctm")
msg2(id,"Take that gun!")
parse("spawnprojectile 0 30 64 64 160 135")
redb=image(gfx/noob/red.bmp",16,16,1)-- its just a red circle
imagehitzone(redb,102,-16,-16,32,32)
tween_move(redb,2000,200,0)--is there way to loop...
timer(2000,"first")
function first()
tween_move(redb,2000,0,0)
timer(2000,"second")
function second()
tween_move(redb,2000,500,0)
timer(2000,"third")
function third()
tween_move(redb,2000,0,0)--...these actions?
end
end
end
end
addhook("hit","bhit")--When I hit redb.bmp with AK47, nothing happens?
function bhit("redb,1,0,30,x,y,0")
msg2(id,"Hit!")
end
function ctm(id,x,y)
if player(id,"team")==0 or 1 and x==0 and y==0 then
freehook("movetile","ctm")
msg2(id,"Take that gun!")
parse("spawnprojectile 0 30 64 64 160 135")
redb=image(gfx/noob/red.bmp",16,16,1)-- its just a red circle
imagehitzone(redb,102,-16,-16,32,32)
tween_move(redb,2000,200,0)--is there way to loop...
timer(2000,"first")
function first()
tween_move(redb,2000,0,0)
timer(2000,"second")
function second()
tween_move(redb,2000,500,0)
timer(2000,"third")
function third()
tween_move(redb,2000,0,0)--...these actions?
end
end
end
end
addhook("hit","bhit")--When I hit redb.bmp with AK47, nothing happens?
function bhit("redb,1,0,30,x,y,0")
msg2(id,"Hit!")
end
What you see after fixing indention is:


Note: You do NOT have to declare functions for timers directly after using the timer command. It doesn't matter where you declare them. Just do not declare them within other functions! (same for hooks btw!)
Sample how to do it right:
Code:
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
function someFunctionThatIsCalledSomehow()
timer(2000,"doSomething")
end
function doSomething()
msg("looping...")
timer(2000,"doSomething")
end
timer(2000,"doSomething")
end
function doSomething()
msg("looping...")
timer(2000,"doSomething")
end
Explanation:


You may also take a look at

This for example will lead to exactly the same result and is a bit cleaner/better:
Code:
1
2
3
4
5
6
7
2
3
4
5
6
7
function someFunctionThatIsCalledSomehow()
timer(2000,"doSomething","",0)
end
function doSomething()
msg("looping...")
end
timer(2000,"doSomething","",0)
end
function doSomething()
msg("looping...")
end
Note the additional parameters for

edited 4×, last 23.08.14 10:07:47 am



I've one more question though, regarding hitzone and imagehitzone.
Code:
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
35
36
37
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
35
36
37
parse("sv_gamemode 2")
addhook("movetile","playermove")
function playermove(id,x,y)
if player (id,"team")==0 or 1 and x==0 and y==0 then
freehook("movetile","playermove")
msg2(id,"Take that gun!!")
parse("spawnprojectile 0 30 64 64 200 135")
redb=image("gfx/noob/red.bmp",16,16,1)
imagehitzone(redb,102,-16,-16,32,32)
end
end
numfired = 0
addhook("attack","fire")
function fire(id)
if id == 1 then
numfired = numfired + 1
end
end
numhit = 0
addhook("hitzone","contact")
function contact(imagehitzone) -- am i doing it right?
if contact == 1 then
numhit = numhit + 1
end
end
addhook("reload","akload")
function akload(id,mode)
if mode == 1 then
msg2(id,"You hit the target "..(numhit).." out of "..(numfired).." times.")
elseif mode == 2 then
numfired = 0
numhit = 0
end
end
addhook("movetile","playermove")
function playermove(id,x,y)
if player (id,"team")==0 or 1 and x==0 and y==0 then
freehook("movetile","playermove")
msg2(id,"Take that gun!!")
parse("spawnprojectile 0 30 64 64 200 135")
redb=image("gfx/noob/red.bmp",16,16,1)
imagehitzone(redb,102,-16,-16,32,32)
end
end
numfired = 0
addhook("attack","fire")
function fire(id)
if id == 1 then
numfired = numfired + 1
end
end
numhit = 0
addhook("hitzone","contact")
function contact(imagehitzone) -- am i doing it right?
if contact == 1 then
numhit = numhit + 1
end
end
addhook("reload","akload")
function akload(id,mode)
if mode == 1 then
msg2(id,"You hit the target "..(numhit).." out of "..(numfired).." times.")
elseif mode == 2 then
numfired = 0
numhit = 0
end
end
In Line 23: You could write
In case you want to use all the parameters which are passed to the hooked function by CS2D.
In your case you don't seem to use any of these values so you can simply do
You could also take just some of them (and use other names)
Only the order matters. You can't simply start with x. In case you want to get the damage you have to enter all the other parameters too because it's the last one
Your line 24 does not make sense though and probably leads to your problems. You're checking if "contact" - which is a Lua function - equals 1. This will never be true and I'm unsure why you added this condition or what you want to achieve with it. The hook is called only when something is hit anyway so you could probably do this:
Completely remove the condition
Optionally: Remove the "imagehitzone" parameter because you aren't using it in the function
Code:
1
function contact(imgID, playerID, objID, weapon, x, y, damage)
In case you want to use all the parameters which are passed to the hooked function by CS2D.
In your case you don't seem to use any of these values so you can simply do
Code:
1
function contact()
You could also take just some of them (and use other names)
Code:
1
function contact(image, player)
Only the order matters. You can't simply start with x. In case you want to get the damage you have to enter all the other parameters too because it's the last one
Your line 24 does not make sense though and probably leads to your problems. You're checking if "contact" - which is a Lua function - equals 1. This will never be true and I'm unsure why you added this condition or what you want to achieve with it. The hook is called only when something is hit anyway so you could probably do this:


Okay. Thanks for the help DC. For some reason I thought that "contact" needed to be equal to 1 or 0 as in true or false. Anyways, got it working now, thanks again for the help.



