Help - Search - Members - Calendar
Full Version: A little help with accessors
RPG Maker VX Community > RGSS2/Ruby Scripting > Learning Ruby & RGSS2
Aarowaim
I'm currently trying to set a variable to an event's x position. My problem is, I do not understand how to use an accessor to use a definition within the class. I found this definition, but my script is using the Game_Variables class. I'm trying to access a command that lists the location of an events x coordinate and store it as a variable.
I'm trying to access @events[id].x .
It seems like there isn't an accessor (Or I'm not trying the right way), but I may be wrong.
I've tried $game_map.events[id].x and $game_map[ :events[id].x] already.
I do see a mention of RPG::events in the Game_events script. Maybe that will help.
ShadoweD
it's .event

do a "p $game_map"

if it return "nil' it's because the map wasnt loaded yet...

if it's not nil, it should return all the atributes inside it as a @

every @ is an accessor you can call/manipulate


======[edit

hmm strange O.o I cant seem to gain access to the event list anymore lol =p

======[edit 2

it lists as @event

but it is .events like you were calling, check if you're not calling from a place where the map wasn't generated yet...

$game_map is only updated when the player change maps
FenixFyreX
if you want to call an event, use $game_map.events[i] where i is -1, 0, or 1 and above.

-1 is the player, 0 is the current event(which doesn't really work in this situation) and above is any event on the map.

every @ item, or instance variable, can be called by an attr_accessor, attr_writer, or attr_reader. To do this, simply put this at the top of the script, directly under class Whatever(Whatever being the class name)

CODE
attr_accessor :item


where item is @item throughout the script.

To store the event's x and y, you would do this:

CODE
$game_variables[1] = $game_map.events[i].x
$game_variables[2] = $game_map.events[i].y
ShadoweD
the problem is that he got the right code but didnt work ;p

if he's calling before the map is generated, it's not going to work
Aarowaim
Well, I'm trying to call the map by making a definition and using an event that accesses this definition from the same map. I understood the fact that the map MUST be generated or else I need to do map id. I keep getting a syntax error with everything I try before I can even get to the main menu. When I get home, I'll post my code so far and as many variations as I can (it's not too long.)
@FenixFyreX: I see, that's where all the seemingly uninitialized variables where hidden. Well, I could make a class (in my code) for game_map containing my attr_accessor and it will merge with the class Game_Map, right?
I think you gave me an alternative solution and my problem. I wasn't using $game_variables before identifying the variable, the reason I didn't is because I thought I had to use an existing definition in $game_variables.
FenixFyreX
Well, from the start I wasn't sure what you were doing. What exactly are you trying to do? I might be able to give you a straight answer if I know o.0 Cool, it'll be great if you could post your code.

And yes, if you put this in the editor:

CODE
class Game_Map

def do_something
end
end


It will merge with the existing Game_Map, but won't have any effect simply because it does nothing and it is not called by anything tongue.gif

Oh, and I thought I should just throw this out there, accessors do not call methods, just @instance variables. If you want to call a method from something, do

CODE
something.method_name
Aarowaim
XD you misinterpreted what I meant, but your earlier post has been right on the $game_variables[variable_id] = value.
When I said merge, I was meaning a code like this:
CODE
class game_map
attr_accessor :MyVariable
end
end

The code above would let me call MyVariable from my definitions and I could make it equal to $game_map.events[i].x, but I don't think I'll need to do this.


But, I'm just starting on testing your $game_map.events[i].x , but I'm pretty sure it'll work.
BigEd781
On a side note; instance variables, methods, and properties (accessors) always begin with a lowercase letter and words are separated by an underscore.
Aarowaim
I'm not getting any syntax errors anymore, and thanks for the pointer BigEd. Now that I'm not getting syntax errors, I can polish up my code and adapt it.
btw, I tried
CODE
def save_xy(v_id)
    $game_variables[v_id] = $game_player.x, $game_player.y
   end

about 20 times and it kept saying undefined method save_xy. But as soon as I tried pasting this higher up on my code, it worked! It must have been too close to the closer (the double end) (it was above them, but this is the only possible answer).
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-2014 Invision Power Services, Inc.