Very Simple Question

Phonantiphon

Veteran
Veteran
Joined
Jan 17, 2016
Messages
261
Reaction score
145
First Language
English
Primarily Uses
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? :)
 
Last edited by a moderator:

Sarlecc

Veteran
Veteran
Joined
Sep 16, 2012
Messages
453
Reaction score
211
First Language
English
Primarily Uses
RMMV
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.
 

jwideman

Veteran
Veteran
Joined
Oct 2, 2015
Messages
230
Reaction score
97
First Language
English
Primarily Uses
I believe such errors occur because you're testing the variable before the default code declares it. Where are you calling that module?
 

Shaz

Veteran
Veteran
Joined
Mar 2, 2012
Messages
40,098
Reaction score
13,704
First Language
English
Primarily Uses
RMMV
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.
 

Phonantiphon

Veteran
Veteran
Joined
Jan 17, 2016
Messages
261
Reaction score
145
First Language
English
Primarily Uses
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:

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!!
 
Last edited by a moderator:

Sixth

Veteran
Veteran
Joined
Jul 4, 2014
Messages
2,162
Reaction score
822
First Language
Hungarian
Primarily Uses
RMVXA
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.
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Posts

Latest Profile Posts

How many parameters is 'too many'??
Yay, now back in action Happy Christmas time, coming back!






Back in action to develop the indie game that has been long overdue... Final Fallacy. A game that keeps on giving! The development never ends as the developer thinks to be the smart cookie by coming back and beginning by saying... "Oh bother, this indie game has been long overdue..." How could one resist such? No-one c
So I was playing with filters and this looked interesting...

Versus the normal look...

Kind of gives a very different feel. :LZSexcite:
To whom ever person or persons who re-did the DS/DS+ asset packs for MV (as in, they are all 48x48, and not just x2 the pixel scale) .... THANK-YOU!!!!!!!!! XwwwwX

Forum statistics

Threads
105,849
Messages
1,016,981
Members
137,563
Latest member
cexojow
Top