ID:831187
 
(See the best response by Kaiochao.)
Code:
mob
verb
direction()
set src in view()
var/a = get_dir(src,/turf/water)
usr << "[a]"


Problem description:
the code is my attempt to try to understand the get_dir statement. from my understanding of get_dir is that it tells me the direction of the first loc to the second loc. so here i have src in the first loc and water turf in the second loc. what i am trying to do is get some output when mob is walking towards the water. however, all that is outputted is "0"

    verb
direction()
set src in view()
var/a = get_dist(src,/turf/water)
usr << "[a]"


the get_dist statement is returning a value of 127 even though the mob is standing next to the water tile
/turf/water is just a type-path. You need an actual reference to a /turf/water in order for it to work.

One way is:

mob
verb
getDir()
var/d = get_dir(src, locate(/turf/water))

src << d


Though this isn't good if you have more than one /turf/water.
Best response
/turf/water is a path. It's not an object that physically exists. You need to find a specific instance of water.

If I knew what you were trying to do, I could give you an example.

...For example: (here's an example of an example I could give you if you were more specific!)
mob
verb
find_water()
var turf/water/w = locate() in view(world.view + 5, src)
if(!w)
src << "There's no water in sight!"
else
var dir_to = get_dir(src, w)
src << "There's water to the [dir2text(dir_to)]!"

var dir_text[] = list(
"north", // 1
"south", , // 2
"east", // 4
"northeast", // 5
"southeast", , // 6
"west", // 8
"northwest", // 9
"southwest" // 10
)
// Returns a text form of a given BYOND-direction
proc/dir2text(dir)
if(!isnum(dir) || dir < 1 || dir > 10 || dir == 3 || dir == 7)
CRASH("Invalid dir: [dir]")
return dir_text[dir]
Kalster wrote:
so here i have src in the first loc and water turf in the second loc

Not quite. Your second argument is not a reference to a water turf, but rather a type path. To get the first instance of a type path, you can use a variation of locate():
var/turf/water/W = locate(/turf/water) in world

locate() will also implicitly look for the type of the variable you're assigning it to, so often you'll see the above written like this:
var/turf/water/W = locate() in world


<edit>
Wow, double-ninja'd
i have more than one turf with water and now i am getting some strange readings.

mob
verb
direction()
set src in view(2)
var/turf/sand/w = locate(/turf/sand) in world
var/a = get_dir(src,w)
usr << "[a]"


hi Kaiochao. to answer your question, i am only studying the statements. i am not trying to do anything in particular.

i am getting some strange outputs when there is more than one water turf in view(2). like you said, i need to make the water in question a separate instance. thank you
/mob/proc/closest_water()
var/nearest
for(var/turf/water/W)
if(get_dist(src, W) < get_dist(src, nearest) || !nearest)
nearest = W
if(nearest)
return "The closest water turf to you is [get_dist(src, nearest)] tiles away."
return "There doesn't seem to be any water in this world!"

/mob/verb/find_water()
src << closest_water()
i seem to be having learning problems with the get... statements.

pressing f1 on get_step_away, in the documentation it says... Max: The maximum distance between Ref and Targ before movement halts.

but in this code, the movement does not halt when mob is two tiles away for target. the if statement for the get_step_away seems to work regardless of the value set in target. the code outputs the mob name even when the mob is five tiles away.

mob
verb
look()
for(var/mob/m in oview())
if(get_step_away(src,m,2))
usr << "[m]"