Local variable in event?

Status
Not open for further replies.

Napoleon

Veteran
Veteran
Joined
Dec 29, 2012
Messages
869
Reaction score
97
First Language
Dutch
Primarily Uses
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.
 

Zeriab

Huggins!
Veteran
Joined
Mar 20, 2012
Messages
1,268
Reaction score
1,422
First Language
English
Primarily Uses
RMXP
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*
 

Tsukihime

Veteran
Veteran
Joined
Jun 30, 2012
Messages
8,564
Reaction score
3,846
First Language
English
The scope of an event command is that event command itself.
 
Last edited by a moderator:

Andar

Veteran
Veteran
Joined
Mar 5, 2013
Messages
31,359
Reaction score
7,672
First Language
German
Primarily Uses
RMMV
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
 
Last edited by a moderator:

Napoleon

Veteran
Veteran
Joined
Dec 29, 2012
Messages
869
Reaction score
97
First Language
Dutch
Primarily Uses
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 ;)
 

Shaz

Veteran
Veteran
Joined
Mar 2, 2012
Messages
40,098
Reaction score
13,704
First Language
English
Primarily Uses
RMMV
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.
 

Tsukihime

Veteran
Veteran
Joined
Jun 30, 2012
Messages
8,564
Reaction score
3,846
First Language
English
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...
 
Last edited by a moderator:

Napoleon

Veteran
Veteran
Joined
Dec 29, 2012
Messages
869
Reaction score
97
First Language
Dutch
Primarily Uses
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).
 
Last edited by a moderator:

Shaz

Veteran
Veteran
Joined
Mar 2, 2012
Messages
40,098
Reaction score
13,704
First Language
English
Primarily Uses
RMMV
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.
 

Tsukihime

Veteran
Veteran
Joined
Jun 30, 2012
Messages
8,564
Reaction score
3,846
First Language
English
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.
 
Last edited by a moderator:

Napoleon

Veteran
Veteran
Joined
Dec 29, 2012
Messages
869
Reaction score
97
First Language
Dutch
Primarily Uses
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.
 
Last edited by a moderator:

Tsukihime

Veteran
Veteran
Joined
Jun 30, 2012
Messages
8,564
Reaction score
3,846
First Language
English
I don't think that is supported.

Things like this wouldn't be valid method definitions

Code:
def local[key] # square brackets?
 
Last edited by a moderator:

Shaz

Veteran
Veteran
Joined
Mar 2, 2012
Messages
40,098
Reaction score
13,704
First Language
English
Primarily Uses
RMMV
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
 

Napoleon

Veteran
Veteran
Joined
Dec 29, 2012
Messages
869
Reaction score
97
First Language
Dutch
Primarily Uses
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...
 
Last edited by a moderator:

Shaz

Veteran
Veteran
Joined
Mar 2, 2012
Messages
40,098
Reaction score
13,704
First Language
English
Primarily Uses
RMMV
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?
 
Last edited by a moderator:

Tsukihime

Veteran
Veteran
Joined
Jun 30, 2012
Messages
8,564
Reaction score
3,846
First Language
English
You can't define a setter method that takes more than one argument.
 

Napoleon

Veteran
Veteran
Joined
Dec 29, 2012
Messages
869
Reaction score
97
First Language
Dutch
Primarily Uses
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.
 
Last edited by a moderator:

Shaz

Veteran
Veteran
Joined
Mar 2, 2012
Messages
40,098
Reaction score
13,704
First Language
English
Primarily Uses
RMMV
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.
 

Tsukihime

Veteran
Veteran
Joined
Jun 30, 2012
Messages
8,564
Reaction score
3,846
First Language
English
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
 
Last edited by a moderator:

Engr. Adiktuzmiko

Chemical Engineer, Game Developer, Using BlinkBoy'
Veteran
Joined
May 15, 2012
Messages
14,682
Reaction score
3,003
First Language
Tagalog
Primarily Uses
RMVXA
anyway, why not just use a global variable? (aside from it denoted as $, which means 1 additional character)
 
Status
Not open for further replies.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Posts

Latest Profile Posts

Holy stink, where have I been? Well, I started my temporary job this week. So less time to spend on game design... :(
Cartoonier cloud cover that better fits the art style, as well as (slightly) improved blending/fading... fading clouds when there are larger patterns is still somewhat abrupt for some reason.
Do you Find Tilesetting or Looking for Tilesets/Plugins more fun? Personally I like making my tileset for my Game (Cretaceous Park TM) xD
How many parameters is 'too many'??
Yay, now back in action Happy Christmas time, coming back!






Back in action to develop the indie game that has been long overdue... Final Fallacy. A game that keeps on giving! The development never ends as the developer thinks to be the smart cookie by coming back and beginning by saying... "Oh bother, this indie game has been long overdue..." How could one resist such? No-one c

Forum statistics

Threads
105,857
Messages
1,017,019
Members
137,564
Latest member
McFinnaPants
Top