ID:155752
 
I'm attempting to add all mob's names in view of a player to a list but the only way I can think to do this would force the player to select which mobs to add to the list.

How do i make it add ALL mobs without distinction.


//list name is L

test5(mob/M in oview(10))
L += M.name
This should work.
var/list/L = list()
proc
test5()
for(var/mob/M in oview(10))
L += M.name
In response to Asielen
There is a bug in this code, though it is somewhat subtle.

oview() defaults to centreing on 'usr'. In a procedure, use of usr is bad practice.

The other problem is the use of a global list for return values.

A better proc:

proc/mobs_in_view(atom/centre)
. = list()
for(var/mob/M in oview(centre)) . += M.name
In response to Jp
> proc/mobs_in_view(atom/centre)
> . = list()
> for(var/mob/M in oview(centre)) . += M.name
>


I don't really understand this code. What does adom refer to and centre? How do I access the contents of this list i don't even see where its being stored.

It being a procedure, is that what makes it not prompt for a mob selection? I modified the code by changing . to something else and accessed it like a normal list but I get a "list index out of bounds" error but I think this is because im trying to access slots in the list that haven't been taken up. so it seems to work but I still don't understand why.

Thanks.
In response to Lilcloudy1
I wrote some of it using a little shorthand. This version is functionally identical and maybe a little clearer:

proc/mobs_in_view(var/atom/centre)
var/list/ret = list()
for(var/mob/m in oview(centre)) ret.Add(M.name)

return ret


The bit in the parentheses is a variable declaration - I'm saying that this procedure takes one argument, of type /atom, named 'centre'. /atom is the base type for everything that can go on the map - so turfs, areas, mobs, and objs.

It doesn't prompt for a mob partially because it's a procedure (and therefore not directly user-accessible), and partially because it doesn't ask for it.

'.' is a special variable that all procedures have - I probably shouldn't have used it in an example, it's just shorthand. The list is returned using 'return' like procedures as usual.

You can use the proc like this:

mob/verb/test_mobs_in_view(mob/m in world)
var/list/names_in_view = mobs_in_view(m)
for(var/name in names_in_view)
world << name


That verb lets you pick a mob in the world, calls the procedure on it and stores the returned value in a list named 'names_in_view', and then loops through the list outputting all the names in it.
In response to Jp
Thank you for this and explaining. I did eventually figure out a way to make it run but it's incredibly sloppy compared to yours. Very much appreciated.

Jp wrote:
I wrote some of it using a little shorthand. This version is functionally identical and maybe a little clearer:

> proc/mobs_in_view(var/atom/centre)
> var/list/ret = list()
> for(var/mob/m in oview(centre)) ret.Add(M.name)
>
> return ret
>

The bit in the parentheses is a variable declaration - I'm saying that this procedure takes one argument, of type /atom, named 'centre'. /atom is the base type for everything that can go on the map - so turfs, areas, mobs, and objs.

It doesn't prompt for a mob partially because it's a procedure (and therefore not directly user-accessible), and partially because it doesn't ask for it.

'.' is a special variable that all procedures have - I probably shouldn't have used it in an example, it's just shorthand. The list is returned using 'return' like procedures as usual.

You can use the proc like this:

> mob/verb/test_mobs_in_view(mob/m in world)
> var/list/names_in_view = mobs_in_view(m)
> for(var/name in names_in_view)
> world << name
>

That verb lets you pick a mob in the world, calls the procedure on it and stores the returned value in a list named 'names_in_view', and then loops through the list outputting all the names in it.