ID:1905942
 
(See the best response by IchiroKeisuke.)
Hey guys, I'm fairly new to Byond coding, and I've been searching around this forum for my problem, but in vain so here it is:

The player in my game has a verb called "Rest" which is supposed to restore his powers, and with an icon_state that is supposed to make him unable to move. However I can move freely around the map while he rests.
How do I stop him from moving?
Best response
You'd probably want to use a var for movement

mob/var/can_move=1


Under the mob's Move() procedure, just stop him from moving if he can't move.

mob
player
Move()
if(!can_move)return
..()


under your rest verb you would just set can_move to 0.

Hope this helps.
It's best to define a variable on the mob that will prevent movement.

mob
var/tmp
next_move = 0 //we dont want to save world time offsets
Move(atom/NewLoc,Dir=0,Step_x=0,Step_y=0,Source=null)
if(Source==src) //this is a self-directed movement
if(next_move>world.time) //if next move is greater than world time
return 0
return ..() //perform default action

//only use the below two functions if you need to save next_move data
Write(savefile/F)
..()
//store the remaining time in next_move (optional)
F["next_move"] << max(next_move-world.time,0)

Read(savefile/F)
..()
//load the next_move value from file and adjust to current time
F["next_move"] >> next_move
next_move += world.time

//we need to change some stuff to allow self directed movement distinctions

client
Move(atom/loc,dir)
if(!mob||!loc) return 0
walk(mob,0)
//define pixel movement if you want to use it
#ifdef PIXEL_MOVEMENT
var/x = 0
var/y = 0
if(dir&NORTH) y=1
else if(dir&SOUTH) y=-1
if(dir&EAST) x=1
else if(dir&WEST) x=-1
return mob.Move(mob.loc,dir,mob.step_x+mob.step_size*x,mob.step_y+mob.step_size*y,mob)
#else
reurn mob.Move(loc,dir,0,0,mob)
#endif


I am on my phone atm, so if this has problems, i will fix them when i get home. The basic idea is that when you don't want the player to move, just set their next_move to a value greater than world.time by x ticks, which represents how long they are unable to move for.

If you want to lock movement for an unspecified period of time, just set next_move to positive infinity.

Ichiro's solution is the classic solution, but i use something a bit different because the default solution, i find often results in the design of a managed state machine. Managed state machines are excellent for handling states that persist over short periods of time, but once you throw long term actions into the mix, bad things can happen if the player is saved at the wrong time due to the state slipping out of the controls that ensure variables are maintained. I feel that while my approach may be harder to understand, it better conforms to good design standards and helps avoid a lot of future sanity testing and forced maintenance. In addition, state collisions are easier to prevent using a time offset rather than a boolean.

Also, i like being able to move things around by various forces and treat different movements differently. Self-directed movements can be distinguished by the source argument i have added to Move. The reason I tend to use this is because I like projectiles and attacks to cause players and npcs to slide around. Simply magnetizing players on the spot if they are in an action state makes games inflexible and unfun. The easy solution is above, for sure. But the added effort of my approach is a small knowledge gap that's worth closing.
Thank you very much to both of you for the fast replies. Unfortunately, I get a black screen from Ichiro's method (because I think that my player's Move() proc is built in and I have no idea how to modify it). The cause for the black screen is because there's something wrong with the code after I implement Ichiro's method. I know this because after I delete the code, the black screen disappears.

I can barely understand Ter's method though (maybe it's because I got into the developer side of Byond 3 days ago haha). Where do I implement your code Ter? Do i put it in the Player's Procs, Player's code or do i put it in the main .dm file?
My question probably sounds noobish but I feel so darn lost in all of this info.
In response to Masta Sparx
Baby steps.

It might be a good idea to read the guide, especially since your new : http://www.byond.com/docs/guide/

My guess at why you got the black screen is you disabled movement by default. It would help if you post the relevant code as a snippet so we can see what you tried.

(you can do this by copying/pasting into dm tags.

<*>Your code.</*>.

* should be changed to dm)
@Ichiro, the reason he got the black screen is actually because your code doesn't work.

mob
player
Move()
if(can_move)
return ..()
return 0


You aren't returning anything. Move() MUST always return a value.

But yeah, my code is probably not something you want to tackle for a little while, Masta. Read that guide! It's where we all started.
In response to Ter13
@Ter, yeah your right - my bad, this
mob
player
Move()
if(can_move)
return ..()
return 0

is the correct way to do it.

(I'm on linux and couldn't/can't test anything I wrote in DM, so bear with me here)

@OP, This corrected snippet is most certainly the one you need for this issue, but make sure to look at the guide and absorb some tutorials before trying to tackle a full game project.

You can hit F1 (brings up the docs) to look up anything in particular you don't understand, it's always useful as it usually includes a detailed explanation and an example snippet.

GL!

EDIT:I looked up a snippet from my project and I noticed I rarely modify Move() anymore - I just ignore it and use my own system for movement, but it would be damn near impossible to explain it to someone who just started. So I'm glad I threw that simple solution out there, as it's much easier to grasp.
So I'm glad I threw that simple solution out there, as it's much easier to grasp.

Agreed, it's probably more suited to the OP.
Still not working. I can move freely around while resting.

I am also using the built-in movement system, I don't have a Move() proc in my game.

I did read the guide before attempting to make a basic game and I've been following Falacy's tutorials and Zilal's with a pretty good understanding so far. Guess I'll re-read them and see if I can come up with something on my own for movement. I'll post here the solution, for anyone who in the future will have the same problem as me.
Are you seeing can_move to 0 when you start resting?
It's working now! what I did was:
//set tha mothafuckin var
mob/var/can_move = 1

mob
Player
Move()
if(can_move)
return ..()
return 0

//add if he can or can't move below the icon_state of the action performed
icon_state = "rest"
can_move = 0
.
.
.
icon_state = "default"
can_move = 1


That did the trick! No more running around like a rabid sheep while resting! Thank you for the support!