Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

Database Flag Reader (Scripting Tool)

Database Flag Reader
v1.2

Introduction


Another very little script, and the first real scripting tool of mine. It's purpose is to practically use the note fields in the database, effectively returning it's contents to a hash. Check the instructions section for a more in-depth explanation of that.
Bottom line is: All of us who are using element tags to set non-default aspects to database elements can now not only choose between true or false (element_set.include? boolean), but also assign any integer, string or array you want for further usage.


Features


  • Returns any requested, predefined object in string, integer, array, hash, boolean, float or nil form
  • Works with all note-capable database tabs (Skills, Items, Weapons, Armors, Enemies, States)
  • Allows default values for easier setup and reduced error probability 1.2 and above


Script


[rgss]#==============================================================================
# Database Flag Reader                                                     v1.2
#------------------------------------------------------------------------------
# Script by BlueScope
#==============================================================================
 
module RPG
  #--------------------------------------------------------------------------
  DefaultFlags = {}
  #--------------------------------------------------------------------------
  class BaseItem
    def flags(identifier)
      return RPG.flaglist(@note, identifier)
    end
  end
  #--------------------------------------------------------------------------
  class Enemy
    def flags(identifier)
      return RPG.flaglist(@note, identifier)
    end
  end
  #--------------------------------------------------------------------------
  class State
    def flags(identifier)
      return RPG.flaglist(@note, identifier)
    end
  end
  #--------------------------------------------------------------------------
  def self.flaglist(note, identifier)
    elements = note.split(/\r\n/)
    flags = {}
    for i in 0...elements.size
      temp = elements.to_s.split(' = ')
      flags[temp[0]] = RPG.decrypt_string(temp[1])
    end
    if flags.include?(identifier)
      return flags[identifier]
    elsif DefaultFlags.include?(identifier)
      return DefaultFlags[identifier]
    else
      return nil
    end
  end
  #--------------------------------------------------------------------------
  def self.decrypt_string(value)
    if value.to_i != 0 or value == '0'
      # integer and float
      value.include?('.') ? result = value.to_f : result = value.to_i
    elsif value == 'true'
      # boolean true
      result = true
    elsif value == 'false'
      # boolean false
      result = false
    elsif value.include?('[') and value.include?(']')
      # array
      result = []
      value.delete!('[ ]')
      array = value.split(',')
      for i in 0...array.size
        result = decrypt_string(array)
      end
    elsif value.include?('{') and value.include?('}')
      # hash
      result = {}
      value.delete!('{ }')
      hash = value.split(',')
      for i in 0...hash.size
        temp = hash.split('=>')
        result[temp[0]] = decrypt_string(temp[1])
      end
    elsif value == 'nil'
      # nil
      result = nil
    else
      # string
      result = value
    end
    return result
  end
  #--------------------------------------------------------------------------
end
[/rgss]


Instructions


To use this script, you have to do two things: Pre-set flags with values in the database, and trigger those from somewhere in your script library. You can't possibly change anything in your game with this script alone, hence it's a scripting tool. In other words, you have to script whatever script be working with this by yourself, it's just a bridge from the Database to the scripts editor.

You can set flags easily by following this layout:

Code:
FlagName = value

FlagName can be anything really, but I'd suggest using descriptive names for your further reference.
value can be any
  • string (without quotation marks of any kind),
  • integer (again without quotation marks, as usual),
  • float (no quotation marks, and of course needs the "." there or else it'll be recognized as integer),
  • boolean ("true" or "false", no quotation marks),
  • array (use same rules as before for contents),
  • hash (use typical "key => value" addression, again, same rules as above for both key and value),
  • or nil (just the word, no quotation marks).

Here's a practical example of how the note area in your database would look like:

noteg.png


This would return the following hash:

Code:
{

"Test"=>true,

"Health"=>600,

"Prerequisite"=>"FACT",

"OriginMaps"=>[2, 4, 5],

"HouseTeam"=>["Taub", 13, "Kutner"],

"Equip" => {"Sword" => "Dragonblade", "Armor" => "Golden Plate Armor"},

"Time" => 30.5,

"Empty" = nil

}


To check for either of those flags, you can use the regular way, using a reference to the game element, as well as the method identifier, with the flag name as an additional attribute.

Code:
$data_items[1].flags('OriginMaps')


You can also set default values to not input generic values into every single item note, for example. So if you let the majority of your items have UseCount = 1, for example, put this in the DefaultFlags constant instead:

Code:
DefaultFlags = {'UseCount' => 1}
Whenever you don't define a value for UseCount in your database now, every referencing script will automatically assume it's set to 1 (Integer) when there's no respective Database entry. Note that now you have to use proper syntax for the key (no quotation marks needed in database edit).


FAQ


Q: Why is this script VX-only?
A: RMXP doesn't feature note fields in the database, which is why a XP version wouldn't make sense.


Compatibility


Unless any other scripts have the same names as the ones in my script, it's perfectly compatible, as it's a simple add-on - no modified methods.


Author's Notes


I'm personally using this for my newly developed (unreleased) equip system, because - as mentioned in the introduction - just a boolean value isn't enough customization sometimes. It's a neat little add-on that won't hurt performance even if you just use it for a single item... though of course, you could most likely get there some other way in that case ;)


Thanks


...to Devlin for pointing out a major bug, along with the solution, which has been fixed for v1.2


Terms and Conditions


You may use and/or modify this script in any way you want, and/or release your modifications, as long as you accept the following terms and conditions entirely.
You may not use this for commercial projects, profit-oriented projects, or projects that aren't meant to be sold, but otherwise aquire money (i.e. through donations). That is not limited to RPG Maker projects, but valid for all appliances. You may not remove or delete contents of the original header included in the script, but you may add your name if you made any changes.

The actual mentioning of my nickname in your release (i.e. in-media credits, printed pages or even a readme file) is not necessary, but much apprechiated.
 

Twirly

Sponsor

This sure is interesting, it will make the life of many scripters easier
and if it will be creatively used, there will be some nice results.
Good job BlueScope :thumb:
 
Interesting script Scope, however I have a single question for you. Would I be able to make an effect like Attack*4 on a single line, where the script would reach each individual item (Attack, *, and 4) seperately? or would I have to make each effect a different value read?
 
I don't quite see what you're trying to do here, but I figure it's about multiple attacks... what you could do is make something like AttackCount = anInteger, and then have your battlesystem execute the basic attack algorythm for just as much times, using for example:

Code:
for i in 0..@actor.weapon.flags('AttackCount')
  # attack method goes here
end

It's pretty simple and useful, if you ask me... ^^
 
A curious method.  Notably less bulky than what I had done, although I see that the function is similar only on the most superficial level.  Dependent on on the needs of the user, I'd recommend yours over what I did in many situations.
 
Pretty neat, I think I can convert my script with this with a method name
Algorithm = [Strength, %, 300] which would return a skill that does damage equal to 300% of the users attack.

Sorry for the question for for that method HouseTeam, how would I set up the script to read it like that? Right now the method I am thinking about using might not work
 
@Drako: I haven't seen yours, to be honest... if you submitted it, would you mind pointing me there for reference? ^^

@Twi: Depending on how you want the % there to work, you need to adjust it. By default, it's read as ["Strength", "%", 300], having two strings and one integer. Now if the integer there is always %, you don't need that one at all, if you wanna change between % and actual add, you should include a switch, aka formating it by [attribute, effect, amount], looking like something like this: ["Strength", 1, 300]

Now what you'd need to do within your script is doing something like this:

Code:
value = skill.flags('Algorythm')[0]
case value
when "Strength"
  # ...
...

Code:
value = skill.flags('Algorythm')[1]
case value
when 0
  # add the amount to the currect value
when 1
  # calculate the %-wise value of current value with amount as percent
...

And so on, you get the idea... basically, it'd be smarter to use seperate flags, though, because they're easier to work with than arrays, and you have less trouble converting. I suggest you use something like this:

Code:
Attribute = Strength
Type = 1
Amount = 300

That method also means easier customization, because you can without much hassle add default values within your scripts, so the user won't have to set the whole array, but can also leave 'Attribute' and 'Type' out if he or she wants to use your default-given value. Of course, you have to set those within your conditionals.
 
Can you implement symbols as well?
Would be useful for example to store methods name
to use later like this : myobj.method(mysymbol).call(myargs)

As their also unique identifiers, you could use symbols in your hash instead of strings. as it's just a symbol takes less memory than a string, making it faster to call.(almost twice faster)
thehash[:Test] instead of thehash["Test"]... Very easy to convert from or into a symbol 'my_string'.to_sym => :my_string :my_string.to_s => 'my_string'
or :my_string.inspect => ':my_string'

(just have a go and test this :
a = { :Test => 3 , "Test" => 4}
t = Time.now; 1000000.times { a[:Test] } ; p Time.now - t

t = Time.now; 1000000.times { a["Test"] } ; p Time.now - t
)
 

Devlin

Member

Hey, you made a mistake.

When I was trying to draw a luck amount from the weapon, the game crashed. It also crashed with the armor item. It told me the flags did not exist. According to your topic, I can draw the flaglist from skills, weapons and armor. So I went to take a look at the script.

You wrote "Item", it should be "BaseItem" When I changed it to BaseItem, weapons/armors/skills now works for me.
 
Lemme bump this to tell ya all I've minorly updated this script for improved functionality... while it's only three lines of new code and a changed version number, it's quite an effectful change: Default values for all flags you want to use. Check the updated instruction block for further information.
 
Nice script.
Just... may I suggest the use of the 'eval' function instead of the 'decrypt_string' method. It should help avoiding a lot of complex computations, and handle all classes as well as functions/method calls and other (un-)imaginable dynamic Ruby code execution. I once read something about undetermined programing, or something like that ('non-abouti' in french): the art of writing programs that overwrite themselves.... :crazy: :cute:
 
The problem with using eval, while being aware of all the advantages, is that it's likely to spawn mistakes compared to this method. It's like taking a text field for any kind of database input instead of the determined data, number-only or whatever fields: The user may ut stuff in in the desired way, or he may put in whatever he wants, spawning errors that result in an error that is - if i'd use the eval function - the fault of the script, not his. My method, however, decides between inputs effectively and considers everything a string that doesn't match anything else, not resulting in an error from the script's side. Admittedly, both ways, the user has to know what they're doing, but I still prefer this method, as for me, it's the clearer and more straight-to-the-pont approach. Also, the method in question isn't memory-or-time-expensive at all, which therefore doesn't result in a loss of performance too much (actually, unless you check a list of items a bazillion times, it shouldn't even be noticeable).
However, as the terms and conditions state, you're free to modify this to your projects needs, so feel free to replace the method in question with an eval command... this is a scripting tool, and therefore meant to be useful to the scripter who uses it.
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top