Keywords: view
(See the best response by Jp.)



icon = 'Taraval Icons.dmi'
icon_state = "Player"




if(M.client in view(10))
world << "Hello world!"

Problem description:

This has me pretty well stumped. What I intended for this code to do is output "Hello world!" whenever a player is in view(10), but I'm having issues with it outputting "Hello world!".

This particular code doesn't output anything at all even when you are in view, but I have messed around with it and the only results I have been able to get is it constantly spitting out "Hello world!", or nothing at all.

View is a pretty common proc I see used in tutorials and games, so I haven't a clue what I'm doing wrong here.

What exactly are you trying to do? The for() will only, if anything, just cause an infinite loop. Are you trying to make it so when someone enters within someone's view, they automatically get a message saying, 'Hello world!' ?
yes, I am creating an infinite loop (Although in my actual code, there is a few sleep procs.) With this code however, that's what I'm trying to do, display message saying "Hello world!" whenever someone enters Main_Character_Player's (which is a NPC.) view()

I'm open to try any sort of method to accomplish this if a infinite loop isn't how I should go about doing this.

It's not, especially since you aren't at all pausing during the infinite loop (which would cause it to crash). On top of that, it'll just keep telling them, 'Hello world!' over and over (which may be what you want to do). A better method would be modifying the /areas present within the view so that when something steps on them, they are told, 'Hello world!'. If you want them to keep getting that message, then you probably need to call a loop that keeps going until they're out of view.
I see you check for M.client but I don't see M instanced anywhere. It's defined as variable but it's never assigned anything, at least not in the code you posted.
Best response
Plus, clients don't have a position on the map - they won't ever be returned in a view() list.

A better method to accomplish what you want is, as Fizz noted, setting up a /area object around your NPC and having its Entered() proc trigger the output to any mobs with clients that have entered. The code for the area would look something like this:

.=..(m) // First do the default Entered(). It's probably nothing, but may as well be safe
if (ismob(m)) // m might be any atom/movable, not just a mob
if (m.client) // Check if it's a player
m.client << "Hello, world!"

If setting up areas around all the greeting mobs or objects is impractical, you could modify the Move() proc for clients to check for greeters:

var/message = "Hello, world!"

. = ..() // This time calling the default is very important
for(var/mob/greeter/g in viewers(mob)) // Loop through every greeter that can see the player mob
src << g.message // Output the message

Note that the second example has the player greeted every time they move near the greeter, not only on entering an area.
Thanks guys, I finally got around to having time to continue working on my project, and I found that I really like using areas, which are something I had never really used before!

I've changed a few of my systems in my project to areas, now that I see how useful (and easy!) they can be.