Author Topic: A question (mainly) about storing percentage in variables  (Read 508 times)

Legacy_Grani

  • Hero Member
  • *****
  • Posts: 1040
  • Karma: +0/-0
A question (mainly) about storing percentage in variables
« on: August 31, 2013, 08:30:46 pm »


                Hello there,
I'm working on a trade system and part of it is randomization of price of commodities during selling them.
I'll refer to leather as an example of such a commodity.

In short, I wanted a local integer called "pLeather" and equal to d100 be put on the area in which NPC buyer is located.
Then, if a player tried to sell leather, the selling price would become equal to the base price of leather multiplied by value of ["pLeather" divided by 50], plus the base price multiplied by [PC's Appraise skill divided by 20].

A bit chaotic, I know, but I hope you can get the picture.

The thing is, for it to work I would need to use integers of fraction values, which, well, are not integers anymore.

So my question is - how can I store percentages, which are in fact fractions, in variables?
Thank you for help. :happy:
               
               

               
            

Legacy_Squatting Monk

  • Hero Member
  • *****
  • Posts: 776
  • Karma: +0/-0
A question (mainly) about storing percentage in variables
« Reply #1 on: August 31, 2013, 08:43:10 pm »


               Use floats. You can set floats in the toolset just like ints, and if you need to convert between variables types, use FloatToInt() or IntToFloat().
               
               

               
            

Legacy_Grani

  • Hero Member
  • *****
  • Posts: 1040
  • Karma: +0/-0
A question (mainly) about storing percentage in variables
« Reply #2 on: August 31, 2013, 08:53:30 pm »


               Ah, right! I didn't see this IntToFloat function before so I didn't know how to use floats with d100, but this makes things easier.

Thanks. '<img'>

Edit: One more thing, does it work for global integers only? Because it gives me compilation error when I try it on the integer on the area.
               
               

               


                     Modifié par Grani, 31 août 2013 - 07:57 .
                     
                  


            

Legacy_MagicalMaster

  • Hero Member
  • *****
  • Posts: 2712
  • Karma: +0/-0
A question (mainly) about storing percentage in variables
« Reply #3 on: August 31, 2013, 09:00:07 pm »


               Post your code.
               
               

               
            

Legacy_Grani

  • Hero Member
  • *****
  • Posts: 1040
  • Karma: +0/-0
A question (mainly) about storing percentage in variables
« Reply #4 on: August 31, 2013, 09:10:37 pm »


               Um, how do I put a script here without messing up its format? '<img'>

Anyway, I can post the first script for now, since I don't have the second one yet, for setting the final price based on the floats set in this one and PC's Appraise.

Edit: http://pastebin.com/sFiqRxFx
I think I got it, though, I just needed to set integers like this instead of using SetLocalInt. :happy:
               
               

               


                     Modifié par Grani, 31 août 2013 - 08:13 .
                     
                  


            

Legacy_The Amethyst Dragon

  • Hero Member
  • *****
  • Posts: 2981
  • Karma: +0/-0
A question (mainly) about storing percentage in variables
« Reply #5 on: August 31, 2013, 10:00:27 pm »


               If you want your floats to work as percentages, you'll want to divide the d100 results by 100 first, so you end up with a decimal value that's less than 1.

Right now, your code is storing the full numbers as floats.
Example: d100() = 53 ... IntToFloat(53) = 53.00 ...
use that as a percentage in a calculation and you're looking at 5300%

Instead, you can do something like this:
int pLeather = d100();
float fLeather = pLeather * 0.01; // multiplying by a float value gives you a float result
SetLocalFloat(oArea, "pLeather", fLeather);

Your price calculation then should look something like:

object oLeather; // your piece of leather to sell
obejct oPC; // your PC selling the leather
int nBasePrice = GetGoldPieceValue(oLeather);
int nNewPrice = FloatToInt((nBasePrice * GetLocalFloat(oArea, "pLeather")) + (nBasePrice * (GetSkillRank(SKILL_APPRAISE, oPC) * 0.05)));

You can post code here rather easily.  Just start it with [ code ] and end it with [ /code] (remove the spaces inside for it to work).
               
               

               
            

Legacy_Grani

  • Hero Member
  • *****
  • Posts: 1040
  • Karma: +0/-0
A question (mainly) about storing percentage in variables
« Reply #6 on: August 31, 2013, 10:29:53 pm »


               Thanks for the reply, though I think I got it working! '<img'>
Indeed, I could not use the floats as they were in the script I provided, but as I said, the system is based on two scripts. One for the randomization element (that's the one I showed here) and one for setting the final price of the item based on item's base price, randomization done earlier and PC's appraise skill.

I've performed the necessary operations on floats in the second script and from what I've tested so far, it works flawlessly.

That is:
-item's price is randomized to be equal between 2% and 200% of its base value
-an appraise bonus is added: the price increases 5% of the base price per 1 point in PC's appraise

Thanks for your help, guys. '<img'>
               
               

               


                     Modifié par Grani, 31 août 2013 - 09:30 .