ID:1558432
 
(See the best response by Multiverse7.)
Problem description:Okay after being out of all of BYOND for months, I have become rusty at DM. I wanted to create a turn based game like final fantasy and pokemon but not publish it. I haven't mastered lists yet. I looked through the forums for the solutions but failed to find them. Here I get either cannot read from list or cannot modify /mob/ally/magician.loc errors. You see I want to have a party of about 7 members where you put up and can switch 3 members with any of the remaining 4. I keep getting errors.

Here are the codes:

turf
Grass
icon='turf.dmi'
Entered(mob/player/m)
if(m in m.allies)
if(prob(3) && m.partyleader==TRUE && ismob(m))
m.prevloc=m.loc // remember previous location if you were to choose run
if(prob(50))
for(var/mob/mo in m.myparty)
m.inbattle=1
if(m.allies.len>0)//makes sure there is someone in the party greater than the number written so it won't give an error
var/mob/x=m.allies[1]
x.loc=locate(12,6,1)
x.inbattle=1
var/mob/x1=x.myparty[1] //error here or...
x1.loc=locate(x.x, x.y+1, x.z)//...error here or...
var/mob/x2=x.myparty[2]
x2.loc=locate(x.x, x.y-1, x.z)


etc...

mob/
var
list
allies[0]
enemies[0]
totallist[0]
listabilities=list("Fire")
myparty


mob/player
Login()
src.icon = 'player.dmi'
src.loc=locate(2,3,1)
src.step_size = 8
src.myparty=list(/mob/ally/magician,/mob/ally/melee_character,/mob/ally/projectiler,/mob/ally/piercing,/mob/ally/paladin)//...error here or...
for(var/mob/m in src.myparty)
m.inaparty=src
src.inaparty=src
world << src.myparty[1]//...error here
battleinput()
You have to initialize memory for a list by declaring it's size and datatype.

Both of the following are equally correct:
mob/var/list/myParty=list()


mob/var/myParty[]


As per your error. You are trying to read a loc, a variable associated with an atom/movable from a typepath. You'll have to refer to the actual mob of magician, instead of just it's type.
Here I changed these:

mob/player
Login()
src.icon = 'player.dmi'
src.loc=locate(2,3,1)
src.step_size = 8
src.myparty.Add(new/mob/ally/magician,new/mob/ally/melee_character,new/mob/ally/projectiler,new/mob/ally/piercing,new/mob/ally/paladin)//changed
for(var/mob/m in src.myparty)
m.inaparty=src
src.inaparty=src
world << src.myparty[1]
battleinput()


And these:

mob/
var
list
allies[0]
enemies[0]
totallist[0]
listabilities=list("Fire")
myparty[0]//changed
contents=newlist(/obj/potion)


Now it gives me the error saying list index out of bounds.
You're creating a list with an index of zero. Arrays in DM must have an index of one or higher to have any values set.

mob/var/myparty[]//Creates an empty list, equivalent to calling mob/var/list/myparty=list().


Or, if you're interested in a fixed size, you can initialize it with a preset index.

mob/var/myparty[1000]//Creates a list with an index of 1000.


The "list index out of bounds" error means you are trying to access a list item at an index that doesn't exist. This makes sense, because it appears you are initializing the myparty[] list with an index of 0. The problem is at this line:
world << src.myparty[1]

If you want to use a list with a dynamic index that changes at runtime, you will need to make sure that an index exists before you try to access it. So, you can use a conditional check to resolve the error:
if(length(src.myparty) >= 1)
world << src.myparty[1]

Does that eliminate the error?
I now initialized my list with mob/var/list/myparty=list() and at login added
src.myparty.Add(new/mob/ally/magician,new/mob/ally/melee_character,new/mob/ally/projectiler,new/mob/ally/piercing,new/mob/ally/paladin)
. It still gives me errors.
I think newlist() would be a much better choice in this case, so try it like this instead:
src.myparty += newlist(/mob/ally/magician, /mob/ally/melee_character, /mob/ally/projectiler, /mob/ally/piercing, /mob/ally/paladin)


Also, it could be that one or more of those paths are not valid, so make sure that they are all defined correctly.
newlist didn't work
In response to TheDarkChakra
TheDarkChakra wrote:
newlist didn't work

That's very strange. What kind of errors are you getting exactly?
In response to Multiverse7
Multiverse7 wrote:
TheDarkChakra wrote:
newlist didn't work

That's very strange. What kind of errors are you getting exactly?

Index list out of bounds. It points to this code:
                            var/mob/x1=x.myparty[1]
Is x1 being initialized before or after you add the mobs to the list? That should obviously only be done after the list has something in it.

Optionally, you can separate the initialization from the value you are setting it to:
var/mob/x1// This can be anywhere.

// This will patch up the error,
// but this part really needs to happen after you add something to the list,
// otherwise nothing will happen:
if(length(src.myparty) >= 1)
x1 = x.myparty[1]

I hope this makes sense.
I have been testing things out. It is at login that the list gets added to myparty list. But it doesn't get added. I made a debug verb that asks for the length of the list and it shows the list is empty even with src.myparty+=newlist(path, path 2, etc...) I then did
src.myparty+=list(/mob/ally/magician,/mob/ally/melee_character,/mob/ally/projectiler,/mob/ally/piercing,/mob/ally/paladin)
and it did say the list is full but when I test the game it gives me the error that the /mob/ally/magician.loc cannot be modified. For some reason I can't add new mobs to the list. Help?
Best response
What you are doing there is adding a list of type paths, not actual mobs. What newlist() does is take the types you give it, create new objects of those types and return a list of them.

Obviously a type path cannot have a loc, because it's just a type path. It's not an actual object instance.

It's hard to think of a reason why newlist() wouldn't work in this case. When you used newlist(), did it generate any errors at compile time or runtime?

If all of those paths are valid, the only thing that would make sense is if the mobs were being garbage collected, but for that to happen the myparty list would have to be cleared somewhere, which doesn't seem to be the case.

Are you overriding the New() proc somewhere for /mob or /mob/ally?

As a test, just try to add one object to the list and see what happens:
src.myparty += new /mob
if(!length(src.myparty))
world << "Nothing was added to the list!"
// Under any normal conditions, the object will be added successfully.


If you don't get the message then that at least narrows down the problem a bit.
mob/ally
icon='magician.dmi'
melee_character
magician
projectiler
piercing
paladin
New()
battleinput()


I did make a verb with that code you wrote to add to the mob and nothing happened so something was added to the list.

EDIT: I added to spawn() under New() and it looks like the code works. Looks like now I will have to move on to other problems outside this thread.