Language Filter

So, pretty new to scripting from scratch and am working on something which I think should be simple.


So this part of the script is supposed to set weather or not the language filter is on and display the right message depending;
 

Spoiler
@adult = true


def method_adult()
  case
  when @adult == true
    $game_message.add("Adult language filter on")
    @adult = false
  when @adult == false
    $game_message.add("Adult language filter off")
    @adult = true
  else
    ####
  end
end



This is the test method, which is supposed to show stars instead of the swear word if the language filter is on
 

Spoiler
def methodq()
  string = "Well Frick you!"
    unless @adult == true
      string.gsub!("Frick", "****")
    end
  $game_message.add(string)
end



But for the life of me I cannot get it to function properly... currently the switch changes nothing and the language filter is constantly on.


Can anyone help me out?
Both snippets are incomplete, you're not telling us which class you assigned the snippets to and also don't tell us where you call the methods you just defined.


Placing a method definition alone does not execute it, so I think what's happening is that you never called those methods from anywhere.
My apologies, both of the methods are called by events in the game (screenshot below) but the assignment of class is something I not aware of how to do for something like this

Spoiler
Untitled.png



Also, both are evented as "Script: methodq()" and "Script: method_adult()"
Map screenshots are almost automatically useless - the only case where a map screenshot might help is if you have either a graphical error or if you have problems with a blocked movement.


We need the screenshots of the event pages to see if there is any error.


However, in this case the problem is the missing class assignment - the event command "script" can only execute commands that are accessible to the game_interpreter, all functions outside that (limited) area cannot be used inside a script command.


I suggest you go to the "learn Ruby&RGSS" part of the forum and update your knowledge about classes in Ruby.
Methods made outside of any class/module can be called from anywhere, even in script calls.


Your usage of case statement is wrong, as far as I know.


You should do it this way:


def method_adult
case @adult
when true
$game_message.add("Adult language filter on")
@adult = false
when false
$game_message.add("Adult language filter off")
@adult = true
end
end


But I don't know how instance variables function outside of any class/module. I don't think you can use them like that, but I never tried it, so I can't know for sure.


What you can surely do is to change that instance variable to a global one, and since you already use a switch to toggle the filter ON/OFF, you can use a switch instead.


Example:


def method_adult
case $game_switches[12]
when true
$game_message.add("Adult language filter on")
when false
$game_message.add("Adult language filter off")
end
$game_switches[12] = !$game_switches[12]
end


When you call this method in a script call, it checks switch 12. It displays different message based on the state of switch 12.


After the message, it toggles switch 12 automatically (so, if it was ON before, it will be turned OFF, and if it was OFF before, it will be turned ON).


Similarly, change the '@adult' instance variable in your other snippet to '$game_switches[12]' and it should work.


Of course, you can change the ID of the switch to your preferred one.


But it is a bad practice to make methods outside any classes/modules, so I suggest you move the method into a module at least.
Thank you Sixth! By changing it to a global switch it works exactly as I was hoping it would, and thank you for your insights into scripting, Andar, thank you for directing me towards the Learn Ruby section, found loads of useful information there to 🙂


Looks like I'm trying to run before I can walk when it comes to my understanding of RGSS lol

Original thread: https://forums.rpgmakerweb.com/threads/language-filter.56219/