ID:2259530
 
(See the best response by Kozuma3.)
Code:


Problem description:
I would like to put actual use to my bookshelves icons by making them a click system that allows you to write a new book or read ones other people have written.

All bookshelves can be linked or not that part can go either way, but if anyone has a little knowledge they can spend on me. I'd appreciate the help.

This way people can come up and actually write books into the library and people can also come and read them. I just feel like that would be revolutionary type awesome because sometimes its the little things that make a game feel more complete.

If anyone has coding for this sort of thing let me know.

I appreciate the help.
Best response
Tested, just needs to be given an icon.

var save_path = "worldsave.sav"
var list/books = list("Test Book" = "This is the contents of the book.")

obj/Bookcase
Click()
var mob/m = usr
var chosen
var information
switch(input(m,"What would you like to do?") in list("Cancel","Read","Write"))
if("Read")
if(global.books.len == 0)
src << "The bookcase is empty."
return
chosen = input(m,"Which book?","Select a Book") in global.books
information = global.books[chosen]
// chosen = Title of the Book
// information = The contents of the book/text.
src << "Reading <u>[chosen]</u>."
src << information
if("Write")
chosen = input(m,"Title?","Title") as text
if(chosen && length(chosen) > 3)
information = input(m,"The title is [chosen].","Contents") as message
if(information && length(information) > 23)
global.books[chosen] = information

world/New()
..()
global.books = new
if(fexists(save_path))
var savefile/s = new(save_path)
s["books"] >> global.books
world/Del()
var savefile/s = new(save_path)
s["books"] << global.books
..()
In response to Kozuma3
Why are you giving 'books' a default value? You're wiping it out in world/New() anyways. You should also definitely have cancel buttons on your input()s.

You also don't need the 'm' variable at all 'usr' is perfectly acceptable in this case.
In your professional opinion. Where should i slide the icon in. At the top or near the bottom?

Does it matter?
So this code erases old books when a new one is being added is what you're saying?
No, but it's probably not a good idea to be using code that you don't understand, which is why we usually don't just give people the code and say 'good luck!' -- it does nothing to teach people.
I learn in a great way from an example that i know for sure is a proper working system. It's more than just copy and paste with me. I'm known for studying something i don't understand.
In response to Kozuma3
Thanks man i appreciate your help. You saved me bro :D
    BookShelves
icon='bookshelf.dmi'
One
icon_state="bookshelf"
density=1
layer=TURF_LAYER+1
Two
icon_state="bookshelf1"
density=1
layer=TURF_LAYER+1
Three
icon_state="bookshelf2"
density=1
layer=TURF_LAYER+1
Four
icon_state="bookshelf3"
density=1
layer=TURF_LAYER+1
Five
icon_state="bookshelf4"
density=1
layer=TURF_LAYER+1


These are my current Bookshelf settings.
I've spent the last hour or so trying to figure out how to fuse what you've shown me with it.

Can you Give me a hand?
In response to LiquidWhip
obj
BookShelves
Click()
// code here
BookShelf1
BookShelf2
BookShelf3
In response to Nadrew
Nadrew wrote:
Why are you giving 'books' a default value? You're wiping it out in world/New() anyways. You should also definitely have cancel buttons on your input()s.

If you remove "hud = new" in world/New() then the default book will exist if no savefile is found to be saved later, this can be used to build in default books.
In response to Nadrew
Also I overlooked something, the modified code below under "Write" will now check to see if the book already exists with the given title, if so it'll ask again for another title. They have 3 trys.

            if("Write")
var trys = 3
while(!chosen && trys)
chosen = input(m,"Title?","Title") as text
if(chosen in global.books)
chosen = null
trys --
if(chosen && length(chosen) > 3)
information = input(m,"The title is [chosen].","Contents") as message
if(information && length(information) > 23)
global.books[chosen] = information
This is what i've turned up with
    obj/BookShelves
Click()
var save_path = "worldsave.sav"
var list/books = list("Test Book" = "This is the contents of the book.")
var mob/m = usr
var chosen
var information
switch(input(m,"What would you like to do?") in list("Cancel","Read","Write"))
if("Read")
if(global.books.len == 0)
src << "The bookcase is empty."
return
chosen = input(m,"Which book?","Select a Book") in global.books
information = global.books[chosen]
// chosen = Title of the Book
// information = The contents of the book/text.
src << "Reading <u>[chosen]</u>."
src << information
if("Write")
chosen = input(m,"Title?","Title") as text
if(chosen && length(chosen) > 3)
information = input(m,"The title is [chosen].","Contents") as message
if(information && length(information) > 23)
global.books[chosen] = information


It's telling me this though.

media\code\turf\Turf.dm:3714:error: global.books.len: undefined var
media\code\turf\Turf.dm:3717:error: global.books: undefined var
media\code\turf\Turf.dm:3718:error: global.books: undefined var
media\code\turf\Turf.dm:3728:error: global.books: undefined var
media\code\turf\Turf.dm:3716:error: : missing expression
media\code\turf\Turf.dm:3707:warning: save_path: variable defined but not used
media\code\turf\Turf.dm:3708:warning: books: variable defined but not used

I haven't gotten to plugging in the last part yet because of these five errors.
You're missing the global variables 'var list/books = new' & 'var save_path = "worldsave.sav"'

They shouldn't exist under the object.
obj/BookShelves
Click()
'var list/books = new' & 'var save_path = "worldsave.sav"'
var save_path = "worldsave.sav"
var list/books = list("Test Book" = "This is the contents of the book.")
var mob/m = usr
var chosen
var information
switch(input(m,"What would you like to do?") in list("Cancel","Read","Write"))
if("Read")
if(global.books.len == 0)
src << "The bookcase is empty."
return
chosen = input(m,"Which book?","Select a Book") in global.books
information = global.books[chosen]
// chosen = Title of the Book
// information = The contents of the book/text.
src << "Reading <u>[chosen]</u>."
src << information
if("Write")
chosen = input(m,"Title?","Title") as text
if(chosen && length(chosen) > 3)
information = input(m,"The title is [chosen].","Contents") as message
if(information && length(information) > 23)
global.books[chosen] = information



It now reads
media\code\turf\Turf.dm:3707:error: 'var list/books = new': cannot find file
media\code\turf\Turf.dm:3707:error: 'var save_path = "worldsave.sav"': cannot find file
media\code\turf\Turf.dm:3715:error: global.books.len: undefined var
media\code\turf\Turf.dm:3718:error: global.books: undefined var
media\code\turf\Turf.dm:3719:error: global.books: undefined var
media\code\turf\Turf.dm:3729:error: global.books: undefined var
media\code\turf\Turf.dm:3707:warning: &: statement has no effect
media\code\turf\Turf.dm:3717:error: : missing expression
media\code\turf\Turf.dm:3708:warning: save_path: variable defined but not used
media\code\turf\Turf.dm:3709:warning: books: variable defined but not used

Does this mean i need to create a DM file for Books?
Above the object itself you need to define the below.

You need to do this to define them as global variables, defining them under the object will not make them global.

var list/books = new
var save_path = "worldsaves.sav"

obj/Bookcase
// ...
var list/books = new
var save_path = "worldsaves.sav"
obj/
BookShelves
Click()
var list/books = list("Test Book" = "This is the contents of the book.")
var mob/m = usr
var chosen
var information
switch(input(m,"What would you like to do?") in list("Cancel","Read","Write"))
if("Read")
if(global.books.len == 0)
src << "The bookcase is empty."
return
chosen = input(m,"Which book?","Select a Book") in global.books
information = global.books[chosen]
// chosen = Title of the Book
// information = The contents of the book/text.
src << "Reading <u>[chosen]</u>."
src << information
if("Write")
chosen = input(m,"Title?","Title") as text
if(chosen && length(chosen) > 3)
information = input(m,"The title is [chosen].","Contents") as message
if(information && length(information) > 23)
global.books[chosen] = information


I only got one warning and that was a
media\code\turf\Turf.dm:3710:warning: books: variable defined but not used

I'm assuming that's because i haven't input the rest of it in yet. I don't know how i should do the indentation for that. I get a max count error everytime i try to put the bottom portion of the code in
Ah i understand what you mean now by it won't make them global. Do i need to take out the variable list/books that i have listed under obj in the code above?
This is what we have so far

var list/books = new
var save_path = "worldsaves.sav"
obj/
BookShelves
Click()
var list/books = list("Test Book" = "This is the contents of the book.")
var mob/m = usr
var chosen
var information
switch(input(m,"What would you like to do?") in list("Cancel","Read","Write"))
if("Read")
if(global.books.len == 0)
src << "The bookcase is empty."
return
chosen = input(m,"Which book?","Select a Book") in global.books
information = global.books[chosen]
// chosen = Title of the Book
// information = The contents of the book/text.
src << "Reading <u>[chosen]</u>."
src << information
if("Write")
chosen = input(m,"Title?","Title") as text
if(chosen && length(chosen) > 3)
information = input(m,"The title is [chosen].","Contents") as message
if(information && length(information) > 23)
global.books[chosen] = information
icon='bookshelf.dmi'
One
icon_state="bookshelf"
density=1
layer=TURF_LAYER+1
Two
icon_state="bookshelf1"
density=1
layer=TURF_LAYER+1
Three
icon_state="bookshelf2"
density=1
layer=TURF_LAYER+1
Four
icon_state="bookshelf3"
density=1
layer=TURF_LAYER+1
Five
icon_state="bookshelf4"
density=1
layer=TURF_LAYER+1
var list/books should be removed from the object and kept global.

Also, you should scroll up and use the new code in "write" so others can't remake existing books.
Page: 1 2 3