ID:158270
 
I'm curious about how I would go about doing this, and I'm confident I could do it, just not really sure where to start so some theories or pushes in the right direction would be appreciated.

I was wondering rather than like pressing a "Run" button then pressing and holding the arrow keys to start running, it'd be much easier to just hold down the arrow key in one direction, build up speed and eventually switch to running, and if you change directions, you lose some speed, but not enough to stop the process. But halting all together for a few seconds would mean you have to do the process all over again..

Would I have to have a proc constantly draining like a RunningSpeed var, and using North(), East() etc. procs, build up a counter var to which the speed of running/moving could be determined, and by not moving one variable would gain dominance and switch it..

I'm not sure if I answered my own question or just further confused myself on the topic..x.x
//time spent standing still to lose our running bonus
var/const/RUN_TIMEOUT = 10
//number of steps we must take to speed up 1 tick
var/const/STEPS_PER_SPEEDUP = 5
//number of steps we lose for turning
var/const/PENALTY_FOR_TURN = 5

mob
var/tmp/running = 0
//steps after maxrunning won't count. In this case, our speed is reduced by 3 ticks at most
var/maxrunning = 3 * STEPS_PER_SPEEDUP
var/tmp/nextmove = 0
var/minspeed = 6
proc
//tell us how long we should delay between steps
getMoveDelay()
return minspeed - round(running / STEPS_PER_SPEEDUP)
speedUp()
running = min(running, maxrunning)
slowDown()
running = max(running - PENALTY_FOR_TURN, 0)

client
Move(Loc, Dir)
//restrict movement speed
if(mob.nextmove < world.time)
return 0
//reset the running counter if we stand still for too long
if(mob.nextmove + RUN_TIMEOUT < world.time)
mob.running = 0
//store our mob's current dir because it will change after we move
var/D = mob.dir
//perform the default action
.=..()
if(.)
//if we are taking steps in the same direction, increase the running counter
if(D == Dir)
mob.speedUp()
//otherwise, reduce it
else
mob.slowDown()
//set when we can move next
mob.nextmove = world.time + mob.getMoveDelay()
In response to Garthor
That was a brilliant example..I hadn't even thought to use world.time.. I feel utterly stupid because its probably the most logical thing to use.

But can I ask something, whats the difference between using

.=..()

and
..()


Does it make a difference in ordinary code snippets? Or just ones like this this one? I understand, it saves a variable return, but honestly.. in the time I have been working with BYOND, I regret to say, returns was not one of my strong points. I've been getting by, by using alternative methods which are probably inefficient.
In response to DemonicK
DemonicK wrote:
But can I ask something, whats the difference between using

.=..()

and
..()


If you use the forum search you'll find many explanations on this.
The second one only calls ..() (which is the parent proc). Like any proc that is just called like that, whatever value it returns is lost as it's not used in any way (e.g. if you all you do in a statement is call rand(), it generates and returns a random number but you're not doing anything with it, so it's lost).
The first one [is a normal assignation using the = operator and it] calls ..() and assigns what it returns to the variable named "." , which is a built-in variable that all procedures have as a local var (other vars like that are usr and src) . You could assign the value to any var you want, it's just that the . var is one that gets automatically returned when the proc ends if you don't explicitly return something different. The following two snippets are equivalent:
mob/Move()
. = ..()
do_stuff_here
mob/Move()
var/Val = ..()
do_stuff_here
return Val

Things of interest you may want to read up on in the DM Reference and other resources (use the forum search, for instance):
  • ..() (proc)
  • . (var)
  • return (statement)