ID:2698685
 
(See the best response by Lummox JR.)
Code:
The magnetization proc (the walk_towards will probably be replaced with a step towards):
obj/proc
Magnetization()
LOOP
for(var/mob/M in oview(8,src))
if(M.Magnetized)
walk_towards(src,M)
sleep(5)
goto LOOP

In the object:
New()
src.Magnetization()


Problem description:
So I'm trying to make it so I can Magnetize a person (that part is done already) and have a metal object be pulled towards them mid-movement by having the object as a projectile check to see if there's anything magnetic near it. I haven't 100% figured out what the problem is, and I don't know why it doesn't work. Its as if the proc isn't triggering? Any help?
Best response
There are a few problems here, one of the biggest being that you're using goto incorrectly. This is a job for a while() loop.

You should also be spawning the Magnetization() call in New(), so New() can return to the caller.

view() is not the correct call here, since magnets should work through opacity, so you'd want range() instead.

Looking ahead, though, it would be better not to move toward the first object in range but instead to get a directional vector based on all of the objects in range, and work with that.

var/vx=0, vy=0, dx, dy, strength
for(var/mob/M in orange(8,src))
if(!M.Magnetized) continue
dx = M.x - x; dy = M.y - y
if(!dx || !dy) continue
strength = 1 / (dx*dx + dy*dy)
vx += dx * strength
vy += dy * strength
velocity_x += vx
velocity_y += vy

This can be adjusted to allow for step offsets.
Uhh...I use a regular based game, not pixel movement based, just so u know, lol. The Goto is how I've always made loops and it works fine, lol. Its supposed to be an individual object, like a weapon, not an actual magnet. The person becomes magnetized and then the metallic weapon tracks towards them, is how its supposed to work. Its for a Naruto game, so I should probably just show the full code.
obj
Shuriken
density=1
Projectile = 1
name = "Shuriken"
icon = 'throwing weapons.dmi'
icon_state = "shuriken"
New()
src.Magnetization()
Bump(A)
if(ismob(A))

That's part of the code, the part that's actually relevant. Its a thrown weapon that originates from the user, if it hits anything opaque its just gonna despawn (This is because anything opaque in the game is also dense), lol. New is supposed to be when the object spawns from the user, and then its supposed to start checking if there's a magnetized person around it to track to. The problem is I can't figure out why its not tracking, like I said its like the proc itself isn't initiating. Edit: I tried switching it to while and now its crashing the game or just not working, so I think goto is the better option, lol. This is likely because while creates a full loop with no delay, but this requires delay.
just use a spatial hashmap

also all of this code is so extremely cursed lmao
I have no idea what that is dude, lol. Also there's more to the Shuriken code, I just didn't include it cuz its not relevant.
In response to Narutogx2
Narutogx2 wrote:
The Goto is how I've always made loops and it works fine, lol.

I'm not sure anyone can help you then. Learning as you go is part of the experience. You basically just said "I don't want to learn how to do it right."

Abuse of goto is a symptom of a bigger issue, which is that you're not paying attention to how the flow of the proc code works. I know replacing your gotos with while() loops won't fix the immediate issue, but it will help fix the underlying issue that's keeping you from seeing the problem.
I tried replacing it with a while, and it crashed the game, lol. It requires a delay or else its going to break the game, which is why I used goto. And even if it did work it still doesn't address the issue or how to do it, lol. Its just a nitpick in the code really. Right now when I try to trigger the proc with the while, with the delay I managed to figure out, under New() it just breaks the throw verb entirely, and under Move() it completely stops movement but still spawns in. So how else can I do this? Edit: My basic goal here is to pretty much make it change its movement from going straight, to going towards a magnetized person automatically.
Its just a nitpick in the code really.

It's not.

And even if it did work it still doesn't address the issue or how to do it, lol.

The guy answered your question.
Which guy? Also I don't think anyone's answered my question because then I would have the actual solution and it would be working, which I don't and it isn't, lol.
Which guy? Also I don't think anyone's answered my question because then I would have the actual solution and it would be working, which I don't and it isn't, lol.



Lum's answer works. You just have to do the legwork yourself. Instead of answering with sass, maybe asking questions about what you don't understand would get you closer to a solution.
In response to Ter13
Ter13 wrote:
Which guy?


There's two people, now three, responding to this thread.
In response to Ter13
Ter13 wrote:
Which guy? Also I don't think anyone's answered my question because then I would have the actual solution and it would be working, which I don't and it isn't, lol.



Lum's answer works. You just have to do the legwork yourself. Instead of answering with sass, maybe asking questions about what you don't understand would get you closer to a solution.

So A. I have no idea what any of the code he wrote means, much less how to understand it. It just looks like gibberish for like 99% of it. B. I don't even know where to start to try to implement or understand it. C. When I try calling a proc in the New() section of the object, it completely breaks and doesn't spawn at all.
Alright this thread is solved, I managed to do it on my own. I just needed a spawn(0) infront of the proc when it was called in New(). I didn't use any of the coding that was posted by anyone here. The only thing I did take from here was the While advice, cuz that was correct yeah.
obj/proc
Magnetization()
while(src)
sleep(2)
for(var/mob/M in oview(8,src))
if(M.Magnetized)
walk_towards(src,M)


obj
Shuriken
density=1
Projectile = 1
name = "Shuriken"
icon = 'throwing weapons.dmi'
icon_state = "shuriken"
New()
spawn(0) src.Magnetization()

Everyone else was literally just overcomplicating it, as was I at the start, lmao.
A for() loop for the view stuff is actually inappropriate here since there may be more than one mob that fits. Either use locate() or break out of the loop after moving. Or, use a solution that takes all the nearby mobs into account as I mentioned.
In response to Narutogx2
Narutogx2 wrote:
I have no idea what that is dude, lol.

google exists, this is literally exactly what you described that you want
This thread is closed, lol Idk why people are still responding. Also it really wasn't, I wanted something simple, not something I don't even know how to do, and I figured it out myself, lol.
In response to Narutogx2
I mentioned the need for a spawn() in the very first response.
In response to Narutogx2
No need to thank me

var list/magnetar = new

mob/var/tmp/is_magnetar = TRUE
obj/var/tmp/attracter = null

world/New()
spawn(1)
for()
sleep(world.tick_lag)
for(var/obj/o in magnetar)
if(!o.attracter || !step_towards(o,o.attracter) || o.loc == o.attracter:loc)
magnetar -= o


mob/Move()
..()
if(is_magnetar)
for(var/obj/o in view(8,src))
o.attracter = src
magnetar |= o
In response to Narutogx2
Narutogx2 wrote:
Alright this thread is solved, I managed to do it on my own. I just needed a spawn(0) infront of the proc when it was called in New(). I didn't use any of the coding that was posted by anyone here. The only thing I did take from here was the While advice, cuz that was correct yeah.
> obj/proc
> Magnetization()
> while(src)
> sleep(2)
> for(var/mob/M in oview(8,src))
> if(M.Magnetized)
> walk_towards(src,M)
>

> obj
> Shuriken
> density=1
> Projectile = 1
> name = "Shuriken"
> icon = 'throwing weapons.dmi'
> icon_state = "shuriken"
> New()
> spawn(0) src.Magnetization()
>

Everyone else was literally just overcomplicating it, as was I at the start, lmao.

This was the answer, lol. I don't even know what that recent guy is even trying to do with his code.
I'm gonna throw a lock on this since it's become obvious that further discussion on this thread isn't going to be much more enlightening to anyone.

I'm glad you got your answer, Narutogx2. Happy programming!