Main Menu
  • Welcome to The RPG Maker Resource Kit.

Changing gold with event script

Started by StaticSD, December 22, 2011, 10:30:56 PM

0 Members and 1 Guest are viewing this topic.

StaticSD

Hello,
I made a very simple banking system. Basically you can only carry so much at one time, so you have to keep depositing to keep earning, but both cash on hand and in the bank can be used at any time. In the Game_Party script I added some code to get this done and that works great but now I'm trying to make a Banker NPC so you can actually deposit your cash.

In the Game_Party script I added the following:
def deposit
  @bank = [[@bank + gold, 0].max, 9999999].min
  @gold = [[@gold - gold, 0].max, goldmax].min
end


But I can't figure out how to call this from the Banker event. I tried:
@game_party = Game_Party.new()
@game_party.deposit


Obviously this just makes a new instance of game_party that doesn't contain any information from the original game_party instance. Is there any way I can call deposit from the event? If not, are there other ways I can go about this? I don't want to use someone else's bank script so please don't say "Use Variables!" ... unless that's the only way  :P

pacdiggity

The instance of Game_Party used without the game is $game_party. So you should use:$game_party.deposit(gold)where gold is an integer. You'll also have to adjust your deposit method to hold an argument to work properly.def deposit(gold)
  etc
it's like a metaphor or something i don't know

StaticSD

Ah! Thank you. I've been working with C++ all my short life so Ruby is taking some getting used to. I'll have to look up what @ and $ mean I guess... along with everything else. Anyways, thanks again. This worked great  :)

pacdiggity

I can tell you.
@ signifies an instance variable, meaning that it is accessible only through that instance of the class (i.e., that object). It can be accessed throughout the entire instance, but nowhere else unless made so.
$ is a global variable, it is accessible everywhere in the library. So if I did this:
class Whatever
  def initialize
    @thing = 2
  end
end
w = Whatever.new
p @thing # -> has no idea what you're talking about.
class Whatever
  def fix
    $thing = @thing
  end
end
p $thing # -> 2
class Whatever
  def something
    @thing += 1
  end
end
w.something # -> @thing = 3
w.fix # -> $thing = @thing
p $thing # -> 3

If that made any sense.
it's like a metaphor or something i don't know