Jump to content
TheCaliMack

Declare a NEW global variable (RGSS3)

Recommended Posts

You are very welcome! *sprinkles fairy dust on you*

Share this post


Link to post
Share on other sites

Adding more hashes would be as simple as...?

module DataManager
 
  #--------------------------------------------------------------------------
  # * Create Game Objects
  #--------------------------------------------------------------------------
  class << self
    alias create_game_objects_tablelist_base create_game_objects
  end
  def self.create_game_objects
    create_game_objects_tablelist_base
    $game_testhash = {}
    $newhash = {}
  end
  #--------------------------------------------------------------------------
  # * Create Save Contents
  #--------------------------------------------------------------------------
  class << self
    alias make_save_contents_tablelist_base make_save_contents
  end
  def self.make_save_contents
    contents = make_save_contents_tablelist_base
    contents[:testhash] = $game_testhash
    contents[:newhash] = $newhash
    contents
  end
  #--------------------------------------------------------------------------
  # * Extract Save Contents
  #--------------------------------------------------------------------------
  class << self
    alias extract_save_contents_tablelist_base extract_save_contents
  end
  def self.extract_save_contents(contents)
    extract_save_contents_tablelist_base(contents)
    $game_testhash = contents[:testhash]
    $newhash = contents[:newhash]
  end

end
Edited by AeghtyAteKees

Share this post


Link to post
Share on other sites

Yeah that would work... you could also make one hash that contained other hashes, like for example:

a_hash = {}
a_hash[:otherhash] = {}
a_hash[:otherhash][:value] = "a value"
That is more or less how my database script worked. Edited by KilloZapit

Share this post


Link to post
Share on other sites

Yeah, that's awesome. :) Just confirming that a large hash is okay, as well as several hashes. I'm not planning on them being unreasonably large, but several hundred values in a hash yields no negativity, does it?

Share this post


Link to post
Share on other sites

I don't think so... hashes are really fast for lookups, and I don't think memory is that much of a concern.

Share this post


Link to post
Share on other sites

If I need to modify something, but at any point in the game I can't tell if it's been defined and added to the hash, what's the best solution?

 

Something like this (always having to check) seems extraneous:

$testhash[:testmethod] = 0 if $testhash[:testmethod].nil?
#then operate:
$testhash[:testmethod] += 3

And so does pre-defining everything in the script.

Share this post


Link to post
Share on other sites

Actually I think the best way I have found is $testhash[:testmethod] ||= 0 because ||= is really really useful. It can be a bit hard to explain how it works but basically || is a shortcut or, meaning if the statement before it is true, return the statement, otherwise return the next statement. Anything that is nil is false by definition, so you can also use it as a check for that. And x ||= y is short for x || x = y.

 

One handy thing also is a = statement always returns the value set, so you can put it in () and it will just work in a larger statement. But you can't do (x ||= y) += z unfortunately because += only works if the statement directly before the += is something you can set a value to, so you aren't allowed to use () like that. But x = (x ||= y) + z will work. It's more helpful in statements like this:

 

 

if (x ||= 0) > 1
  #do something
end
Edited by KilloZapit

Share this post


Link to post
Share on other sites

So in my script commands, should I do this every time I need to increase a value?

$testhash[:testmethod] ||= 0
$testhash[:testmethod] += 1

Or use a condition?

 

OR just shorten the example above, because I'm sure that can be done somehow.

Edited by AeghtyAteKees

Share this post


Link to post
Share on other sites

You might want to declare a bunch of defaults when you create the hash, like for example:

$testhash = {
  :this => 0,
  :that => 1,
  :blah => "hey there"
}
But if you want to have values that you just add to the hash later, yeah ||= is the best way I have found. Edited by KilloZapit

Share this post


Link to post
Share on other sites

I guess I'll just add default values to the hashes because, while a bit tedious, I get lots of errors when trying to modify a nil value. Seems that only = can be used, which is for defining, and that other operators like <= or += make ruby mad.

 

I wish it wouldn't return an error and crash, but rather not do anything at all unless the method or variable or whatever had already been defined... :/

Edited by AeghtyAteKees

Share this post


Link to post
Share on other sites

Yeah operators like + - * / < > and so on are not defined for nil so they give an error. Another option you could use, if you are doing all numbers by default like $game_variables does, is to write your own wrapper class the same way the Game_Variables class is used for $game_variables to give most of them a default value.

Share this post


Link to post
Share on other sites

I didn't really want to be limited to just numbers, but now that I think about it, I haven't used anything except numbers so I suppose that'll do. :P

I don't know how to write my own wrapper class, though...

 

Also, while $game_variables has everything defaulted to 0, you can still set them as strings. And that's often useful. Would I still be able to do that in my hash?

Share this post


Link to post
Share on other sites

Sure! Here is a simple example of a type of wrapper class:

 

 

class Game_MyHash
 
  def initialize(default = nil)
    @data = default || {}
  end

  def [](key)
    @data[key] || 0
  end

  def []=(key, value)
    @data[key] = value
  end
 
end
 

Just like the Games_Variables class it makes a class that uses [] and []= but returns 0 rather then nil if something is empty... you can't use a lot of advanced hash commands this way but it works. Just use Game_MyHash.new and optionally a default hash like Game_MyHash.new({}) and it should work. It's pretty basic though.

Share this post


Link to post
Share on other sites

Alright. This is all working great.

 

I wonder where these values are being kept (or cached, if that's the right word?) Primarily because I'm using Modern Algebra's Message Codes, and this includes the code \#{ } which returns the value of a line of ruby, $game_system.save_count for example. If I enter code from the hash we've made, \#{$game_testhash[:testmethod]} for example, it always returns the value at startup time. Before our wrapper, all values were nil until defined, so even after definition this code would return nothing. Now with the wrapper, this code returns all values as 0.

 

Although I suppose it may be an issue with the message codes script itself...?

Share this post


Link to post
Share on other sites

Hmmm could you see if it changes right by changing it and printing the value to the console with puts?

Share this post


Link to post
Share on other sites

Sigh...I don't know what happened. It's working fine now and I consciously changed nothing. I think Ruby has a mind of its own. 

Share this post


Link to post
Share on other sites

Hehe, sometimes that happens... Sounds like a heisenbug!

Share this post


Link to post
Share on other sites

I'm trying to create my own \v[ ] message code (in MA's Message Codes script) to use with the hash we created, but I'm having some trouble with the following part.

result.gsub!(/\eEXAMPLE\[(\w+)\]/i)   { $example[:$1] }

When typing (as an example of course) /example[method] in a message window, a playtest returns 0 every time, presumably because the code in the curly brackets isn't correct.

 

Keeping in mind that I know very little about what I'm doing, haha (*sobs*), could you maybe help? :)

Share this post


Link to post
Share on other sites

I don't think putting : in front of a variable will work, I am pretty sure you can't convert to a symbol that way.  Use $1.to_sym to convert the string to a symbol instead. I think you are literally asking for a symbol called ":$1" there. I would have thought characters like $ would be illegal in symbol names but I guess not.

Edited by KilloZapit

Share this post


Link to post
Share on other sites

Thanks, that did the trick.

 

Also, thank you for being my personal scripting guru. I feel like I've overtaken this thread and it's getting off topic...Because I could keep asking you for help until the endtimes.

Edited by AeghtyAteKees

Share this post


Link to post
Share on other sites

I am glad I can help! *sprinkles fairy dust on you*

Share this post


Link to post
Share on other sites
Guest
This topic is now closed to further replies.

  • Recently Browsing   0 members

    No registered users viewing this page.

×
Top ArrowTop Arrow Highlighted