Very Simple Question

● ARCHIVED · READ-ONLY
Started by Phonantiphon 7 posts View original ↗
  1. Why does this very basic script fail:





    with this error:





    All I need to know is how to initialise the variable to stop it from failing.


    That's all.


    I'm not new to scripting I am just new to Ruby and I just need to get my head around some of the basics. So, just a nudge - why does this fail on load?


    I have the $game_switches[1] switch set to "true" as a Player Touch event on the map.


    To clarify: I get that the variable has to exist before I can apply a value to it. I've been through the scripts I am using but what I cannot see is a methodology for initialising the variable\switch\widget that I need to use PRIOR to when I need it, so the game does not fail.


    I can do this this in Powershell and T-SQL, but I cannot work out how to declare and set the initial value for a variable in Ruby, specifically in VX Ace.


    That's all the info I need to know - how to do that...?!


    Thank you!


    Anyone? :)
  2. Well basically it's because $game_switches does not exist at the time you are trying to access it in your module PHON.


    In your next module

    Code:
    @Switch

    will be undefined because it is outside the scope it is set at.


    I'm assuming you got an error when trying to add a message to $game_message before you wrapped it in an if statement. While the reason for that would be the same problem as $game_switches it's simply not defined at the time you are trying to add a message to it. So to fix this try:

    Code:
    module PHON
        def self.test(text)
          $game_message.add(text.to_s)
        end
    end

    Then in an event using the script command type PHON.test("sometext")


    and it will display the text in a message window.
  3. I believe such errors occur because you're testing the variable before the default code declares it. Where are you calling that module?
  4. I've moved this thread to RGSSx Script Support. Please be sure to post your threads in the correct forum next time. Thank you.


    Please use meaningful thread titles.
  5. Hi, Sorry didn't realise it needed to go here rather than the other section. :)


    Anyway thanks for your help but I spent some time doing some more reading and I worked it out - there are better solutions I am sure, but this works - (it's always tricky getting started in another language!!)


    @Sarlecc: Thank you very much for your example and whilst that is extremely useful it's not quite what I wanted...


    basically I want the status of the control switch to allow or deny the action of something else, based upon that switch's setting.


    What I found that works is:

    Spoiler
    module PHON1
        module GET_SWITCH
            SWITCH = 25
        end
    end



    module PHON_SWITCH
        def self.no_action



            return $game_switches[PHON1::GET_SWITCH::SWITCH]
            return false if PHON1::GET_SWITCH::SWITCH <= 0
        end
    end



      def the_thing_that_I_want
        if !PHON_SWITCH.no_action
         the_thing_that_I_want_either_works_or_does_not_work_depending_on_if_switch == 1 or =\= 1
        end
      end



    #except the above actually works the wrong way round but that's kind of beside the point! :D

    As I say, it's probably a little bit clunky but that was what I was after! :D


    Thanks again guys.


    If a mod could close this now, that would be brilliant, thanks!!
  6. There is a line which will never get executed in your code.


    module PHON_SWITCH
    def self.no_action
    return $game_switches[PHON1::GET_SWITCH::SWITCH] # This line will always return something (true or false) ending the method.
    return false if PHON1::GET_SWITCH::SWITCH <= 0 # This line will never be executed.
    end
    end


    Switch the lines and both will get checked.
  7. @Sixth ah thank you! I missed that...