Local variable in event?

This applies to events (those things on the map, tab 3 -> script)

Code:
a = 1<Show message command: "blabla">
Code:
p = a # << crashes, undefined variable a # note that this is a new script command.
Local variables are local to the current scope. (There may be some nuances with lambda functions and Procs)

You are probably thinking of @instance variables rather than local variables.

*hugs*
The scope of an event command is that event command itself.
the problem with this is that each script command in an event is a new instance, and the variables defined in that script command instance are deleted when that instance is closed.

The commands would work if both A = 1 and P = A were inside the same script command, but when closing that command then all local variables defined in that script instance are deleted.

Ninja'ed by Tsukihime
Ugh Thanks... I forget that all the time! Because it's "one event" it feels like "one scope". But that's not the case.

I better just add an attr_accessor hash to the Game_Interpreter or something to dump all the local variables into. That should work (as long as parallel events use some unique hash_key_prefix).

Or I can use game_variables. But game_variables are also saved to the HD (which means that a lot of crap is saved to the HD).

I wouldn't mind 'event-scoped variables' in a future RM release or something 😉
There is a script out there somewhere that lets you define local variables for your events. I just don't remember what it was called, or who wrote it. I'm sure someone's memory will be jogged who knows what I'm talking about.
Each event has their own interpreter, but you're going to have problems because it doesn't get destroyed, so custom data from previous runs are still going to persist.


Of course this might not be a problem if you actually want that.


For common events, they all use the map interpreter, so you're going to have lots of problems with data isolation.


Of course you can always write code to clear out all instance variables I suppose...
I only know this one (not suitable):

http://www.rpgmakervxace.net/topic/18060-local-switches-variables/

And I can't find any other.

But if each event has it's own interpreter. How can they then 'share' custom data like an attr_accessor? It's not static right?

This works (bad coding no alias I know but just for testing):

class Game_Interpreter #-------------------------------------------------------------------------- # * Public Instance Variables #-------------------------------------------------------------------------- attr_reader :map_id # Map ID attr_reader :event_id # Event ID (normal events only) attr_accessor :local # <<<<<<<<<<<<<<<< #-------------------------------------------------------------------------- # * Object Initialization # depth : nest depth #-------------------------------------------------------------------------- def initialize(depth = 0) @depth = depth check_overflow clear @local = {} # <<<<<<<<<<<<<<<< end.....
Code:
local[:a] = 1
Code:
p local[:a]
Btw, I don't care that another event can also access local[:a]. I mean, in C++ you also assign/clear a variable BEFORE you use it anyway. I don't find that troublesome. And as long as parallel events add a prefix (like their event_id) it should be fine... Now that I think about it.. I could code a super simple script that adds the event_id as the prefix. But how do I do the following?:

#@local istypeof Hashclass Game_Interpreter def local[key] @local[event_id+key] end def local=(key, value) @local[event_id+key] = value endendI don't know how to make a custom getter/setter for a hash. I can't find it on Google either (only for classes).
Yeah, that's not the one I'm thinking about, and I don't see anything in that script that will save local switches and variables, either.


The Self Data Suite is the one I was thinking of, by PK8.
I don't know how to make a custom getter/setter for a hash. I can't find it on Google either (only for classes).
What?


Provide examples of what you want to achieve.
Thanks Shaz. That will come in handy. It's very very large though if you just want some local variables. That guy spend a lot of time scripting.

I did post the example:

#@local istypeof Hashclass Game_Interpreter def local[key] @local[event_id+key] end def local=(key, value) @local[event_id+key] = value endendinstead of just a attr_accessor :local.

I could just make a new Hash class and override the [] and []= but it should be possible without doing that I believe.
I don't think that is supported.

Things like this wouldn't be valid method definitions

Code:
def local[key] # square brackets?
Take a look at Game_SelfSwitches, which uses a hash.

I don't know if you can do that with a variable name in front though.

Try something like this (I haven't tested it)

Code:
def local(key)  @local[@event_id.to_s + key.to_s]enddef local=(key, value)  @local[@event_id.to_s + key.to_s] = valueend
No that won't work I already tested something similar. You really have to name the setter and getter differently (the Ruby setter does not support multiple parameters afaik). That's why I wanted to use brackets like local[]=(key, value) but that is indeed not supported either...

I'm still somewhat looking for good lightweight solution. set_local🙂a, 1) just to say a=1 is kinda long.. I could shorting it to sl🙂a1, 1) but still... I also tried overloading the [] brackets in the Game_Interpreter so you can code just [:a, 1] but that won't work either. Perhaps I call it wrong from the event. But self.[:a] wont work either.

My Ruby skills just lack at this area...
When you say "it doesn't work", it would be really helpful if you also gave the error message that it gives you.


Actually, doesn't setting it as attr_accessor automatically create the getter and setter methods? Why do you need to create them manually?
You can't define a setter method that takes more than one argument.
When you say "it doesn't work", it would be really helpful if you also gave the error message that it gives you.

Actually, doesn't setting it as attr_accessor automatically create the getter and setter methods? Why do you need to create them manually?
The error is the same for pretty much any attempt I made. Always some unexpected ',' or [] or etc. which clearly shows that, that syntax is just not supported.

No, attr_accessor is no good because then I can't prefix the event_id to it. set_local(event_id,:a,1) is even worse.

Anyway, my conclusion is that it is not possible without a lot of Ruby knowledge unless you allow the events to share their 'local' variables via an attr_accessor. In other words, this is sadly still the best solution so far (my first solution):

class Game_Interpreter attr_accessor :local alias nap_local_initialize initialize def initialize(depth = 0) nap_local_initialize(depth) @local = {} endendIt's also not stored in the savefile so it's good. And you can access is relatively easy. You just have to manually prefix it if you use it in parallel events. That's the only disadvantage really.
What do you mean, you "can't prefix the event id"? @event_id (not event_id) in the interpreter means the current event's id.
This may interest you...

Code:
class Tester  def initialize    @local = {}  end    def local(key, value)    @local[key] = value    self.class.send(:define_method, key) do      value    end  endendtest = Tester.newtest.local(:one, 1)test.local(:two, 2)p test.onep test.two
anyway, why not just use a global variable? (aside from it denoted as $, which means 1 additional character)

Original thread: https://forums.rpgmakerweb.com/threads/local-variable-in-event.20861/