ID:170867
 
I would like players to be able to pull certain objects after them, so like aftet using pull on something, it keeps following you until you use it again. Could someone help me with making one?
Give the mob a variable that will reference the object to be pulled. Probably either var/obj/pull or var/atom/movable/pull

Have the verb locate an object ahead of you when it is used, and set that object to be referenced by pull if one is found.

In mob/Move, check to see if pull is true. If it is, and if you successfully move, move it into your old location.
In response to Loduwijk
Umm, Yeah, thats exactly waht i thought about doing, But the whole reason i posted here was the fact that i didnt know enough code to do it =P
In response to Takaru
mob/var/obj/pull

mob/verb/pull()
if(pull)pull=null
else pull=locate()in get_step(src,dir)

mob/Move(NewLoc,Dir=0)
var/OldLoc=loc
.=..()
if(.&&pull)pull.loc=OldLoc

There is an example of each part I said needs to be done. Well, more than just an example, as that is pretty much all you need to do.
In response to Loduwijk
Could you explain what that does? Otherwise, it just wouldnt feel right >_> (Not just what it does in general, i mean like, for each line ><)
In response to Takaru
mob/var/obj/pull
/*The above variable will keep track of what is being pulled.
It will equal null if nothing is being pulled.*/


mob/verb/pull()
if(pull)pull=null //If you are pulling something, stop
else pull=locate()in get_step(src,dir)

Look up get_step in the help file. As you will see, it gets a turf a step away. In this case, a step in front of the player's mob.

Look up locate as well. It returns an object. If you do not supply an argument, and we are not, and you are setting a variable to reference its return value, which we are, the type of object it finds will default to the type the variable is defined as. The variable is mob/var/obj/pull, which means it is of type /obj and locate will return an object of type /obj.

The get_step is going to return a /turf object, and the code says "locate() in get_step", so the object it returns is going to be in the turf specified. If there is no object of type obj in the turf, it will just return null.

So at the end of that verb, the pull variable will either equal null, meaning you are not pulling anything, or it will reference an object that was in front of you, meaning you are pulling that object.

mob/Move(NewLoc,Dir=0)
var/OldLoc=loc
//The above var will keep track of where you were before trying to move

.=..() //This makes the normal movement happen.
if(.&&pull)pull.loc=OldLoc

As for the last line, you need to know about the "." and about how if statements work.

The "." is basically a variable. Normally when you want the function to return something you do it with something like "return variable". If the function ends without coming to a return statement though, it will usually return null as default. The "." is a return value, meaning that whatever you set it as will be returned if no return statement is found.

The Move function normally returns 1 on success and 0 on failure. Normally, you don't need to know if a movement was successful or not, but you very well may later on; so it is best to let it return its normal value even if you don't plan on using it any time soon. In this case though, we will be using it right away.

"..()" means "do what this function normally would have done if I had not overwritten it", and you may have used it in Login before. As I said before, Move returns 1 on success and 0 on failure, so when we are doing ".=..()" we are not only telling it to do what it normally does, but we are also telling whatever it returns (1 or 0) to be placed into the "."

Now you need to know exactly how if statements work. Whatever is placed between the parentheses in an if statement is evaluated, and the block of code owned by the if statement executes if it evaluates to true, but not if it evaluates to false.

In Byond, false is any number 0, empty text string (that is, ""), or null. Anything else is true. So if(1) or if("c") or anything similar will both evaluate to true and run the if's code block, but if(0), if("") and if(null) will not.

Since . will be either 1 or 0, that means it will be true or false. Also, pull is going to be either null (false) or a referene to an object (true).

As for the && operator, you can look that up in the help file. I will explain it briefly here though. It evaluates to true if the equations on both side evaluate to true, or false if either or both are false.

Thus if(.&&pull) will evaluate to true only if two conditions are met: the movement was successful and you are pulling something.

If the two conditions are met, it executes the pull.loc=OldLoc. OldLoc is the variable that was set to the mob's loc before it moved, so that means that pull.loc (the variable determining where the pulled object is) will be set to where you were standing before you move, effectively pulling it along behind you.
Takaru wrote:
I would like players to be able to pull certain objects after them, so like aftet using pull on something, it keeps following you until you use it again. Could someone help me with making one?

If you're interested in studying how this might be done, Air Mapster's Draggin' Ballz has code that does this. The source code is available with a BYONDScape subscription. (However since he wrote it for the 4K Challenge of 2002, you might need some help decoding it.)

Lummox JR