Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


Generalize routine using either Float or Double [Solved]

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting
View previous topic :: View next topic  
Author Message
paul44
Expert Cheater
Reputation: 2

Joined: 20 Jul 2017
Posts: 194

PostPosted: Sun Jul 14, 2024 11:52 am    Post subject: Generalize routine using either Float or Double [Solved] Reply with quote

image ref: [ https://ibb.co/tMCVGGm ]

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 Rolling Eyes)
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
Back to top
View user's profile Send private message
ParkourPenguin
I post too much
Reputation: 150

Joined: 06 Jul 2014
Posts: 4657

PostPosted: Sun Jul 14, 2024 1:19 pm    Post subject: Reply with quote

`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.
Back to top
View user's profile Send private message
paul44
Expert Cheater
Reputation: 2

Joined: 20 Jul 2017
Posts: 194

PostPosted: Tue Jul 16, 2024 11:05 am    Post subject: a simple function did the trick... Reply with quote

^ 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

...
writePrecision(addrCoord,readPrecision(addrOrigX))[/code]

=> (global) bPrecision is set in my main script


ps: Lua internally uses doubles: nice to know, expecially since many articles @stackoverflow refer to float - rather then double - in general
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites