Maris Posted April 2, 2015 Share Posted April 2, 2015 (edited) First of all, we got crash, while testing our mod:We look deeper and found interesting thing:print ( math.floor ( 1.15 * 100 ))--114You can type it in console. That's true.math.floor ( 1.15 * 100 ) is 114. So this Klei code does not work properly:function InventoryItem:SetWalkSpeedMult(walkspeedmult) local x = math.floor((walkspeedmult or 1) * 100) assert(x >= 0 and x <= 255, "Walk speed multiplier out of range: "..tostring(walkspeedmult)) assert(walkspeedmult == nil or math.abs(walkspeedmult * 100 - x) < .01 , "Walk speed multiplier can only have up to .01 precision: "..tostring(walkspeedmult)) self.classified.walkspeedmult:set(x)endBecause x==114 and walkspeedmult * 100 == 115Of course math.abs(walkspeedmult * 100 - x) < .01 is FALSE! Edited April 2, 2015 by Maris Link to comment Share on other sites More sharing options...
rezecib Posted April 2, 2015 Share Posted April 2, 2015 (edited) Of course math.abs(walkspeedmult * 100 - x) < .01 is FALSE! No, it's not. If math.floor(1.15*100) == 114, then there's rounding error causing 1.15*100 to be 1.14999999999 or something similar. So x is 114, and walkspeedmult*100 is 1.1499999999, so math.abs(walkspeedmult * 100 - x) is 0.0099999999, which is less than 0.01. That being said, it shouldn't be crashing there, because you are giving it what should be valid input. edit: derp, off by two orders of magnitude. that's really strange.... Edited April 2, 2015 by rezecib Link to comment Share on other sites More sharing options...
Maris Posted April 2, 2015 Author Share Posted April 2, 2015 No, it's not. If math.floor(1.15*100) == 114, then there's rounding error causing 1.15*100 to be 1.14999999999 or something similar. So x is 114, and walkspeedmult*100 is 1.1499999999, so math.abs(walkspeedmult * 100 - x) is 0.0099999999, which is less than 0.01. That being said, it shouldn't be crashing there, because you are giving it what should be valid input.x = 114walkspeedmult*100 = 114.9999999999walkspeedmult*100 - x = 0.999999990.99999999 > 0.1 Link to comment Share on other sites More sharing options...
Maris Posted April 2, 2015 Author Share Posted April 2, 2015 (edited) Solution (but it's a little hack):inst.components.equippable.walkspeedmult = 1.15 + 0.00001 Edited April 2, 2015 by Maris Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now