Help - Search - Members - Calendar
Full Version: Changing Module inside Eventing
RPG RPG Revolution Forums > Scripting > Script Development and Support > RGSS2
Jonnie19
So, I'm now jumping into scripting, so after working with abit of JS thanks to Code Acadamy. I'm attempting to make my first menu script...
Basically I am looking for three seperate menu backgrounds for each of the three Characters. I'm using GubID's FFT Menu Script Tutorial to help start me out smile.gif

Anywho, here is the beginning of the script (At the moment I'm just trying to get the actual thing to change when I use an Script Even command.

My small script
CODE
module Lullaby_Menu
  #-----------------------------------------------------------------------------
  # Menu_Background_Type
  #-----------------------------------------------------------------------------
  # 0 = First Character
  # 1 = Second Character
  # 2 = Third Character
  #-----------------------------------------------------------------------------
  Menu_Character_Type = 0
  First_Chara_BG = "Menu_Back"
  Second_Chara_BG = "Menu_Back1"
  Third_Chara_BG = "Menu_Back2"
end
class Scene_Menu < Scene_Base
  def start
    create_menu_background
    case Lullaby_Menu::Menu_Character_Type
    when 0
      bitmap = Cache.system(Lullaby_Menu::First_Chara_BG)
      @menuback_sprite.bitmap = bitmap
    when 1
      bitmap = Cache.system(Lullaby_Menu::Second_Chara_BG)
      @menuback_sprite.bitmap = bitmap
    when 2
      bitmap = Cache.system(Lullaby_Menu::Third_Chara_BG)
      @menuback_sprite.bitmap = bitmap
    end
  end
  def update
    if Input.trigger?(Input::B)
      Sound.play_cancel
      $scene = Scene_Map.new
    end
  end
  def terminate
    super
    dispose_menu_background
  end
end

Now, what I wanna know is how to change
CODE
Menu_Character_Type = 0

inside an event command.
I know this is probably easy for experience scripters, and may seem a stupid question, but hey I'm learning.

Thanks for your help in advance tongue.gif
Thallion
That variable is a constant b/c it starts with a capital letter. You can't change the value of a constant at runtime.
Jonnie19
Okay, thanks to Jens and Thallion for both helping me correct the problem...now I have a syntax error, which I am trying to solve, now perhaps an experienced scripter could help solve the issue smile.gif
CODE
module Lullaby_Menu
  def initialize
    First_Chara_BG = "Menu_Back",
    Second_Chara_BG = "Menu_Back1",
    Third_Chara_BG = "Menu_Back2",
    end
  def Lullaby_Menu.menu_character_type
        if @menu_character_type.nil?
          @menu_character_type = 0
        end
      end
       return @menu_character_type
     end
      def Lullaby_Menu.menu_character_type=(value)
       @menu_character_type = (value)
     end
   end
end

There is something wrong with the Syntax of this module....(thank you Jens tongue.gif) It's probably something simple but i've tried going through the coding and I'm not sure I've got this right smile.gif
Help really appreciated tongue.gif
Philip
QUOTE (Jonnie19 @ Nov 26 2012, 05:33 PM) *
Okay, thanks to Jens and Thallion for both helping me correct the problem...now I have a syntax error, which I am trying to solve, now perhaps an experienced scripter could help solve the issue smile.gif
CODE
module Lullaby_Menu
  def initialize
    First_Chara_BG = "Menu_Back",
    Second_Chara_BG = "Menu_Back1",
    Third_Chara_BG = "Menu_Back2",
    end
  def Lullaby_Menu.menu_character_type
        if @menu_character_type.nil?
          @menu_character_type = 0
        end
      end
       return @menu_character_type
     end
      def Lullaby_Menu.menu_character_type=(value)
       @menu_character_type = (value)
     end
   end
end

There is something wrong with the Syntax of this module....(thank you Jens tongue.gif) It's probably something simple but i've tried going through the coding and I'm not sure I've got this right smile.gif
Help really appreciated tongue.gif

You have too many end statements in your "Lullaby_Menu.menu_character_type" def
Jonnie19
Still struggling with this one, i've removed said additional end that wasn't needed XD but i'm STILL I'm getting the error for Line 3...
I'm most likely being really stupid, I also noticed I wrote...mmodule :X
CODE
module Lullaby_Menu
  def initialize
    First_Chara_BG = "Menu_Back"
    Second_Chara_BG = "Menu_Back1"
    Third_Chara_BG = "Menu_Back2"
  end
  def Lullaby_Menu.menu_character_type
        if @menu_character_type.nil?
          @menu_character_type = 0
        end
       return @menu_character_type
     end
      def Lullaby_Menu.menu_character_type=(value)
       @menu_character_type = (value)
     end
   end
end
Jens of Zanicuud
You can't declare constants inside a method (such as initialize); you must use @ variables if you want your module to "see" them outside the initialize method or just declare them as constants outside a method.
Try this one:

CODE
module Lullaby_Menu
  
  Chara_BGS = {
  1 => "Menu_Back",
  2 => "Menu_Back 2",
  3 => "Menu_Back 3",
  }
  
  def Lullaby_Menu.menu_character_type
      if @menu_character_type.nil?
          @menu_character_type = 0
        end
       return @menu_character_type
     end
    
  def Lullaby_Menu.menu_character_type=(value)
    @menu_character_type = (value)
  end
  
  def Lullaby_Menu.menu_picture
    return Chara_BGS[@menu_character_type]
  end
end


I've added a method (Lullaby_Menu.menu_picture) which gives you back the item in thehash named Chara_BGS, which matches with the @menu_character_type variable's value (e.g. if you want to call "Menu Back", just call Lullaby_Menu.menu_character_type= 1. If you want to access an element of the hash, just call Lullaby_Menu::Chara_BGS[index] where index is the number you see on the left of the hash - commonly named key).
Hashes are powerful instruments you should learn about, they solve a whole bunch of problems smile.gif

There should actually be another method to create that module using the initialize, but I have to read some documentation before saying that for sure.

For any issue concerning this point, just reply here or send me a PM smile.gif

I hope this can help,

Jens
Philip
QUOTE (Jens of Zanicuud @ Nov 27 2012, 10:13 AM) *
You can't declare constants inside a method (such as initialize); you must use @ variables if you want your module to "see" them outside the initialize method or just declare them as constants outside a method.
Try this one:

CODE
module Lullaby_Menu
  
  Chara_BGS = {
  1 => "Menu_Back",
  2 => "Menu_Back 2",
  3 => "Menu_Back 3",
  }
  
  def Lullaby_Menu.menu_character_type
      if @menu_character_type.nil?
          @menu_character_type = 0
        end
       return @menu_character_type
     end
    
  def Lullaby_Menu.menu_character_type=(value)
    @menu_character_type = (value)
  end
  
  def Lullaby_Menu.menu_picture
    return Chara_BGS[@menu_character_type]
  end
end


I've added a method (Lullaby_Menu.menu_picture) which gives you back the item in thehash named Chara_BGS, which matches with the @menu_character_type variable's value (e.g. if you want to call "Menu Back", just call Lullaby_Menu.menu_character_type= 1. If you want to access an element of the hash, just call Lullaby_Menu::Chara_BGS[index] where index is the number you see on the left of the hash - commonly named key).
Hashes are powerful instruments you should learn about, they solve a whole bunch of problems smile.gif

There should actually be another method to create that module using the initialize, but I have to read some documentation before saying that for sure.

For any issue concerning this point, just reply here or send me a PM smile.gif

I hope this can help,

Jens

Yeah I was thinking that but I've never tried it before so you got to it before me LOL.

@Jonnie not only can you not define constants inside a method you still had one too many ends in that module.

Jens of Zanicuud
You're right, Philip smile.gif
I've taken care of that end in the snippet I posted in the previous reply, however.
That code I posted should (more or less) work...

Jens
Jonnie19
Thankyou guys, it is now working perfectly, I never considered using Hash before now, but it really is alot easier XD so thanks to both of you for your help tongue.gif
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2013 Invision Power Services, Inc.