ID:836071
 
(See the best response by DarkCampainger.)
I'm trying to get the hang of lists but I still can't get how to access an individual item in a list.

mob/verb/list_add(mob/m)
if(m.ingroup==0)
friend1.Add(m)
m.ingroup=1
while(1)
var/mob/x=friend1[1]
var/mob/y=friend1[2]
if(src.loc==locate(12,6,1) && x in friend1 && y in friend1)
x.loc(locate(12,3,1))
y.loc(locate(11,9,1))
cancelfor=1
if(cancelfor==0 && x in friend1 && src.loc==locate(12,6,1))
x.loc=locate(12,3,1)
sleep(10)


alternatively i tried
friend1[2]=new(locate(11,9,1))
and that didn't work either.
Can you explain what is supposed to be happening here?

EDIT: Also, this isn't going to work:
x.loc(locate(12,3,1))
y.loc(locate(11,9,1))
I'm trying to make a team. If I am at a certain location, they will teleport to locations near me.

ingroup checks if the mob is alredy in the team
cancelfor makes sure that the next(last) if statement doesn't happen as there are 3 people on the team.
I don't see the issue here. You are grabbing the first item in friend1 and typecasting it so you can modify it. The only problem I see is that you are treating x.loc and y.loc as procs. Set them equal to locate().
But it doesn't work
Btw i made a mistake i did have loc = locate

mob/verb/list_add(mob/m)
if(m.ingroup==0)
friend1.Add(m)
m.ingroup=1
while(1)
var/mob/x=friend1[1]
var/mob/y=friend1[2]
if(src.loc==locate(12,6,1) && x in friend1 && y in friend1)
x.loc=locate(12,3,1)
y.loc=locate(11,9,1)
cancelfor=1
if(cancelfor==0 && x in friend1 && src.loc==locate(12,6,1))
x.loc=locate(12,3,1)
sleep(10)
I tried a more simpler code and it seems var/mob/y=friend1[2] is not the way to access a list
friend1[2] will grab the second item in friend1.
Here I'm trying a more simple list access. I created a variable and then created a verb adding someone to a list then making a variable accessing that item and finally equalling it to 1. But it gives me an error.

mob/var/vari
mob/verb/var_test(mob/m)
friend1.Add(m)
var/mob/a=friend1[1]
a.vari=1


runtime error: Cannot modify null.vari.
proc name: var test (/mob/verb/var_test)
usr: TheDarkChakra (/mob)
src: TheDarkChakra (/mob)
call stack:
TheDarkChakra (/mob): var test(TheDarkChakra (/mob))
Although it doesn't affect the problem very much, assuming x and y exist in this context, you're not considering order of operations. For example,
(a && b in c && d)
== (((a && b) in c) && d)

What you probably want is for (b in c) to work. For that, you need parentheses to force a higher priority.
(a && (b in c) && d)
Best response
Where are you initializing the friend1 list? Make sure you aren't giving it a starting size, or it will create null entries (and Add() will put the new entries after the null entries).

Regardless, you don't want to be accessing them by hard-coded indexes. You should be using a for-in loop. And you want to be calculating or marking the locations instead of hard-coding them.
DC I gave my list a starting size
In response to TheDarkChakra (#10)
A starting size of 0?
Of 2
As DC explained, when you initialize a list with a starting size, it ends up being list(null,null). Meaning, adding something to the list makes it list(null,null,thing), making thing == list[3]. Instead, you can either not initialize with 2, or set the list indices with list[1] = thing.
I thought that gives a list max size
use list.len for checking what size your list is (so you dont have to set a max size you can simply check it)

since you don't set a max size null,null is not in your list anymore and you can use list[1] or list[2] like they explained
Okay that simple list worked. But more advanced doesn't:

mob/verb/list_add(mob/m)
if(m.ingroup==0)
friend1.Add(m)
m.ingroup=1
while(1)
var/mob/x=friend1[1]
var/mob/y=friend1[2]
if(src.loc==locate(12,6,1) && x in friend1 && y in friend1)
x.loc=locate(12,3,1)
y.loc=locate(11,9,1)
cancelfor=1
else if(cancelfor==0 && x in friend1 && src.loc==locate(12,6,1))
x.loc=locate(12,3,1)
sleep(10)
So try looking at this:

for(var/x in friend1)
world << "The item [x] is [friend1[x]]"


This will list out each key/value in friend1.
You're doing a lot more work than you need to. If you're grabbing something from a list, you don't need to check if it's in the list.

If you're lining up your allies in a specific position, you can use a procedure or a list to position them in a certain pattern.
In response to Pirion (#17)
I was also about to mention that, but it's irrelevant because he's doing different things to each mob. Of course, you could also do this:
var positions[] = list(
locate(12,3,1),
locate(11,9,1))
for(var/n in 1 to friends.len)
var mob/ally = friends[n]
ally.loc = positions[n]
Page: 1 2