Submit Your Article


 
RPG Maker

Welcome Guest ( Log In | Register )


  Games Resources RPG Maker VX RPG Maker XP Scripts Tutorials Downloads

 
Reply to this topicStart new topic
> How to make a variable control a number in script?
HeartBorne
post Feb 8 2012, 10:13 AM
Post #1



Group Icon

Group: Member
Posts: 3
Type: Event Designer
RM Skill: Skilled




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?
Go to the top of the page
 
+Quote Post
   
Titanhex
post Feb 8 2012, 10:26 AM
Post #2


Guru of Water
Group Icon

Group: Revolutionary
Posts: 1,096
Type: None
RM Skill: Masterful
Rev Points: 5




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.


__________________________
Go to the top of the page
 
+Quote Post
   
HeartBorne
post Feb 8 2012, 01:13 PM
Post #3



Group Icon

Group: Member
Posts: 3
Type: Event Designer
RM Skill: Skilled




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 ;/

Go to the top of the page
 
+Quote Post
   
ForeverZer0
post Feb 8 2012, 08:49 PM
Post #4


Level 2
Group Icon

Group: Member
Posts: 26
Type: Scripter
RM Skill: Masterful




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


Go to the top of the page
 
+Quote Post
   
Philip
post Feb 8 2012, 11:20 PM
Post #5


Nura (The Jade Ring)
Group Icon

Group: Revolutionary
Posts: 325
Type: Developer
RM Skill: Masterful




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.

This post has been edited by Philip: Feb 8 2012, 11:21 PM


__________________________
"If your mind goes blank don't forget to turn off the sound." Unknown Author

Phil


Go to the top of the page
 
+Quote Post
   
Resource Dragon
post Feb 9 2012, 03:28 PM
Post #6


Dragon has RAWR. So... RAWR.
Group Icon

Group: Local Mod
Posts: 984
Type: Developer
RM Skill: Masterful
Rev Points: 95




@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



This post has been edited by Resource Dragon: Feb 9 2012, 03:32 PM


__________________________
click me ->Signup for Digital Hijinks!<- click me
Go to the top of the page
 
+Quote Post
   
HeartBorne
post Feb 10 2012, 06:51 AM
Post #7



Group Icon

Group: Member
Posts: 3
Type: Event Designer
RM Skill: Skilled




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?
Go to the top of the page
 
+Quote Post
   

Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Lo-Fi Version Time is now: 23rd May 2013 - 07:29 PM
RPG RPG Revolution is an Privacy Policy and Legal
eXTReMe Tracker