ID:261488
 
Problem: My spin buttons in the advance/redeploy boxes in Incursion seem to be getting stuck and keep going even after the mouse is released. What's wrong with this code that could cause such a thing?
obj/considered/redeploybox/button/spin
var/mousetimer/timer

army/icon_state="spinunit"
rsc/icon_state="spinrsc"

Click()
// this overrides obj/considered/Click()
// which calls C.Click(src); C is a /consider datum

MouseDown()
C.Click(src)
timer=new(10,usr.client,src,"Trigger")

MouseDrop()
if(timer) del(timer)
MouseUp()
if(timer) del(timer)
proc/Trigger()
C.Click(src)
if(!timer || !C || !C.owner || !C.owner.mob || timer.client!=C.owner.mob.client)
if(timer) del(timer)
return
timer.delay=max(2,timer.delay-2)
timer.Reset()

mousetimer
var/obj/O
var/procname
var/delay=10
var/client/client
var/theargs

New(_delay,client/C,obj/_O,_procname,_args)
O=_O
procname=_procname
delay=_delay
client=C
theargs=_args
Reset()

proc/Reset()
sleep(delay)
if(O)
spawn(-1) call(O,procname)(theargs)
else del(src)

Lummox JR

New(_delay,client/C,obj/_O,_procname,_args)
O=_O
procname=_procname
delay=_delay
client=C
theargs=_args
Reset()

proc/Reset()
sleep(delay)
if(O)
spawn(-1) call(O,procname)(theargs)
else del(src)</DM>

You had better spawn that call to Reset() in New(). Otherwise, new() won't return to the caller until after the sleep finishes, so the object's timer variable might not get set before the MouseUp() event.

Also, I just thought it was worth mentioning that you could accept any number of additional args, make a list out of them, and pass that to your callback via call(O,procname)(arglist(theargs)).
In response to Dan
Dan wrote:
You had better spawn that call to Reset() in New(). Otherwise, new() won't return to the caller until after the sleep finishes, so the object's timer variable might not get set before the MouseUp() event.

I finally realized that might be the case, so I changed the sleep() to spawn(). It had been a spawn originally, but for some reason didn't work right in its first implementation. My new spawn is inside the Reset() proc, but that shouldn't cause a problem for New() returning now because it spawns out all the code within the proc.

Also, I just thought it was worth mentioning that you could accept any number of additional args, make a list out of them, and pass that to your callback via call(O,procname)(arglist(theargs)).

Good idea. I'll have to consider that for future reference.

Lummox JR