ID:171437
 
obj
test
Bump(atom/movable/A)
if(ismob(A))
A << "You're hit"
del(src)
proc/testproc(var/mob/u)
while(src)
step(src,u.dir)
src.overlays += src(locate(src.x+1,src.y,src.z))
sleep(10)
mob/verb/test()
var/obj/test/O = new(locate(usr.x+1,usr.y,usr.z))
O.testproc(usr.dir)


What i'm trying to achieve is an obj that keeps moving in the player's direction until it hits a mob. And every second that the obj exists, add an overlay to obj/test whilst giving the overlay the properties of obj/test.

However this isn't working at all, and i know that isn't the correct way of adding an overlay but i don't know how to show what i need in a better way.
Okay, the description of what you want is confusing. Are you trying to make a laser?
In response to Garthor
Yes i am.

A laser that gets bigger every second.
In response to DeathAwaitsU
Okay, I'll just assume you mean a laser that leaves a trail (like in LaserWars). So, simply:
obj
laser
density = 1
icon = 'laser.dmi'
New(var/atom/loc, var/dir, var/speed)
src.dir = dir
if(speed) pixel_step_size = 32/speed
..()
sleep(1)
if(step(src, dir))
sleep(speed-1)
new /obj/laser(src.loc, dir, speed)
del(src)

mob
verb
shootLaser()
new /obj/laser(src.loc, src.dir, 5)


It's a lot simpler than you'd think. The basic idea is that a laser is created at a point, and takes a step forward. After that, it waits for the step to finish, then creates a new laser. The new laser then does the same thing. One important line is the
if(speed) pixel_step_size = 32/speed
line. pixel_step_size is how many pixels it will move per tick. We know how long we'll be moving each time, so we can determine how many ticks it'll take to have a smooth movement, which is 32/speed. Also note that a speed of 0 will still be a speed of 1, but with slightly different beahvior (look up the difference between sleep(-1) and sleep(0)), but both will still move at 1 tile / tick.
In response to Garthor
Ok thanks, that worked great with one exception, i'm struggling with Bumped()

What i need to do is that if you Bump into any part of the laser, ALL of the laser is destroyed. That includes the moving laser and the trail.

I tried this approach but failed:

obj
laser
density = 1
icon = 'laser.dmi'
var/list/lasers = list()
New(var/atom/loc, var/dir, var/speed)
src.dir = dir
if(speed) pixel_step_size = 32/speed
..()
sleep(1)
if(step(src, dir))
sleep(speed-1)
var/obj/laser/L = new /obj/laser(src.loc, dir, speed)
lasers += L
del(src)
Bumped(atom/movable/A)
if(ismob(A))
for(var/X in lasers)
del(X)
del(src)


I'm also still not sure how this is working:
            if(step(src, dir))
sleep(speed-1)
var/obj/laser/L = new /obj/laser(src.loc, dir, speed)
lasers += L
del(src)


What does if(step(src, dir)) do and what are we deleting with del(src) and when? Oh and where are we creating a new laser and how come it isn't also moving and creating other lasers aswell?

And finally, can someone show me how i'd delete it after it's moved a certain amount of atoms? I'm pretty sure i'll understand how to do this after someone explains the above but i'd still like to know the good way of doing it instead of my usual bad way.
In response to DeathAwaitsU
DeathAwaitsU wrote:
I'm also still not sure how this is working:
>           if(step(src, dir))
> sleep(speed-1)
> var/obj/laser/L = new /obj/laser(src.loc, dir, speed)
> lasers += L
> del(src)
>

What does if(step(src, dir)) do and what are we deleting with del(src) and when? Oh and where are we creating a new laser and how come it isn't also moving and creating other lasers aswell?

if(step(src, dir)) is basically a shorthand way of saying "Step forward, and if you can, do this." What then happens is it sleeps for a few ticks, and then will create a new laser on top of it. The lasers += L was a line you added, and wouldn't work that way. What happens is that when a new laser is created, the current laser simply will wait for the new one to finish being created. However, the new one will also wait, and so on. The result being you have a long string of lasers, each of which waiting for the next one to be deleted. When one of them finaly can't step forward, a new laser isn't created, and it goes straight to delete itself, it hits the bullseye, causing all the other dominoes to fall like a house of cards, checkmate!

And finally, can someone show me how i'd delete it after it's moved a certain amount of atoms? I'm pretty sure i'll understand how to do this after someone explains the above but i'd still like to know the good way of doing it instead of my usual bad way.

You'd pass a fourth variable into the New() proc, I'll say range. Then, when creating the new laser, you'll use range-1. When range reaches 0, you delete the object.

Anyway, if you want to have a laser be deleted when the beam is interrupted, there are a number of ways you can do it. The easiest way would be to move most of the stuff after ..() out of New() and into a new proc (but not the del(src) line). Then, in New(), create the next laser, add it to a new variable, "nextLaser," and then call the proc to start the laser. You'll also have to pass ANOTHER argument into New(), and set that to "lastLaser." Then, in obj/laser/Del(), have it delete nextLaser (if it exists) and lastLaser (if it exists). You'll only delete a laser if it reaches its limit, it hits something, or it is hit by something.
In response to Garthor
Ok but i'm still not getting it.

I've tried all sorts of method but the range - 1 thing only seems to be called when the laser is destroyed.

Nothing that I try is working.
In response to DeathAwaitsU
I'm doing this right now:

obj
laser
density = 1
icon = 'laser.dmi'
New(var/atom/loc, var/dir, var/speed, var/range)
src.dir = dir
if(speed) pixel_step_size = 32/speed
..()
sleep(1)
if(step(src, dir))
sleep(speed-1)
var/obj/laser/L = new /obj/laser(src.loc, dir, speed)
range --
if(range<=0)
del(src)
del(src)


That's basicly doing nothing different happens from before.

And i'm still unsure about Bumped.
In response to DeathAwaitsU
bump!
In response to DeathAwaitsU
Am i not being clear?
In response to DeathAwaitsU
If you're still asking about the ENTIRE laser to be deleted when one bumps into it, why not (for a quick fix until you get a polished version) simply have it so that when the user bumps the laser, have the laser move upwards and downwards, bumping into each other laser, and have it so that if the laser bumps a laser, both of them are deleted. Thus the user bumps the laser, the laser bumps the other lasers an voila they're all gone.

In a sense when the user bumps the laser, they shoot a laser that nobody sees. Like I said, a quick fix.
- The Conjuror