Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

Two quick questions.....(Add Item, and Call a Script)

Ok. I just have two simple questions..... I am just learning how to script and am trying to make a simple mining script. I need to know how to add an item to the players inventory with scripting and how to call my script. I know my script may not be the most efficient, but like I said, I am just learning. Could anyone know how to call this script through an event???? (Go up to the rock and use the action key.....) Thanks!!!  :thumb:

Code:
class Mine
  def main
    coal = rand(10)
  end
  def initialize
    super(0, 0, 640, 480)
    self.contents = Bitmap.new(width - 32, height - 32)
    update
  end
  def update
    self.contents.clear
    if coal > 7
      self.contents.draw_text(0, 0, 200, 32, "Mined Coal!")
    else
      if coal > 3
        self.contents.draw_text(0, 0, 200, 32, "Darn, no coal to be found.")
      else
        self.contents.draw_text(0, 0, 200, 32, "You miss, but you try again.")
        main
      end
    end
  end
end
 
Ok, im in the same boat as you, as scripting goes. but i will try to help you with the 1° question:
Code:
For items:
          $game_party.gain_item(@item.id, @number)
For Weapons:
          $game_party.gain_weapon(@item.id, @number)
For Armors:
          $game_party.gain_armor(@item.id, @number)

About the 2° question, you need to call your script via "Call script" in the event pages (last one). In the call script box you write something like: $scene=Scene_yourscriptscenename.new

After that im totally lost of what to do. Good luck
 

khmp

Sponsor

"coal" has local scope inside your Mine class' main method. So when it does the comparison against a number in the update it questions what the hell is this thing and assumes nil. So my guess is the window only ever displays "You miss, but you try again." To correct this you give coal instance variable scope. You do that by placing a "@" symbol in front of coal. Second you never actually call the method main when it matters. Call it before the number is needed not afterwards. Also do you need a window that extends over the whole screen to display one line of text? Finally I noticed the call of super inside initialize which signifies that this class is the child of something but you have no definition of said relationship. And it looks like you want it to derive from Window_Base, or Window. So use a left caret to show it.

Code:
class Mine < Window_Base
  def main
    @coal = rand(10)
  end
  def initialize
    super(0, 0, 640, 480)
    self.contents = Bitmap.new(width - 32, height - 32)
    main
    update
  end
  def update
    self.contents.clear
    if @coal > 7
      self.contents.draw_text(0, 0, 200, 32, "Mined Coal!")
    else
      if @coal > 3
        self.contents.draw_text(0, 0, 200, 32, "Darn, no coal to be found.")
      else
        self.contents.draw_text(0, 0, 200, 32, "You miss, but you try again.")
      end
    end
  end
end

Good luck with it xgamexfreakx! :thumb:
 
Yeah, I fixed that just a minute ago and no, I don't need one to cover the entire screen. I just haven't got around to figuring out dimensions yet, so I just started with the screen size. I made the changes that you said to (btw- the class needs to read
class Scene_Mine < Window_Base because of how it is called.) and I get a script hanging error. I've gotten the error with netplay when the game codes don't match up, but never in just a normal project.

So right now, my code reads....

Code:
class Scene_Mine < Window_Base
  def main
    @coal = rand(10)
  end
  def initialize
    super(0, 0, 640, 480)
    self.contents = Bitmap.new(width - 32, height - 32)
    main
    update
  end
  def update
    self.contents.clear
    if @coal > 7
      self.contents.draw_text(0, 0, 200, 32, "Mined Coal!")
    else
      if @coal > 3
        self.contents.draw_text(0, 0, 200, 32, "Darn, no coal to be found.")
      else
        self.contents.draw_text(0, 0, 200, 32, "You miss, but you try again.")
      end
    end
  end
end

and is being called with....

Code:
$scene = Scene_Mine.new

Any idea what is causing the script hanging???


and for futher reference, what usually causes script hanging errors?

Thanks for the help!
 

khmp

Sponsor

When a scene runs its main method is what is called repeatedly. So it's smart enough to realize that there is no escape from your code and that's why you get that hanging script error. I thought you were making a custom window not a scene so I didn't question it as much as I should have. A scene shouldn't derive from a Window class. It is poor code design. A good practice is to break the two objects apart. One into a scene, the other into a window.

Code:
class Window_Mine < Window_Base
  attr_accessor :coal
  def initialize
    super(0, 0, 640, 480)
    self.contents = Bitmap.new(width - 32, height - 32)
  end

  def refresh
    return if @coal.nil?
    @o_coal = @coal
    self.contents.clear
    if @coal > 7
      self.contents.draw_text(0, 0, 200, 32, "Mined Coal!")
    else
      if @coal > 3
        self.contents.draw_text(0, 0, 200, 32, "Darn, no coal to be found.")
      else
        self.contents.draw_text(0, 0, 200, 32, "You miss, but you try again.")
      end
    end
  end

  def update
    refresh if @coal != @o_coal
  end
end

class Scene_Mine
  def main
    @mining_window = Window_Mine.new
    Graphics.transition
    loop do
      Graphics.update
      Input.update
      update
      break if $scene != self
    end
    Graphics.freeze
    @mining_window.dispose
  end

  def update
    @mining_window.update
    @mining_window.coal = rand(10) if Input.trigger?(Input::L)
    $scene = Scene_Map.new if Input.trigger?(Input::C)
  end
end

I didn't check that code it's done completely in this post so I have no idea if I'm missing something besides I'd prefer you'd only use it as a basis.

Good luck with it xgamexfreakx! :thumb:
 
Hopefully this will be my last question for this topic..... When you use the Scene class, do you still draw the text and stuff like you do in the Window class? I was using the Window class because that is just what I learned to use. Also, when it calls the scene, it doesn't show anything. I am guessing that the text needs to go into the scene half of the script. Thanks for your help!
 

khmp

Sponsor

xgamexfreakx":37kkz9h4 said:
When you use the Scene class, do you still draw the text and stuff like you do in the Window class?
Take a gander at every Scene class in the RTP. Each one relegates drawing behavior to other objects, windows, sprites etc. The scene itself is only accountable in updating the objects. Or at least that's the mindset you should have when creating a custom scene. Think of a scene as a container of objects you want to show and/or have the user interact with for an effect.

xgamexfreakx":37kkz9h4 said:
Also, when it calls the scene, it doesn't show anything. I am guessing that the text needs to go into the scene half of the script.
If you hit the "Q" key on the keyboard while you are in Scene_Mine you will see text appear.

If you have any further questions just ask.
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top