ID:148394
 
On my new game that I recently began making you get a gun you also get a fire verb with it, the gun should be firing in the direction the player is facing but it just fires south.
I know where the problem is but I just cant seem to figure out howto make it work. This is the part of the code with the problem in it.

obj/weapon/M16/verb/Fire()
switch(dir)
if(NORTHEAST)
dir = NORTH
if(NORTHWEST)
dir = WEST
if(SOUTHEAST)
dir = EAST
if(SOUTHWEST)
dir = SOUTH

var/turf/newloc = get_step(src,dir)
var/obj/beam/B = new(newloc)
B.dir = dir
B.icon_state = "middle start"
B.next_state = "middle"
spawn() B.fireloop()

the problem is that the gun is not switching to fire the correct way. Can anyone help me sort out this problem?
I think it's because all uses of the variable dir in that verb will refer to the gun, not the player...

And since the gun is an object, it's dir variable doesn't change unless you tell it to... And the default is SOUTH, so it will always remain SOUTH, until you tell it to be something else...

You could either change the dir references to be usr.dir (I know, usr is bad, but not in a verb like this that will only have one potential usr, which is the mob carrying the gun)

However, it looks like the easiest way to do this is to just add a line like so:

Fire()
dir = usr.dir
// then the rest of the verb just as you have it coded

What this will do is set the gun's direction to the player's direction... Then, the rest of the verb will operate as if the gun was facing whatever direction the player is facing...
For a demo on projectile-firing and the like, check out my War Demo.

Hope that helps. :)