ID:157402
 
How would I make a Beam shoot outwards a desired number of tiles, and then retract to the one who fired it, then delete itself? Thanks in advance!
Xorbah wrote:
How would I make a Beam shoot outwards a desired number of tiles, and then retract to the one who fired it, then delete itself? Thanks in advance!

obj/beam
New(loc, dir, amount)
spawn()
for(var/i = 1 to amount)
step(src, dir)
for(var/i = 1 to amount)
step(src, turn(dir, 180))
del(src)
mob/verb/beamer()
new /obj/beam(loc, dir, 5)

No, but seriously...

What exactly do you mean? Normally I would talk about just how to move objects around, but you specifically say "Beam" and many people mean "A projectile which leaves a trail behind as it moves" when they say just "Beam."

I'll start by at least telling you to look up step() and del. Then you also need to decide a few more things about how you want it to work. What does "retract to the one that fired it" do? Just retrace its step back to the spot it started? Can the object that fired it move while the beam is moving, and if so should the beam return to the firer's new location instead of where the firer was? And, as I said before, what is the behavior of the "beam?"

If you're looking for something extremely simple, my initial jesting answer might still be close to what you want, but I can't say anything for sure since I'm not sure what you want.
In response to Loduwijk
I meant by, "Beam" that it would be a projectile that could fire outwards a certain number of tiles that you wanted it, and then return to the user. And no, the user would not be able to move.
In response to Xorbah
Xorbah wrote:
I meant by, "Beam" that it would be a projectile that could fire outwards a certain number of tiles that you wanted it, and then return to the user. And no, the user would not be able to move.

Because of your second sentence and because you have not asked about that, I will assume you have already taken care of locking movement and will not mention that.

Look up the function step(), the atom variable "dir," probably sleep(), and for loops if you don't know about them.

step() will allow you to physically move the object. sleep() will allow you to slow it down so that it does not all happen instantly (since you likely want the effect to be visible), and you will need to specify a direction, possibly the firer's dir.

So you create a new beam object, then you step(the beam, the direction) a certain number of times, then, since the firer will not be moving, you can step the beam the same number of times in the opposite direction, which would be the direction turn(direction, 180), so you might want to look up turn() as well. The directional rotation turn(), not the icon turning function.

If you want the beam to shoot in something other than a straight line, such as if you have a target, then you need to figure out a way to always make the direction be toward the target instead of having the direction be static, in which case you can use get_dir().

Throw the movement into a loop which loops X iterations, and the beam will move X tiles.

Here is an example of shooting in a specific direction, then a second example where instead you throw a stone at a target. One of them will be very close to what you want, depending on whether you are shooting in a direction or at a target.
// shoot in direction

obj/gun
// to pick it up
verb/get()
set src in view(1)
Move(usr)

// shoots a bullet object
verb/shoot()
// first we create it; as specified for "new" first argument by default is where to create
// usr is the mob using the verb, so usr.loc is the turf that player is standing on; create the bullet there
var/obj/bullet/B = new(usr.loc)
// the bullet's own project() handles its firing, so call it
B.project(usr.dir, 5)

obj/bullet
// make this so that it knows how to be a projectile
proc/project(direction, amount)
// step "amount" number of times, as specified by caller
for(var/i = 1 to amount)
// src is the "source" of the function, the bullet
step(src, direction)
sleep(1)
// delete it when it's gone its max distance
del(src)

// just delete it prematurely if it bumps into anything
Bump()
del(src)

// shoot toward target

obj/stone
verb/get()
set src in view(1)
Move(usr)

verb/throw(mob/target in oview())
for(var/i = 1 to 5)
var/direction = get_dir(src, target)
step(src, direction)
del(src)

Bump()
del(src)