Help - Search - Members - Calendar
Full Version: How to make a variable control a number in script?
RPG RPG Revolution Forums > Scripting > Script Development and Support
HeartBorne
Hey guys smile.gif
The subject is pretty much self explanatory.

I have this line to control the difficulty of the battle system i use:

@ai_level = 2

How can I make a variable control that number?
I've tried some things but none worked, anyone can help?
Titanhex
That is a variable. It's a class variable. You can tell cause it has an @ before it. They can only be used and changed in the class they're in, and are unique for every class.

You may mean a global variable, which is not unique, but can be changed anywhere, any time. They start with a $. You want to add that variable to the Scene_File so that it saves and loads. You can also use the $game_variables[#] series, which are what the Game Variables use and are always saved and loaded.
HeartBorne
I've tried this and it didn't work...

@ai_level = $game_variables[5]

Could you please explain in deeper detail, I'm a child when it comes to scripting ;/

ForeverZer0
Its an INSTANCE variable, not a CLASS variable. Class variables have a completely different scope.

To make a variable callable via a script call or whatever, pick a class that is already globally defined, like Game_System, Game_Map, etc. and do something like this:

CODE
class Game_Map
  att_accessor :my_variable
end


Actually that is all you really need, but it is important to remember that at this point it is not initialized and is nil, so add something like:
CODE
@my_variable = 3
to Game_Map's initialize method as well.

Now, to access and change it via a script call, you can do this:
CODE
$game_map.my_variable = 5

and
CODE
$game_variables[2] = $game_map.my_variable


Philip
QUOTE (HeartBorne @ Feb 8 2012, 03:13 PM) *
I've tried this and it didn't work...

@ai_level = $game_variables[5]

Could you please explain in deeper detail, I'm a child when it comes to scripting ;/


We can't be sure where all you use this @ai_level variable but it can only be defined and modified in the class you have it in. So for instance when you define @ai_level by the code:

CODE
@ai_level = $game_variables[5]


It sets it to Game Variable 5 only for that class instance. Once you exit the class it resets. So as stated by ForeverZer0 you should define @ai_level in a global setting or use $game_variables[5] as your @ai_level variable to begin with.
Resource Dragon
@ai_level = $game_variables[5]

This should work fine, as long as you take out the @ sign, so it should be;

ai_level = $game_variables[5]

I see no reason why this wouldn't work, as long it is in the same script your already using it in.

You might have to predefine the variable, thus making it like this;

ai_level = 5
ai_level = $game_variables[5]

Or maybe doing this;

ai_level = 5
ai_level = $game_variables[5].to_i

HeartBorne
thanks for your help but I still can't get it right...
Maybe its because ai_level can't be set to 0? IF that's the case could it be something like ai_level = variable + 1 ?

I am trying to do it on Gubid's tactical battle system (GTBS) if anyone has heard of it...

It uses

class Game_System
attr_accessor :ai_level

earlier in the script along with some other stuff and then:

alias gtbs_GS_init initialize
def initialize
@ai_level = 3
end

and much lower it returns with

def ai_level
return $game_system.ai_level
end

Am I doing something wrong?
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2013 Invision Power Services, Inc.