ID:146792
 
Code:
obj/books
name="Book

notebooks
icon_state="notebook"
name="Notebook"
var/entries[]
var/entrynum=1


verb/Name_Book()
var/bookname=input(usr, "What do you want to title this notebook?", "Name Book", src.name) as text
src.name=bookname
usr<<"You Title this notebook, '[src.name]'."

verb/Write_in_Book()
//Ask if they want to add a new Entry first
var/newentry=input(usr, "Add a new entry.","Write in [src.name]") as text
entries[entrynum] = "Entry #[entrynum]: [newentry]"
++entrynum


verb/Read_Book()
usr<<"[entries]"


Problem description:
I'm making books that people can add entries to, basically just lists. Any idea why the above code won't work? I was reading about Lists and this seems ok
Error messages (if there are any) and detailed problem description, please? =)

Since you haven't said what the problem is, I'll make a few educated guesses...

Firstly, your list variable is defined but not initialised. In other words, you've indicated that it's meant to hold a list (using the [square brackets]), but you haven't set it to anything, so it has the value null.

The two best ways of defining lists are:

var/entries[0] // Note the 0!
var/list/entries=list()


Those two lines are equivalent. I prefer the latter, as it's more explicit; but either one works. Pick your poison.

Secondly, you're trying to set an index that hasn't been set before... IIRC, that doesn't work for non-associative lists. Instead, try this:

//Ask if they want to add a new Entry first
var/newentry=input(usr, "Add a new entry.","Write in [src.name]") as text
entries.Add("Entry #[length(entries)]: [newentry]")


Then you don't need the entrynum var; you can use it if you really want, but it's more robust to simply check the length of the list directly, like this: length(entries)

Thirdly, about Read_Book(); outputting a list just results in "/list", which is slightly annoying. =\ To fix that, loop through the list and display each item in it:

obj/books/verb/Read_Book()
// May or may not be necessary, I don't rememeber if there's a default "set src" for obj verbs
// You could first try it without the following line and see if the verb shows up in the
// Commands panel, if you're interested in finding out.
set src in usr
// Here's the loop I mentioned
for (var/X in entries)
usr << X
In response to Crispy
Sry, it was late.
I had a Stack error, it just wouldnt finish running and call the stack error.
In response to karver
Stack error? I think you're getting confused... all error messages have a "call stack", but that doesn't have much to do with the error message itself.

Errors with stacks are entirely possible, but that's a more abstract problem... a computer would be more likely to report an "Overflow" or "Out of memory" error, or possibly just get weird data-related problems because you accessed bits of memory that you didn't mean to access (but that last would happen more in C; not in BYOND).

Anyway, I hope my post helped!
In response to Crispy
Could you post the exact runtime error message? it would help a lot