ID:138850
 
Code:
            New() // New Proc 
..() // Biz as usual
src.AI() // Begin AI

AI() // AI Code
var/obj/Gold/T // Define T
var/X = 100 // Define X to be 100
for(var/obj/Gold/A in world) // Loop through gold in world
if(X > get_dist(src,A)) // If it's closer than the current X value (Changes with target)
T = A // Make Target that gold mine
X = get_dist(src,A) // Define X as the distance
if(isnull(T)) // If T is null
world << "[src] was unable to find a target!" // Debug
else // If T is not null
src.Target = T // Define Target to be that gold mine
world << "[src] has targetted the [src.Target]" // Debug
walk_towards(src, src.Target, 0, src.MoveDelay) // Walk to it
world << "[src] is walking"
return


Problem description:
I have 3 rocks on the map, it should be able to find at least one target, but target is always null. Any ideas on why?
Lugia319 wrote:
Code:
>           New() // New Proc
> ..() // Biz as usual
> src.AI() // Begin AI
>
> AI() // AI Code
> var/obj/Gold/T // Define T
> var/X = 100 // Define X to be 100
> for(var/obj/Gold/A in world) // Loop through gold in world
> if(X > get_dist(src,A)) // If it's closer than the current X value (Changes with target)
> T = A // Make Target that gold mine
> X = get_dist(src,A) // Define X as the distance
> if(isnull(T)) // If T is null
> world << "[src] was unable to find a target!" // Debug
> else // If T is not null
> src.Target = T // Define Target to be that gold mine
> world << "[src] has targetted the [src.Target]" // Debug
> walk_towards(src, src.Target, 0, src.MoveDelay) // Walk to it
> world << "[src] is walking"
> return
>

Problem description:
I have 3 rocks on the map, it should be able to find at least one target, but target is always null. Any ideas on why?

Doesn't for() choose the closest target by default?
In response to Kumorii
    AI() // AI Code
var/X = 100 // Define X to be 100
for(var/obj/Gold/A in world) // Loop through gold in world
if(X > get_dist(src,A)) // If it's closer than the current X value (Changes with target)
X = get_dist(src,A) // Define X as the distance
if(!A) // If T is null
world <<output("[src] was unable to find a target!","chat") // Debug
else // If T is not null
if(A) // Define Target to be that gold mine
world<<output("[src] has targetted the [A]","chat") // Debug
walk_towards(src, A, 0, 3) // Walk to it
world << "[src] is walking"
return



this didnt give me null and the player walked to the gold hope this is what u wanted
Yeah I can't see anything immediately wrong with it.

I assume already that the gold is within 100 steps of the AI as well.

Try just after the <t>for</t> loop finding gold/A, just having another debug message announcing it has found something, and if it has then the issue is with the algorithm (but again I don't see anything wrong with it), and otherwise its just flat out not finding gold.
In response to Trickykitty
Turns out my original code was good, yours is wrong. Yours isn't wrong, but there are some unnecessary bits. Turns out the problem was in the New() procedure for the Miner.
In response to Kumorii
You're probably thinking of looping through view()-type procs, which return a list of atoms in order of distance. A for(x in list) statement simply loops from the beginning of a list.

Also, just throwing this out there.
var obj/Gold/T
for(T in range(100, src)) break

if(T)
Target = T
walk_to(src, Target, 0, MoveDelay)