Author Topic: Do 'game' functions like game:get_max_life() truncate decimals?  (Read 462 times)

Kamigousu

  • Newbie
  • *
  • Posts: 17
    • View Profile
Was working on stats for a new game and noticed that some of the resources like health and stamina were not coming out to their expected value, but rather they were rounded up or down depending on the following decimal. 100.1 becomes 100, 89.97 becomes 90.

I found that other values I printed were coming out with all decimal places intact though these values weren't called through any 'game' function. It mostly hasn't been a problem until I needed to use a multiplier that scales between 1.01 and 1.9999999. Calling the multiplier through the 'game' function I made for it truncates and rounds the number to either 1 or 2. It's causing a little bit of a problem with a few things but nothing game breaking.

Any one know of a work around or a way to get 'game' functions to print out full number values? I have tried a few things but nothing has worked out yet.

llamazing

  • Full Member
  • ***
  • Posts: 187
    • View Profile
Re: Do 'game' functions like game:get_max_life() truncate decimals?
« Reply #1 on: March 30, 2019, 11:59:22 pm »
I'm having a difficult time following what you are trying to do here. Why do you want to store the multiplier value in a 'game' function like the player's life or stamina?

You should be able to do something like the following that won't truncate the multiplied value:

Code: (lua) [Select]
local multiplier = 1.25
local multiplied_value = game:get_max_life()*multiplier

You're probably going to have to post your code if you want better help than this.

Kamigousu

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: Do 'game' functions like game:get_max_life() truncate decimals?
« Reply #2 on: March 31, 2019, 12:25:33 am »
Sorry, I will try to explain better. There are a few scripts that work with this so I will post them if this post doesn't get across what I'm talking about.

I'm working on an RPG that is supposed to be a mix of Dark Souls 3 and ALttP. It has lots of parameters like Strength, Endurance, and Vigor and some of these parameters are added or multiplied together to define other parameters.

My current issue lies in determining my hero's vitality level. Vitality level depends on three factors: Endurance level, Strength level, and Vigor level.

(strength * 2) + (vigor * endurance multiplier) = vitality

So the parameters (Strength, Vigor, Endurance) all have min and max values (1 - 99). For Strength and Vigor, all I need are the levels; so if my Strength level is 45, I use it in the first part as (45 * 2). However, the Endurance Multiplier is calculated using the Endurance level and its min and max values are 1.01 up to 1.9999999; so if my Endurance level is 25 then my Endurance Multiplier would be 1.340.

This is where the problem lies. I have functions to get,set,add,remove,and calculate all these values but when I call the functions, like game:get_end_mult() (to get my endurance multiplier, obviously), the decimals are removed from the value leaving it as 1 or 2 depending on how large the decimal portion was. I'm currently working on trying the same thing but with the hero metatable instead of the game_metatable. My first test seemed to work; the value printed was the full decimal value that I had typed in.

I was just hoping to be able to integrate all the stats and params into the game table...I suppose its not really necessary for anything besides health, stamina, and magic. I made this post mostly because I am curious as to what the reason is for the game table truncating values.

llamazing

  • Full Member
  • ***
  • Posts: 187
    • View Profile
Re: Do 'game' functions like game:get_max_life() truncate decimals?
« Reply #3 on: March 31, 2019, 03:51:34 am »
It is totally possible to do what you want.

I'm not familiar with the game:get_end_mult() function. Is that a custom function you defined yourself? If so then you must be doing something that's truncating the value, which should be fixable since it is your function.

Please post your game:get_end_mult() function. I'm guessing the problem will be obvious.

llamazing

  • Full Member
  • ***
  • Posts: 187
    • View Profile
Re: Do 'game' functions like game:get_max_life() truncate decimals?
« Reply #4 on: March 31, 2019, 04:18:55 am »
Okay, so thinking about your problem some more, I have a feeling that your problem is that using game:set_value() and game:get_value() is what's truncating your numbers.

If that's the case then you can do something like the following:

Original code that gets truncated
Code: (lua) [Select]
local function set_multiplier(value)
  game:set_value("multiplier", value)
end

local function get_multiplier()
  return game:get_value("multiplier")
end

How to fix it:
Code: (lua) [Select]
local function set_multiplier(value)
  game:set_value("multiplier", tostring(value))
end

local function get_multiplier()
  return tonumber(game:get_value("multiplier"))
end

So this way you'd be storing the values as a string that includes the decimal portion and it won't get truncated.

Kamigousu

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: Do 'game' functions like game:get_max_life() truncate decimals?
« Reply #5 on: March 31, 2019, 10:48:53 am »
The function mentioned is one I defined however it doesn’t have anything in it that would truncate the decimal.

I think what you have posted may just work. I was trying tostring and tonumber but I don’t think I was placing them correctly for the desired effect. I will try it the way you have said and see if it helps.

It beats my original plan: change everything over to hero_meta functions. That way, my get/set_value becomes get/set_property which doesn’t work as expected ( although I suspect I haven’t properly changed al of my scripts to match the new hero_meta versus the old game_meta setup).

I’ll post back tomorrow and hopefully will have some successful results.

Diarandor

  • Hero Member
  • *****
  • Posts: 1045
  • Cats are cool! (ΦωΦ)
    • View Profile
Re: Do 'game' functions like game:get_max_life() truncate decimals?
« Reply #6 on: March 31, 2019, 11:28:09 am »
Why don't you just keep a function getEnduranceMultiplier() instead of saving the current value? IMO, you don't need to save that value.
“If you make people think they're thinking, they'll love you. But if you really make them think, they'll hate you.”

Kamigousu

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: Do 'game' functions like game:get_max_life() truncate decimals?
« Reply #7 on: March 31, 2019, 10:15:17 pm »
You're saying to just use the function to calculate the number anytime it is needed? I suppose I could, I just thought it would be better to save it; not sure why.

Kamigousu

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: Do 'game' functions like game:get_max_life() truncate decimals?
« Reply #8 on: March 31, 2019, 10:41:41 pm »
As llamazing suggested, the tostring and tonumber option worked out perfectly. I had tried that but I put them in the wrong place to get the expected results. Thank you for the helpful tip, llamazing.

@Diarandor: I ended up setting it up to calculate the value whenever you call it but it still saves the value (for now) because I'm not sure if there will items that directly affect the multiplier (without affecting Endurance). So a little of both solutions helped here.

I appreciate this community for being so helpful; this post wasn't even about getting help but you helped me reconfigure my scripts to work properly. Do either of you know, by chance, why it is that game values are truncated? That was my main reason for posting. I wasn't sure if anyone knew.