ID:116658
 
Hey guys. I made this little(I emphasize little) chunk of code here earlier toda-err, yesterday, in about 30 minutes. It is a simple piece that allows you to run after moving for a certain amount of time. Whenever I have come across a game that has this kind of feature, it is almost always laggy and buggy. However, while testing this, I got no bugs at all.

NOTE that I am using Deadron's Event Loop library. If you plan on testing this for yourself you will need it.

/*
RunningLoops.dm

This file contains code that makes running possible.

Definitions:
WALKING - The number (2) next to this defines how many ticks it takes to take another step while walking.
RUNNING - The number (0) next to this defines how many ticks it takes to take another step while running.
TIME2RUN - The number(30) next to this defines how long it takes to start running.

Move():
First it checks if you are already moving. If so, it stops you.
Then it sets your moving var to TRUE.
Then it changes your icon state depending on if you are running or walking.
Then it does the normal action. It uses an if() statement to see if the movement succeeded.
If it did(Meaning you cant start running by walking against a wall), then if does its checks and actions.
Touching this part isn't necessary, as it doesn't control anything that should need changing.
Touch it at your own risk. I'm not fixing it if it breaks.
If the movement failed, then it sets your moving to FALSE and returns so you can try to move again.


base_EventCycle():
This is a proc found in Deadron's EventLoop library. Download it if you do not have it, or you will get errors.
This is 4 lines of code that controls your running completely. It runs every tick(1/10 of a second). It checks to see
if you can run or not. Touch this at your own risk. This uses TIME2RUN a few times, and that is all you should change.
*/





#define WALKING 2
#define RUNNING 0
#define TIME2RUN 30

mob{
var{
tmp{
moving = FALSE
walk_time = 0
steps = 0
move = WALKING
}
}
Move(){
if(moving) return FALSE
moving=TRUE
if(move==RUNNING) icon_state="Run"
if(move==WALKING) icon_state=""
. = ..()
if(.)
steps++
var/s=steps
spawn(move)
moving=FALSE
spawn(5)
if(s==steps)
walk_time=0
steps=0
if(move==RUNNING && walk_time <= TIME2RUN) icon_state=""
else
moving=FALSE
return FALSE
}
base_EventCycle(){
if(moving)
walk_time++
if(move==WALKING && walk_time>=TIME2RUN)
move=RUNNING
else if(move==RUNNING && walk_time<=TIME2RUN)
move=WALKING
}
}


Now that you are past that, did any of you "Excelsior" programmers see anything major that should be fixed? I want to make sure this piece is good to go.
if(..()) is not the best thing to do. Instead, you should set . to ..() and do if(.). This is so your Move() proc returns anything other than FALSE.
However unlikely it is for you to be doing if(Move(T)) anywhere in your code, if you actually do try that with your current code, it would not work properly and you might have a hard time figuring out why.
Alright, thanks.
Is that for Hulio-G's game by any chance?
No, it isn't. Is he still working on that? As far as I knew they lost the source. *cough* Partly my fault.
I guess. Just the other day he asked for my help on a mechanic very similar to that.
Oh. Well, it isn't. I was helping someone else out, but I rather liked how I did this, so I put it up here to make sure there wasn't anything really wrong.
I see.

Well, what I would say is having your run timer in Move() is probably the absolute wrong way to go about it. You should have some sort of mob/proc/MoveLoop() which handles all your movement-related functions. Put your clock in that.