My tables come with a number of routines - among others - manipulating coordinates (save/restore position, free roaming). But I've now come across a couple of games that use doubles instead.
So rather then "extending" with 'if/then', I thought about "redefining" the read/writeFloat to Double counterpart (as shown in image)
(~ tested and working; picked this up when doing print vs save_to_file some time back)
Some questions/advice (before i get on to it):
1. is there a better/preferred way to do this (besides 'if then') ? Note that:
- most manipulation is done in lua, and singleFloat is precise enough
- do same thing with 'writeFloat to double'...
- asm part is always game_specific
b. if readFloat-function did not get saved first, how to restore original function ?
(besides quiting/loading CE ~ sh... happens )
c. add/multiply float to/with double: tested and working as expected
ps: if found a conversion algorithm 'double to float" which works fine. not yet tested if one can just "move params" to left_side in order to calc 'dVal' ?
=> dVal = ... ?
Last edited by paul44 on Tue Jul 16, 2024 11:09 am; edited 1 time in total
`readFloat = readDouble` - don't ever do this. If you must do something like this, use another variable that gets set to either readFloat or readDouble.
Code:
local readVal, writeVal
if gameUsesFloats then
readVal, writeVal = readFloat, writeFloat
else
readVal, writeVal = readDouble, writeDouble
end
That's kind of beside the point. If you're writing an API to manipulate coordinates, you should abstract away the details about how those coordinates are implemented in the game. Maybe use a higher-order function that takes in functions that transform the coordinates in the game's memory to/from some Lua structure that you control (e.g. a table w/ keys x, y, z).
paul44 wrote:
most manipulation is done in lua, and singleFloat is precise enough
Lua internally uses doubles. `readFloat` reads the 4-byte float from the game's memory and converts that to a double for Lua to use. `writeFloat` converts the double back to a float. _________________
I don't know where I'm going, but I'll figure it out when I get there.
Posted: Tue Jul 16, 2024 11:05 am Post subject: a simple function did the trick...
^ yeah, i wasn't all that keen either on doing that 'overwriting function'-thing.
Anyways: created 2 functions for that, and first tested on existing table using floats (getting it working with that setup)
(obviously needed to do some "clean up" - several vars to 8-byte mostly)
And now - more or less - applied it on new table with doubles; and it works fine (with little effort); ànd did not have to touch the (updated) routines !
In a nutshell:
[code]-- accepts float & double...
function readPrecision(addr)
local dVal = readFloat(addr)
if (bPrecision == 1) then dVal = readDouble(addr) end
return dVal
end
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You can download files in this forum