RPG Maker VX Ace

RPG Maker VX Ace

View Stats:
using $game_switches in a script turning up errors
I'm using a custom script and I'm trying to make it decide to run the script one way or the other. As such, I'm making it read a switch in a
if $game_switches[62] == true blahblahblah code else more code lmao end

typical fashion. The problem is I get a "nilclass" return on startup and the game crashes. I know that it's because the script is calling a variable before the variables class is created, so how can I get it to read this switch this early? Is there a way to make this script run after the variable class script is read, or a way to make it read a switch this early? It's at the bottom of my materials section, and there's not really any other alternative to this type of code, since I'm making my game load a difficulty menu and then once something is beaten, it changes all the difficulties.

Any help would be greatly appreciated c:
Last edited by vita.hacks.guide; Nov 19, 2015 @ 2:20pm
< >
Showing 1-12 of 12 comments
Gamerpro69 Nov 19, 2015 @ 1:51pm 
where n is set a number of that variable if it still happens then screenshot

Gamerpro69 Nov 19, 2015 @ 1:52pm 
so if varible 1 was for switch instead of n put 1
vita.hacks.guide Nov 19, 2015 @ 2:20pm 
My switch isn't based on a variable, I only wrote it that way in the above code to reflect it being a typical script.

I'll edit the comment to reflect it, but my switch is just $game_switches[62], and I want it to actually process that switch to check if it returns true or false.
Kio Kurashi Nov 19, 2015 @ 3:27pm 
I think that if it is running before the class is even made then even if we make it wait until that class is made everything will be false. The data would then be updated through the savefiles at which point your code would have already ran. Either way here is how you can force the code to wait ( I found otherways but it just made the code not run period)
class Thingy def initialize if $game_switches[62] == true p "It's true" else p "It's false" end end end class Game_Variables alias :theGreatInitialize :initialize def initialize theGreatInitialize Thingy.new end end
Edit:
Another possibility you could use is the .ini file. Since that is stored outside the save games.
If you need the code to be run after the game though then just put it into a Script Call command in a parallel process common event.
Last edited by Kio Kurashi; Nov 19, 2015 @ 4:25pm
vita.hacks.guide Nov 19, 2015 @ 7:55pm 
The code seems like it works, my issue is now that what I'm basically doing is taking an entire script and placing it within the conditional branch, so that the parent branch will use the switch to decide which version of the script to run. Not exactly an optimal way to do things, I know, but I'm pretty new to this coding thing so it's a matter of "as long as it works, it works."

I'm using the CSCA Difficulty script, alongside the LGlobalSave script, so that once my game is completed, it will save a switch saying the game is completed to load at the start of the game, which will change the difficulty listings. I wanted it so that every difficulty changes its name (it's some funky fourth-wall breaking thing I'm doing to trigger a spooky boss fight), which is why I'm trying to get the script to decide between one of two versions of itself to run based on the switch's value.

The issue I run into now is that by doing so, I insert modules or class definitions into a method, which comes up as an error.

Is there a workaround for this, or better yet, some way to place a script beforehand that will decide which version of the same script to run and which one to not run based on a switch's value?

Again, thank you so much for your help so far!
Kio Kurashi Nov 19, 2015 @ 8:03pm 
Originally posted by minatums:
The code seems like it works, my issue is now that what I'm basically doing is taking an entire script and placing it within the conditional branch, so that the parent branch will use the switch to decide which version of the script to run. Not exactly an optimal way to do things, I know, but I'm pretty new to this coding thing so it's a matter of "as long as it works, it works."

I'm using the CSCA Difficulty script, alongside the LGlobalSave script, so that once my game is completed, it will save a switch saying the game is completed to load at the start of the game, which will change the difficulty listings. I wanted it so that every difficulty changes its name (it's some funky fourth-wall breaking thing I'm doing to trigger a spooky boss fight), which is why I'm trying to get the script to decide between one of two versions of itself to run based on the switch's value.

The issue I run into now is that by doing so, I insert modules or class definitions into a method, which comes up as an error.

Is there a workaround for this, or better yet, some way to place a script beforehand that will decide which version of the same script to run and which one to not run based on a switch's value?

Again, thank you so much for your help so far!
If you could send me a picture or a copy of the file that you have written so far I can probably find a better way to do this.
vita.hacks.guide Nov 19, 2015 @ 10:17pm 
Here's the link for my current script edit: http://pastebin.com/dPbhnNyc

And here's the original script if you need some sort of comparison to the script I'm using: http://pastebin.com/LyAdCAam

My version of the script so far, I copied the original module "DIFFICULTY" and made a second module of it right underneath as "DIFFICULTYALT" where it creates the new difficulty descriptions. I then used your above script underneath it to check for the value of $game_switches[62] and run the script using DIFFICULTY module if false, DIFFICULTYALT module if true.

All I really need is an instance of the original script being able to run one way or the other based on the value of the switch, so I included the original script if you want to just use that to make a framework instead. Whatever you're willing to help with c:
Kio Kurashi Nov 19, 2015 @ 10:45pm 
Hahaha. You can't run instances of modules! Modules are loaded and classes are instances of either other classes or modules. That is what is the problem here.
Also you took the normal difficulty out of the CSCA module therby removing it from it's functions.
Also you can't define a class inside method as you have tried to do on line 261.

It would be really helpfull if I knew what tou were trying to change because it may be as simple as adding and if statement to it. Also modules as I said are loaded first so having them bound to an if statement that uses an at the time undeclared array would just put us back at square one.
vita.hacks.guide Nov 20, 2015 @ 12:21am 
Hm okay that seems to make sense!

All I'm trying to change is the difficulty descriptions and text to display completely differently in game. I made another version of the script where the text says the different things I want it to (each difficulty has a different name and description than it normally does) and it works, but I need to make it so the difficulties display their normal names and descriptions, but when a switch is activated (which IS loaded before the difficulty showing up due to the global save script), it will load the different names and descriptions for the difficulties.
Kio Kurashi Nov 20, 2015 @ 1:27am 
Originally posted by minatums:
(which IS loaded before the difficulty showing up due to the global save script),
Loaded before the scene but not before the module. That includes the Global save as well. I checked:rshocked:.

Edit: Aha! I know how to load from the ini file now. Really hard to find a tut on this (eneded up not finding one). The point though is that you can definitely do this with what you want here.

Edit2: Nevermind I had a false positive.

Edit3: Nevermind again I had a false negitive and have throughly tested it this time. It does work. Late hereso I'll just post what I have gotten. You'd need this utility module here[pastebin.com] to get the methods needed. thenin the difficulty script just before the declaration of the difficulties write the following
Checker = Utility.read_ini('Game_won') Checker = Checker.to_i if Checker == 1 Difficulty_Names = [] Difficulty_Names[1] = "Splurg" else Difficulty_Names = [] Difficulty_Names[1] = "Normal" end
make sure to add the names to the array for each of the normal and the "Splurg" modes. after that set the names in each declaration to be ":name => Difficulty_Names[n]," with n being the number of the name in the above list of course.

In your Game.ini file located in the root folder of your project ad the line at the bottom
Game_won=0
This will make the default value equal to 0 or in our case make the if statement return false until we change the value to 1.

How we do that is when you've decided that the player has won the game place in a n event somewhere at that time the following line of code
Utility.write_ini('Game_won', '1')

This will modify the value in the INI file and will enable the new names.
Last edited by Kio Kurashi; Nov 20, 2015 @ 2:18am
vita.hacks.guide Nov 20, 2015 @ 4:29pm 
IT WORKED!!

Not only this, but I assume with this utility script I can add all sorts of variables to the configuration ini and read from them across the entire game to do all sorts of advanced things now too?

But again, thank you so much! I accomplished exactly what I wanted to do!
Kio Kurashi Nov 20, 2015 @ 4:50pm 
Originally posted by minatums:
IT WORKED!!

Not only this, but I assume with this utility script I can add all sorts of variables to the configuration ini and read from them across the entire game to do all sorts of advanced things now too?

But again, thank you so much! I accomplished exactly what I wanted to do!
If you plan on doing that and perhaps orginizing them by tags (like volume control being tagged Volume) then you'll need to add another field onto both of those commands.
As an example here is a volume settings example that was given by this group here that initially taught me how to save at least.
[Volume] Master=100 bgm=100 bgs=100 sfx=100
To write to one of these values it would be:
Utility.write_ini('bgm', '90', 'Volume')
To Read from one is:
Utility.read_ini('bgm', 'Volume')

Also if you want to store these into another file (example: Custom.ini) you would add that field onto the end of those like so:
Utility.write_ini('bgm', '90', 'Volume', 'Custom.ini')
I'm sure you can figure out the read version.
Also when searching for the file it starts at the root of the project folder. So this means that if you have the Custom.ini in the System folder of the project you would change that last field to "System\Custom.ini"(I may be wrong about if it is a back or a forward slash).

Have fun with your new found knowledge and power.
< >
Showing 1-12 of 12 comments
Per page: 1530 50

Date Posted: Nov 19, 2015 @ 1:46pm
Posts: 12