0

I want to put money in the game like a bank account the problem is that when I put the variable "BankAccount" which is attached to an Interger in the GameScene or the game view controller only those to know what "account" is. When I go to my secondScene it doesn't know what account is. So. I won't every scene to be able to access/know what "account" is. And if I put account as a variable in all scenes then now they are not the same account. I don't know what to do?

Hunter
  • 1,321
  • 4
  • 18
  • 34
  • possible duplicate of [How to make data visible for all view controllers?](http://stackoverflow.com/questions/31592910/how-to-make-data-visible-for-all-view-controllers) – vikingosegundo Aug 09 '15 at 00:24

1 Answers1

0

You have to make sure that your variable is outside of any functions or classes, unless you want to use those methods to call the variable.

Variables defined outside of any blocks will be available to the entire application.

If your var is defined inside a block then you need to use that block to call it.

struct getMoney {
var money: Int = 100
}

You could access it like...

var playerMoney = getMoney.money
ALTVisual
  • 116
  • 10
  • Thanks this helped a lot!! – Hunter Aug 09 '15 at 00:22
  • 1
    see the linked question. in short: pass it where you need it. – vikingosegundo Aug 09 '15 at 00:49
  • Globals can cause issues when misused. Using appdelegate.swift is another option. See the following link.. [Get Reference from Appdelegate](http://stackoverflow.com/questions/24046164/how-do-i-get-a-reference-to-the-app-delegate-in-swift) – ALTVisual Aug 09 '15 at 06:50