Variables

Okay, newbie question.

I'm trying to modify a script so that a game variable is included in the math.

How do I declare a game variable so that when I run the game, it doesn't say something about an error for "undefined method '[]' for nil:nilClass
I've moved this thread to RGSSx Script Support. Please be sure to post your threads in the correct forum next time. Thank you.


Tell us the EXACT error and show us where you have it set up in the script.


Without that information, we can only guess. And my guess would be that you're trying to access $game_variables before it has been created, by putting it in a script that runs before the main menu is shown and the player chooses to load a game or start a new game. $game_variables only exists while a game is actually in progress.
EvTDl9E.png


The code is located in Materials, a modification on Yanfly's Extra Drop script to make the drop_rate influenced by variable 200. The area modified is within the black box.

ikefcZf.png


And this is the error that pops up whenever I try to run it with the code.

The addition of "* $game_variables[200]" is the only modification I made to Yanfly's Extra drops code, which can be found here:

http://dl.dropbox.com/u/49701990/YEA/Extra_Drops.rb
If that script is called from DataManager at the time when the $data* objects are created (ie - if you're reading and processing all the note tags right on loading the game), then $game_variables does not exist.


You will either need to use a constant instead, or set this up as a method that can be called AFTER $game_variables[200] is set.
I'll add some background information because I think that's were you misunderstand some of the processes.


Notetags are used to easily transfer data from the developer into the scripts, but in a lot of scripts that is done only once (at new game), because reading the notetags is slower than using internal variables for operations.


So at the beginning, the data is transferred from the notetags into the script's variables, and then the script works with its variables only.


That is why placing a $game_variable into the loading function for notetags is often useless - even if it works and you change that variable during gameplay, it will not have the desired effect because in a lot of cases the "load notetags" will not be called again later.


You need to find the lines where that value is actually used and apply the game variable there to have the desired effect.
Okay, so, going off of Shaz and Andar's advice, I looked for where "drop_rate" was being processed, on the logic that it would take place after a battle and when all the delicious game variables had been booted up.

Several hours of cursing and bashing my head against a wall (which will necessitate plaster) later, I tried putting the coding under the method within extra drops, as seen below.

3gDoGuI.png


Using three items set to 1% Drop rate, I used values of 10000 and 20 to confirm the new code's ability to do both certain drops and mid-rarity drops.

Thanks for the help, gentlemen.
I think you want to multiply by those numbers, not add them (working on the assumption that your calculation in the original script was correct).


(ps - I'm a girl 😉 )
1. The idea was that with a drop rate of... say, 20%, and then a bonus of 10 in v[200], you'd get a total drop cos of 30%.

Unless I screwed that up.

2. ...derp. My apologies, madam.
Nah, a bit of testing should indicate whether it's right or not. I'm just going by the fact that you multiplied it in the first script, and you're adding it in the second one. So the two will give different results.

Original thread: https://forums.rpgmakerweb.com/threads/variables.37455/