ID:2152398
 
(See the best response by Kaiochao.)

Hi, I'm making trying to do a basic code to use a chest for storage... I would also like to limit the storage to 28 slots which I don't know how to do.
Code:

obj/Chest/var/Useable


obj/Chest/verb
Open()
set src in oview(1)
icon_state = "Open"
Useable = 1 //When the chest is open you can use it.
Close()
set src in oview (1)
icon_state = "Closed"
Useable = 0 //When the chest is closed you can't use it

//Close, it compiles but has a stack error when I try to store.)

Store()
set src in view(1)
if (src.Useable != 0) //if the chest Useable !=0 the chest is open and can be used.
Move(/obj in usr.contents to src.contents)
usr << "You put your stuff into the chest"
else
usr << "The Chest is closed."

Withdraw()
set src in view(1)
if (src.Useable != 0)
Move(/obj in src.contents to usr.contents)
usr << "You withdraw your items from the chest."
else
usr << "The chest is closed."


This code is compiled in Byond with no errors, but when I try to deposit
any items in dreamseeker, I get a stack error... I think I know where the
problem is....

Move(/obj in src.contents to usr.contents)

should be something like :

/obj/Chest/o
Move(usr.contents in o)

but when trying to make my variables work I consistently get errors.

The first 2 verbs work perfect (open and close verbs, so does their new variable Useable)
:


When you call Move(), you're actually calling src.Move(), which means "move src". The first argument to Move() should be the desired location. In this case, the desired location is usr, the player using the verb.
// move src to usr
src.Move(usr)

// "src." can be left off
Move(usr)
I tried to fix it up using this code, but when I pressed store or withdraw, the whole chest would go into my inventory.

I fixed it up like this:

    Store()
set src in view(1)
if (src.Useable != 0)
Move(usr.contents in(src.contents))
usr << "You put your stuff into the chest"
else
usr << "The Chest is closed."

Withdraw()
set src in view(1)
if (src.Useable != 0)
Move (src.contents in(usr.contents))
usr << "You withdraw your items from the chest."
else
usr << "The chest is closed."


This worked a little better... it now says "Putting your stuff in the chest" and "Withdrawing items from chest", but no items actually leave my inventory or go back in...
I think I need a list() to show which items I want to store and which items I need to withdraw... I would also like to limit storage to 28... I hope you guys can help, thanks in advance.
Best response
Haha, I wasn't really paying attention. You don't want to be moving src (the chest), you want to move some object from inside the chest into the user (and vice-versa).

The main issue is that your syntax for calling Move() doesn't make sense to the computer.
Move(/obj in usr.contents to src.contents)
Move(usr.contents in(src.contents))

Format:
movable.Move(NewLoc, NewDir, NewStepX, NewStepY)

What you're doing is trying to pass these expressions as NewLoc:
/obj in usr.contents to src.contents
usr.contents in(src.contents)

But neither expression makes sense and returns a loc.

Eventually, you will have a reference to one of the objects and a reference to the destination, and you will then call Move like so:
object.Move(player)

where "player" is whatever variable that contains the user that is interacting with the chest (assuming that's where you want the object to go).

If you want to ask the player to choose something, one simple way is to call input().
    Store()
set src in view(1)
// `Useable` is shorthand for `src.Useable`
// `if(Useable)` is shorthand for `if(Useable != 0 && Useable != "" && Useable != null)`
if(Useable)
// `as null | anything`: `null` enables a Cancel button
// and `anything` doesn't apply a filter to the "things"
// `in usr` fills the prompt with "things" from usr.contents
// `var atom/movable/object` declares a new variable
// called `object` with the type `/atom/movable`
// so we can access vars and procs of `/atom/movable`.
var atom/movable/object = input("What will you store?", "Chest") as null | anything in usr

// input() returns null when the Cancel button is clicked, so check that the object exists.
if(object)

// "Move object to usr"
object.Move(usr)

usr << "You put [object] into the chest."

else
usr << "The chest is closed."

// and similar for Withdraw(), but instead of `anything in usr` it'll be `anything in src`

(It's recommended that you don't use input() in your final product since it's not customizable and looks out of place)

I would also like to limit storage to 28
You can just check the length of the contents list in Store() to disallow storing anything if there are 28 items in the chest. You should be able to do that much on your own.