Hello, i have a trouble with one long variable that is returning weird results in every instance. I need long variable to get the amounts of resources that are on the planets of my space based game. And amounts are huge, even using the kT or dam3 trick at the end. For example, in real life, there is 800000000000 Tons of iron on Earth, wich is a small planet. The weird thing is that the long var works for all my resource but one, water, which do not have the biggest amount, and it’s not returning the negative max value, values returned are always negative but are not the same on every planet, it seems random…
The code is (US) :
private var randomSize : float;
var resourceWater : long;
So I really don’t understand what is wrong with that code (the result can’t be negative) and the weird thing is that i have another resource, Minerals, wich is 9500000000 * randomSize wich work well and do not return negative random values. And all my other resources, 8 more, work well…Can this be a kind of bug, like the scripts don’t treat the var as a long, even if it’s stated ? Any help will be greatly appreciated.
Sounds like an implicit casting error. If randomSize ends up being a float that loses precision when casted to a int I’m not positive what will happen.
For the operation the long MAY, and I say may because I’m not sure, be casted to a float in this case and then recasted to a long on assignment. My suggestion is to use explicit casting but you should more likely use a larger datatype instead of a float like double.
I may not be the best to answer this question though, I wish you luck.
I can’t see why you’d even consider storing the “real” values in the first place. It’s unnecessary, since nobody wants to look at numbers like that in a game. Use smaller, relatable numbers and then add a suffix, like “800 gigatons”. As a bonus, you bypass the coding issue entirely and don’t have to waste time figuring it out.
I need to have thoses high numbers because the production of ressource on the planet have to be calculated in kT or dam3, because my game is turn based and a turn equal to one month. And production for one month is not equal to a gigaton or something like this, so i need a smaller unit. For example, the smallest ship of the game will cost between 500 and 900 kT of Metal. And i need the amount to be really big so that the ressources on a planet last around 500 turns with 1000000K population. Even if i divide the numbers in the UI, so players won’t have to read to huge numbers, i need to have a more precise count in the background. But what i don’t understand is why it works for all my other resource, and not for water, wich use the same code.