Ensuring an aliased method exists

Shaz

Veteran
Veteran
Joined
Mar 2, 2012
Messages
40,098
Reaction score
13,704
First Language
English
Primarily Uses
RMMV
When you have many scripts to share, and they all read notes from database items, how do you write your scripts so the methods that read the notes don't overwrite each other? With a normal class method, you'd just alias it each time. But if it's a new method that hasn't been defined in the RPG:: classes, there's nothing to alias.


I mean, when you write them in a way that the notes are loaded at the same time as the database is loaded, and save the data into variables, not so that they're looked at every time you want to check if an item has particular contents in its notes. Like a read_notes method that's called on each object after the $data_* classes are loaded.


I'm thinking of making a script that just defines the method for each class but leaves it empty, and calls them when the database is loaded. Then each script would alias the method. But that means when scripts are shared, the user would need to grab both - the script that they WANT, plus the one that defines the read_notes method.


Or do you just not do it that way, and have every script scan through the notes when and where it wants to know if the specified tag exists?
 
Last edited by a moderator:

FenixFyreX

Fire Deity
Veteran
Joined
Mar 1, 2012
Messages
434
Reaction score
310
First Language
English
Primarily Uses
Usually I go with defined?(method_name), like so:

Code:
class RPG::Blah  def read_notes    note.scan(SomeRegex).flatten  end unless defined?(read_notes)end
You can also use unless RPG::Blah.instance_methods.include?:)read_notes).
 

Shaz

Veteran
Veteran
Joined
Mar 2, 2012
Messages
40,098
Reaction score
13,704
First Language
English
Primarily Uses
RMMV
But if you have several scripts that all have read_notes, doesn't that mean only the first (or the last) will actually run what's in its method?
 

Tsukihime

Veteran
Veteran
Joined
Jun 30, 2012
Messages
8,564
Reaction score
3,846
First Language
English
I've done ugly things like

1. Alias if it exists

2. Call alias if the alias exists

Code:
class Base  alias :first_notes :notes if method_defined?(:notes)  def notes    first_notes if respond_to?(:first_notes)    p "test"  end  endclass Base  alias :second_notes :notes# if method_defined?(:notes)  def notes    second_notes if respond_to?(:second_notes)    p 'more notes'  endend
Don't know if there's a cleaner way to do it.
 
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
That's what I'm after. Thanks :)


What's the if method_defined?:)notes) commented out for in the second group? Can it be done either way, or is there a difference between the two?
 

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
how do you write your scripts so the methods that read the notes don't overwrite each other?
Personally, I just prefix them with the name of the script or part of the name... XD


that's for public resource... on my personal proj, I made a tag and feature manager so that I can keep the note loading to those two scripts only... I'm not sure if it's worth making the public scripts require a tag/feat manager...
 
Last edited by a moderator:

Tsukihime

Veteran
Veteran
Joined
Jun 30, 2012
Messages
8,564
Reaction score
3,846
First Language
English
Personally, I just prefix them with the name of the script or part of the name... XD
You can't alias methods that don't exist already, which is a problem when you're introducing your own methods in your own scripts, but the order that the scripts are placed does not matter.
 
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
prefixing them means each script would get its own method, and if you have a ridiculous 50 scripts that all read notes from the database, wouldn't that mean going through each entry in the database 50 times, each time reading all notes, looking for the specific ones to that script?


I guess what I'm thinking of is one script that should be distributed each time (maybe note it as a "Required Script") that will loop through all the objects, reach the notes, and for each note line, call a method that checks if a certain string is in that line. Then provide a dummy, empty method for each object type, which will be aliased by each script. So if I have 50 scripts that all ready notes from the database, it's only looping through the database entries once, and only looping through the notes for each entry once, but every script will get a look-in at the individual note lines and be able to grab its data, if it exists.
 

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
@Tsukihime - I don't mean aliasing them with the prefix, it's like Shaz's reply, it would be a new method everytime...


@Shaz - well yes, that will do it for every script... Which I think makes a single Note Manager script nice... basically the method just loads each line that is a feature (you specify how to know if it's a feature, something like feature:name:value), then save value into a hash using name as key...
 

Shaz

Veteran
Veteran
Joined
Mar 2, 2012
Messages
40,098
Reaction score
13,704
First Language
English
Primarily Uses
RMMV
Yeah, I'm more wanting to avoid repeated passes through the notes for each script. I'd rather something that would read through it once, then let each script take a peek and see if there's any data that is useful for that particular script, but without having each script loop through all of the notes.


Or, really, the original question was about how script B can alias the method, when script A may or may not have defined it in the first place, and if it did define it, making the method run in both scripts. I think the method_defined check will do it nicely.
 

Tsukihime

Veteran
Veteran
Joined
Jun 30, 2012
Messages
8,564
Reaction score
3,846
First Language
English
I guess what I'm thinking of is one script that should be distributed each time (maybe note it as a "Required Script") that will loop through all the objects, reach the notes, and for each note line, call a method that checks if a certain string is in that line. Then provide a dummy, empty method for each object type, which will be aliased by each script. So if I have 50 scripts that all ready notes from the database, it's only looping through the database entries once, and only looping through the notes for each entry once, but every script will get a look-in at the individual note lines and be able to grab its data, if it exists.
That sounds like something that would only work if you used very simple note-tags.

For example

<tag: value>or something.If I suddenly wanted to use multi-line tags like

Code:
<tag>  opt1: value1  opt2: value2</tag>
That would probably make things more difficult.
 

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
you could still save each one separately... though for things like Mr. Bubble's Tactics ogre crafting, this might make things unnecessarily more complicated... unless you make it such that multilines are saved as an array/hash inside the original hash...
 
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 - I only use complete tags, not opening/closing with other stuff in between.
 

Tsukihime

Veteran
Veteran
Joined
Jun 30, 2012
Messages
8,564
Reaction score
3,846
First Language
English
you could still save each one separately... though for things like Mr. Bubble's Tactics ogre crafting, this might make things unnecessarily more complicated... unless you make it such that multilines are saved as an array/hash inside the original hash...
The problem I see is how you are supposed to distinguish between all the different types of note-tag formats.

These are the ones I use frequently

Compact tag with a single value:

Code:
<tag: a>
Compact tag with multiple values delimited by a specified delimiter (eg: white space)

Code:
<tag: a b c>
Extended tag with values in the opening tag, and non-distinct options in the body

Code:
<tag: a b c>  opt1: 1  opt2: 2  opt2: 3</tag>
Nested tags:

Code:
<tag: a b c>name: some nameid: some id  <tag: formula>     if this        a     else        b     end  </tag: formula></tag>
Of course, this assumes you use XML style tagging. Some people use very simple syntax like just having a particular character before the tag name

Code:
/tag: value
Others might use JSON or any other serialization format of their choice.
 
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
Yes, this would be difficult if you were doing it like that. I'm only asking this for my own purposes though, for my own scripts, which all contain everything needed in a single line (like your first two examples), not extended or nested (like your third and fourth examples).


I'm not intending to write a tool for other scripters here - I'm just after something to make it easy to share my own multiple scripts that read the note tags, without having them all loop through the same things or stopping other scripts (of mine) from getting their notes.
 

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

Latest Threads

Latest Posts

Latest Profile Posts

Couple hours of work. Might use in my game as a secret find or something. Not sure. Fancy though no? :D
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'??

Forum statistics

Threads
105,864
Messages
1,017,056
Members
137,573
Latest member
nikisknight
Top