ID:149876
 
Im having some trouble with lists, mainly that when I add something to an empty list its not adding it. This is the problem code:
world

New()
..()
sleep(1)
var/savefile/F = new("lists.sav")
Immortals = new()
F["immortals"] >> Immortals
if(length(Immortals)==0)
world << "Immortals created"
Immortals += HEAD_ADMIN
Immortals += "DummyAdmin"
if(HEAD_ADMIN in Immortals) world << "[HEAD_ADMIN] added to Immortals"
else world << "[HEAD_ADMIN] NOT added to Immortals"

if("DummyAdmin" in Immortals) world << "DummyAdmin added to Immortals"
else world << "DummyAdmin NOT added to Immortals"
Immortals["[HEAD_ADMIN]"] =4
Immortals["DummyAdmin"] =4
F["immortals"] << Immortals

else
world << "Immortals exists, and contains : "
for(var/a in Immortals)
world << "\t [a] (Immortals)"
world << "\t [Immortals[a]] (Immortals)"



It generates the following output :

Alathon isnt in Immortals
runtime error: bad index
proc name: New (/world/New)
source file: Immortal_System.dm,90
usr: null
src:
call stack:
: New()
Immortals created
Alathon NOT added to Immortals
DummyAdmin NOT added to Immortals

The only way I see this happening is if it never actually adds anything to the Immortals list, but I don't see how it wouldn't? I get the feeling it has something to do with this line (F["immortals"] >> Immortals) but Im not sure if its changing the list to a string or something else somehow.

Thanks,
Alathon

Edit : By the by, line 90 is Immortals["[HEAD_ADMIN]"] =4
and yes I know why it generates that runtime error(Incase someone is thinking I am asking why its giving that error)
I am not completely sure, but the fuzzy line looked to me like it could be:

Immortals = new()

Maybe:

var/list/Immortals = list()

would work better?

-Lord of Water, trying to be helpful
In response to Lord of Water
Lord of Water wrote:
I am not completely sure, but the fuzzy line looked to me like it could be:

Immortals = new()

Maybe:

var/list/Immortals = list()

would work better?

-Lord of Water, trying to be helpful

Actually it should be newlist() look it up
In response to Lord of Water
Lord of Water wrote:
I am not completely sure, but the fuzzy line looked to me like it could be:

Immortals = new()

Maybe:

var/list/Immortals = list()

would work better?

-Lord of Water, trying to be helpful

Immortals is previously declared as var/list/Immortals

Alathon
In response to Super16
Actually, the = new() should still work the way he used it. That gives it a new copy of it's type, which is list.

The problem could be in how you save the list. If F["immortals"] is not a list then it could change the type of Immortals which could mess everything up.

I don't see any problems in it as I glanced through it so it might be helpful if we could see the relevant save code as well.

here are some test statements that might help you diagnose the problem.

New()
..()
sleep(1)
var/savefile/F = new("lists.sav")
Immortals = new()
F["immortals"] >> Immortals
world << "Immortals: [Immortals]"
world << "Length: [Immortals.len]"
In response to English
The idea is this : It loads immortals.sav["immortals"] into the Immortals list. If this is empty, Immortals length would still be 0, so in that case go and add stuff to the list and save it to immortals.sav. If it did exist, Immortals would have one or more elements in it, and those would then be displayed.

Again, im not sure what it does to a list if you do something like F["immortals"] >> list if F["immortals"] isnt anything.

Alathon

P.S: It works fine if I do Immortals += list(HEAD_ADMIN=4) instead of Immortals += HEAD_ADMIN, but I can't do that for a few reasons.
In response to Alathon
I just tested it out and it appears that if there is nothing stored in F["immortals"] it will delete the list your trying to put it into. Basically you have to put in a test like this AFTER you try to store the data from the file into Immortals:

if(!Immortals)
Immortals = new()

You can't just test the len of it because the list itself might not exist.
Alathon wrote:
Edit : By the by, line 90 is Immortals["[HEAD_ADMIN]"] =4
and yes I know why it generates that runtime error(Incase someone is thinking I am asking why its giving that error)

Perhaps the notation in line 90 is what's giving it a headache?

Try the following and see if it helps. If that doesn't work, I'll try to look at it in more detail when I'm not so tired. :)

var/HEAD_NAME = "[HEAD_ADMIN]"
Immortals[HEAD_NAME] =4
In response to English
English wrote:
I just tested it out and it appears that if there is nothing stored in F["immortals"] it will delete the list your trying to put it into. Basically you have to put in a test like this AFTER you try to store the data from the file into Immortals:

if(!Immortals)
Immortals = new()

You can't just test the len of it because the list itself might not exist.

Ah, yeah it was as I suspected. Thanks :)

Alathon
In response to Super16
Actually it should be newlist() look it up

I'd suggest you go look it up too. =)

newlist() creates a list of new objects. Eg, newlist(/obj/myobj1, /obj/myobj2, /obj/myobj3) will create a list containing three actual objects.

list() creates a list with actual data. Eg, list(/obj/myobj1, /obj/myobj2, /obj/myobj3) will create a list with those three types (*NOT* actual objects) in it.

newlist() and list() function identically without arguments, but don't use them interchangeably, because they serve very different purposes!