ID:158934
 
Hello, I wanted to create an array of bank accounts that is accesible from everything.

I used to know how to make it but forgot.
Something like this?
Code:
var/ba/ba = new /ba/ba()
var/accounts[2][] //Multidimensonal list containing names on the first row and cash amount on second.

And i want to access it like this.

var/money = ba.amount[2][12]

Please help

Thanks in advance,
Lcooper
Um, wouldn't it be easier to just keep a variable on the mob for this information?

mob/var
bank = list(0,list()) // bank[1] = money, bank[2] = list of items... I could have defined this as bank[2]


NPC/Banker
Click()
usr << "[src.name]: You have $[usr.bank[1]] and [length(bank[2])] items in your account."
// src = the NPC; usr = the person who clicked
In response to GhostAnime
It may be easier, but it may also not be what he wants. For example, he might want to access the bank information when a player isn't online.

Anyway, what you want here isn't exactly a multi-dimensional list. Better would be an associated list. So, you'd do something like this:

bank
var/list/accounts = list()
proc
//open an account, return 1 on success, 0 if account already exists
open(var/name)
if(accounts.Find(name))
return 0
else
//add the account, and set its balance to zero
accounts += name
accounts[name] = 0
//deposit money into an account, return new balance on success, null on failure
deposit(var/name, var/amount)
if(amount <= 0)
return null
// . is the default return value, so it will be returned without an explicit return statement
. = balance(name)
if(isnum(.))
accounts[name] += amount
. += amount
//withdraw money from an account, return new balance on success, null on failure
withdraw(var/name, var/amount)
if(amount <= 0)
return null
. = balance(name)
if(isnum(.) && . >= amount)
accounts[name] -= amount
. -= amount
//returns the balance of an account, returns null if account does not exist
balance(var/name)
if(accounts.Find(name))
return accounts[name]
else
return null


I spent too long thinking about what good return values would be, there.
In response to Garthor
Garthor wrote:
It may be easier, but it may also not be what he wants. For example, he might want to access the bank information when a player isn't online.

Anyway, what you want here isn't exactly a multi-dimensional list. Better would be an associated list. So, you'd do something like this:

bank
> var/list/accounts = list()
> proc
> //open an account, return 1 on success, 0 if account already exists
> open(var/name)
> if(accounts.Find(name))
> return 0
> else
> //add the account, and set its balance to zero
> accounts += name
> accounts[name] = 0
> //deposit money into an account, return new balance on success, null on failure
> deposit(var/name, var/amount)
> if(amount <= 0)
> return null
> // . is the default return value, so it will be returned without an explicit return statement
> . = balance(name)
> if(isnum(.))
> accounts[name] += amount
> . += amount
> //withdraw money from an account, return new balance on success, null on failure
> withdraw(var/name, var/amount)
> if(amount <= 0)
> return null
> . = balance(name)
> if(isnum(.) && . >= amount)
> accounts[name] -= amount
> . -= amount
> //returns the balance of an account, returns null if account does not exist
> balance(var/name)
> if(accounts.Find(name))
> return accounts[name]
> else
> return null

I spent too long thinking about what good return values would be, there.


Um its compiles ok but whenever i try to add something like this to the code it gives me an error.
bank.withdraw(usr,amount)
It gives me an undefined var error.
In response to Lcooper
Because you need to create a bank first.
In response to Garthor
Like this?
var/bank/bank = new bank()
In response to Garthor
Im having trouble accessing the bank.

var/bank/bank
bank
var/list/accounts = list()
.......




/mob/Login()
bank.open(src.ckey)
bank.deposit(src.ckey,100)
world << bank.balance(src.ckey) //For testing purposes.

Any idea what might be the problem? Im not getting any compiling or runtime errors.
In response to Lcooper
You didn't initialize the bank from what I can see. And, because you put that in Login(), the errors will appear before the game loads. Replace var/bank/bank with var/bank/bank = new().