The RPG Maker Resource Kit

RMRK RPG Maker Creation => RPG Maker General => General Scripting => Topic started by: shintashi on November 01, 2011, 09:07:14 PM

Title: ? SI unit 'if 10,100,1000...'dec,hec,kilo..' etc. list?
Post by: shintashi on November 01, 2011, 09:07:14 PM
How do I make an SI unit 'if 10,100,1000...'dec,hec,kilo..' etc. list, in blocks of 1000? I'm trying to add labels to my encumbrance/max lifting lists, but they get exponentially larger, so creatures like dragons and giant robots can move tons, or even kilotons of stuff (useful if you are doing a 'transport' game with things like 'ship capacity' and get paid by the weight of the cargo).

So basically I want to do kiltons, megatons, gigatons, and so on, but I don't want a huge list of if then else, and i don't know how to make something similar to it with case. My goal is to take up very little space.


Code: [Select]
  label = @actor.enc > 1000 ? "tons" : "kg"
  div_max = @actor.enc > 1000 ? @actor.enc/1000 : @actor.enc
Title: Re: ? SI unit 'if 10,100,1000...'dec,hec,kilo..' etc. list?
Post by: modern algebra on November 01, 2011, 11:50:17 PM
Why does it matter how much space you take up? Just put it in its own conversion method which takes a number and returns a string with the appropriate unit. Something like this would be fine:

Code: [Select]
  def metric_convert (x)
    if x > 1000
      return sprintf("%.2f kg", x / 1000.0)
    elsif x > 100
      return sprintf("%.2f hg", x / 100.0)
    else
      return "#{x} g"
    end
  end

That would just be grams to kilograms, but you could extend it.

Where you need to get the converted number, you could just call that method and pass the number you want to convert.
I'm really tired right now, so maybe I missed a method that would be faster and more efficient, but I don't really see why you care about how many lines it takes up. It would still process pretty fast.
Title: Re: ? SI unit 'if 10,100,1000...'dec,hec,kilo..' etc. list?
Post by: LoganF on November 02, 2011, 01:03:46 AM
As long as you have a method that you can call any time you need to calculate which label to use, like MA says, then your likely doing the best you can do in terms of saving space.

Sometimes you can't avoid having a huge list of statements to check. Writing that huge list once, though, is better than writing it again every time you wanted to make a conversion.

No matter what approach you take you have to separate out the situations in which each tag is used and the simplest way I can think of is if/elsif.
Title: Re: ? SI unit 'if 10,100,1000...'dec,hec,kilo..' etc. list?
Post by: shintashi on November 02, 2011, 09:27:25 PM
is there a way I can write it (the table) once and access it outside my class? I want to be able to use it primarily in Window_Status but since it's about items, it's going to end up popping all over the place - shops, item list, equipment, etc.


I'm thinking something like "Window_Status.my_method(x)".

if not, that's cool. I can use the elsif method.
Title: Re: ? SI unit 'if 10,100,1000...'dec,hec,kilo..' etc. list?
Post by: cozziekuns on November 02, 2011, 09:45:25 PM
The best way to do it is probably to encapse it in a module. E.g:

Code: [Select]
module Metric
 
  def self.function(arg)
    #do blah
  end

end

And then you can call it with:

Code: [Select]
Metric.function(arg)
[code]
Title: Re: ? SI unit 'if 10,100,1000...'dec,hec,kilo..' etc. list?
Post by: modern algebra on November 02, 2011, 10:17:55 PM
Well, it sounds like all the places you would need to use it are in windows, so even putting it in Window_Base might be fine.