RMRK is retiring.
Registration is disabled. The site will remain online, but eventually become a read-only archive. More information.

RMRK.net has nothing to do with Blockchains, Cryptocurrency or NFTs. We have been around since the early 2000s, but there is a new group using the RMRK name that deals with those things. We have nothing to do with them.
NFTs are a scam, and if somebody is trying to persuade you to buy or invest in crypto/blockchain/NFT content, please turn them down and save your money. See this video for more information.
? SI unit 'if 10,100,1000...'dec,hec,kilo..' etc. list?

0 Members and 1 Guest are viewing this topic.

***
Rep:
Level 82
We learn by living...
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

*
Rep:
Level 97
2014 Most Unsung Member2014 Best RPG Maker User - Engine2013 Best RPG Maker User (Scripting)2012 Best Member2012 Best RPG Maker User (Scripting)2012 Favorite Staff Member2012 Most Mature MemberSecret Santa 2012 ParticipantProject of the Month winner for July 20092011 Best Use of Avatar and Signature Space2011 Best RPG Maker User (Scripting)2011 Most Mature Member2011 Favourite Staff Member2011 Best Veteran2010 Most Mature Member2010 Favourite Staff Member
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.
« Last Edit: November 01, 2011, 11:52:35 PM by modern algebra »

*
Rep:
Level 82
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.
(Why do I always feel like it's the end of the world and I'm the last man standing?)

***
Rep:
Level 82
We learn by living...
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.

*****
Rep:
Level 84
This text is way too personal.
Bronze - GIAW 11 (Hard)Silver - GIAW Halloween
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]

*
Rep:
Level 97
2014 Most Unsung Member2014 Best RPG Maker User - Engine2013 Best RPG Maker User (Scripting)2012 Best Member2012 Best RPG Maker User (Scripting)2012 Favorite Staff Member2012 Most Mature MemberSecret Santa 2012 ParticipantProject of the Month winner for July 20092011 Best Use of Avatar and Signature Space2011 Best RPG Maker User (Scripting)2011 Most Mature Member2011 Favourite Staff Member2011 Best Veteran2010 Most Mature Member2010 Favourite Staff Member
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.