Jump to content
New account registrations are disabed. This website is now an archive. Read more here.
  • 0
Polraudio

String Variables Instead of ID#?

Question

Ok so i made  a game_variables class called game_rep to track the players reputation with each npc in the game. I was wondering how i can use text as the id instead of a number if you get what i mean.

EX: instead of using $game_rep[1] += 1 i want $game_rep["Lana"] += 1.

That way it will be easier to modify rep per npc instead of having to remember the id number for thousands of npc's.

If you want to see how i have the script its really simple. Its just a name change of the default variables.

 

class Game_Rep
  #--------------------------------------------------------------------------
  def initialize
    @data = []
  end
  #--------------------------------------------------------------------------
  def [](variable_id)
    if variable_id <= 5000 and @data[variable_id] != nil
      return @data[variable_id]
    else
      return 0
    end
  end
  #--------------------------------------------------------------------------
  def []=(variable_id, value)
    if variable_id <= 5000
      @data[variable_id] = value
    end
  end
end

Thanks in advance :D

Share this post


Link to post
Share on other sites

4 answers to this question

Recommended Posts

  • 0

You have to declare your data holder as a Hash rather than an Array. For this you should write

@data = {}

 

Then the syntax is eactly the same.

@data["stuff"] = 10
@data["stuff"]    # Recall 10

 

Also, if you want to declare a Hash with contents from the start:

@data = { "stuff" => 10, "more stuff" => 20, "yet more stuff" => 30 }

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...