ID:139292
 
Code:
Book Template
obj/book
{
var/open = 0;
var/page[0];
var/Page/num;

verb/close()
{
set src in view(1);
if(open == 1)
{
icon_state = "close";
open = 0;
num = 0;
}
else
{
usr << "The [name] is closed.";
}

}

verb/previous()
{
set src in view(1);
if(open == 0)
{
usr << "The [name] is closed";
}
else if(num > 1)
{
page[num]--;
}
else
{
close();
}
}

verb/next()
{
set src in view(1);
if(open == 0)
{
usr << "The [name] is closed";
}
else if(num < page.len)
{
page[num]++;
}
else
{
close();
}
}
}

Page
{
var/num as num;

}


Problem description:
The above is a snippet from code I'm working on. It's meant to make a template book with all the basic verbs, so all I have to do is make a sub-object and fill in the blanks.

The problem I'm having is with the next() verb. As shown it will only flip one page then stop working.

I can make it flip through all the pages, but then the second part of the next() verb doesn't work. I also want it so that when you reach the last page it will close the book.

The previous verb also shown works fine. I added it to give an idea of what I'm looking for. (Reversed of coursed.)

I'm stumped so any ideas are much appreciated.
You should be incrementing the num variable, not Page[num].

Also, for some reason, you've defined the num variable to be of type Page, which does not make sense.

Also, having the Page type contain a variable for the page number is rather redundant.
In response to Garthor
Garthor wrote:
You should be incrementing the num variable, not Page[num].

Would that still go to the next item in the list?

Garthor wrote:
Also, for some reason, you've defined the num variable to be of type Page, which does not make sense.

Also, having the Page type contain a variable for the page number is rather redundant.

Yea, now that you say it, it does sound redundant. I made sense when I was writing it though.

I'll make some changes and let you know if it worked.

EDIT:
After some test I found that the code and some variations on it will in/decrease num or page[num] just fine. However I cannot get the index of page to increase more than once, so the next page won't show.
next() EDIT
    verb/next()
{
set src in view(1);
if(open == 1)
{
var/lastNum = num;
for(num, num == lastNum, num++)
{
page[num] = num;
usr << num;
}
if(num > page.len)
{
close();
}
}
else
{
usr << "The [name] is closed";
}
}

So I have been messing around with the code. This fixed the problem. If you know anyways to streamline the code more I'll take the advice, otherwise the problem is solved.
In response to Num1
If open is reserved to being only 0 and 1 (boolean), you could be doing...

if(open)


for if(open == 1)

and

if(!open)


for if(open == 0)
In response to Num1
Okay, that for() loop is completely pointless, as it will execute precisely once under every possible circumstance (except when num is absurdly large, in which case it'll execute an infinite number of times). You should just reduce the entire thing (inside the open==1 block) to:

if(++num > page.len)
{
close();
}


Anyway, it's still confusing as to what you are doing with the page list. The line page[num] = num seems to imply that you are intended for the list to be, literally, the list (1,2,3,4,5,...), which is rather pointless. What it really SHOULD be is a list of actual data, be it just text strings or references to datums. As it is, you have a book you can thumb through but no actual way to read anything from it.
In response to Garthor
As I said in the original post this is just a snippet, a part, of the code.

I have a Page datum that gets loaded into the page[] list. I have a working read() verb. just trying to work out the next() and previous() verbs.

It should stop after everyone so you can flip through the book. instead of flipping straight to the end, or displaying everything at once.

I get a "warning: operation has no effect here" message when I compile your code.
In response to LordAndrew
Nice catch. Thanks.
In response to Num1
Upon further testing there are some odd problems with this.
In response to Num1
Num1 wrote:
Upon further testing there are some odd problems with this.

They are likely the exact problems that I have pointed out. I would not consider them "odd", however.

It's also likely that your read() verb is broken.
In response to Garthor
The read() verb displays the information from the Page that is associated with the position in the list page[]. The problem is when the book gets to the end and close the page[] should reset which it does, I checked. But the display won't show the pages a second time.
In response to Num1
You are overriding all the values in the page list with nigh-meaningless numbers. You need to not do that, as I have already suggested.
In response to Garthor
Okay, but how do I change the index number?
num on its own is meaningless.
Tried adjusting page[num] and that did nothing.
Adjusting num and then setting it to page[num] works, but doesn't reset properly.

So just say that I'm doing it wrong doesn't help. Anything less then explaining how to properly adjust the list index is worthless. And a working example would help me a lot.

Otherwise no matter how good a programmer you are, your post are just spam.
In response to Num1
obj
book
var
open = FALSE
list/index
page = 1 //Declare the base /obj/book with parent variables

verb
Open()
src.open = TRUE
src << "You open the book."
Close()
src.open = FALSE
src.page = 1
src << "You close the book."
Next()
if(src.open)
if(++src.page <= src.index.len) //If 1+ to page is less than the uttermost limit of the book's page
src << "You turn the page."
else //If the conditional statement failed, close the book.
src.Close()
Previous()
if(src.open)
if(--src.page > 0)
src << "You turn the page."
else
src.Close()
ReadPage()
world << src.index[page]

Tome
index = list("Page 1","Page2") //Declare a child /obj/book with two items within its list


mob/Login()
src.contents.Add(new/obj/book/Tome)


Here's a working example. Feel free to chime in if you disagree with something I've written.
In response to Thief Jack
When I talk about the list index or the index of a list, I mean the number in the brackets of the list, i.e. 1st item, 2nd item, 3rd item, etc.

With that in thought I tried to slightly modify your code and use it, but it just increases the var page and not the index of the list.

This is what I'm having trouble understanding. In DM how is the index of a list, list[index], increased to access the different items of the list on command?

Since the code has been modified a couple times now, here it is again:
obj/book
{
var/open = FALSE;
var/page[];
var/Page/num;
var/by;

verb/openCover()
{
set src in view(1);
if(!src.open)
{
icon_state = "open";
open = TRUE;
src.num = 1;
}
else
{
usr << "The [name] is open.";
}
}

verb/close()
{
set src in view(1);
if(src.open)
{
icon_state = "close";
open = TRUE;
}
else
{
usr << "The [name] is closed.";
}

}

verb/previous()
{
set src in view(1);
if(src.open)
{
if(++src.num > 0)
{
usr << "You turn the page. You are now on page [src.num]."
}
else
{
close();
}
}
else
{
usr << "The [name] is closed";
}
}

verb/next()
{
set src in view(1);
if(src.open)
{
if(++src.num <= src.page.len)
{
usr << "You turn the page. You are now on page [src.num]."
}
else
{
close();
}
}
else
{
usr << "The [name] is closed";
}
}

verb/read()
{
set src in view(1);
if(src.open)
{
var/Page/p;
for(p in page);
{
p.Show();
return;
}
}
else
{
if(name)
{
usr << "[name]";
}
if(by)
{
usr << "[by]";
}
}
}
}

Page
{
var/num as num;
var/name as text;
var/cat as text;
var/msg as message;

proc/Show()
{
if(name)
{
usr << "[name]";
}
if(cat)
{
usr << "[cat]";
}
if(msg)
{
usr << "[msg]";
}
}
}

I've include more of the script incase it is as Garthor says, and is a problem else where.

I write my books in a separate script as this is an applied template. So here is a book to go along his it.
obj/book/test
{
name = "Test Book";
desc = "by Me";
icon = 'green_book.dmi';
icon_state = "close";

page = newlist(/Page/One,/Page/Two,/Page/Three,/Page/Four,/Page/Five,
/Page/Six,/Page/Seven,/Page/Eight,/Page/Nine,/Page/Ten);
}

Page/One
{
num = 1;
name = "";
cat = "";
msg = "This is page 1";
}

Page/Two
{
num = 2;
name = "";
cat = "";
msg = "This is page 2";
}

Page/Three
{
num = 3;
name = "";
cat = "";
msg = "This is page 3";
}

Page/Four
{
num = 4;
name = "";
cat = "";
msg = "This is page 4";
}

Page/Five
{
num = 5;
name = "";
cat = "";
msg = "This is page 5";
}

Page/Six
{
num = 6;
name = "";
cat = "";
msg = "This is page 6";
}

Page/Seven
{
num = 7;
name = "";
cat = "";
msg = "This is page 7";
}

Page/Eight
{
num = 8;
name = "";
cat = "";
msg = "This is page 8";
}

Page/Nine
{
num = 9;
name = "";
cat = "";
msg = "This is page 9";
}

Page/Ten
{
num = 10;
name = "";
cat = "";
msg = "This is page 10";
}

Ring now the num var for the pages is unused and so will probably be removed since there are better ways of displaying the page number.
In response to Num1
Somelist[x] accesses the x'th element of the list Somelist. Somelist[y] accesses the y'th element of the list Somelist. You do not need to modify the list in any way whatsoever in order to access a different element of it, you only need to place the desired index in the brackets.

So, if you have a variable called "num", and a list that, perhaps, is called "page", containing objects of type, say, /Page, and you want to access the /Page object at position num in the list page, then you would do:

var/Page/P = page[num]


Seeing as your read() verb is not using the index in any way, shape, or form, the issue is there, not with the verbs that turn the page.

Also: again I've noticed you are horribly abusing for() loops. I suggest you read the Reference to see how it's supposed to be used.