| 
			
				|  | Cheat Engine The Official Site of Cheat Engine
 
 
 |  
 
	
		| View previous topic :: View next topic |  
		| Author | Message |  
		| Birdi Expert Cheater
 
  Reputation: 0 
 Joined: 08 Jun 2020
 Posts: 124
 Location: Migrating
 
 | 
			
				|  Posted: Sun Sep 14, 2025 8:19 pm    Post subject: Defining 3-Byte Little Endian Custom Type |   |  
				| 
 |  
				| CE7.5 
 I've got an issue where in memory data is stored as 3 byte little endian for a set of counters.
 Most of these are fine as 2 bytes, but one is meant to count up to 1,000,000.
 Each 4th byte's tag ends as Hex 40, then the next counter is stored.
 
 I've tried a few approaches in the Cheat Table Lua Script to autoload the Custom Type but it always displays as 0 for the value.
 Am I missing something in this?
 
 
  	  | Code: |  	  | do
 local function register3byte(name)
 local function reader(address)
 local ok, b = pcall(readBytes, address, 4, true)
 if not ok or not b or #b < 3 then return 0 end
 local b0 = b[1] or 0
 local b1 = b[2] or 0
 local b2 = b[3] or 0
 return b0 + b1*256 + b2*65536
 end
 local function writer(address, value)
 local v = tonumber(value) or 0
 if v < 0 then v = 0 end
 if v > 0xFFFFFF then v = 0xFFFFFF end
 local ok, t = pcall(readBytes, address+3, 1, true)
 local tag = (ok and t and t[1]) and t[1] or 0x40
 local b0 = v % 256
 local b1 = math.floor(v/256)   % 256
 local b2 = math.floor(v/65536) % 256
 pcall(writeBytes, address, b0, b1, b2, tag)
 end
 local ok, err = pcall(registerCustomTypeLua, name, 4, reader, writer)
 end
 register3byte("3 Bytes")
 end
 
 | 
 
 Example:
 00 01 0F 40 = 4byte: 1074725120, 2byte: 256, In-Game: 983296, CE: 0
 _________________
 
 Trying to learn!
 Add me on Discord if you want hands-on help:
 Birdi.
 |  |  
		| Back to top |  |  
		|  |  
		| Dark Byte Site Admin
 
  Reputation: 470 
 Joined: 09 May 2003
 Posts: 25807
 Location: The netherlands
 
 | 
			
				|  Posted: Mon Sep 15, 2025 5:53 am    Post subject: |   |  
				| 
 |  
				| Don't use address. bytes come first 
 
  	  | Code: |  	  | do
 local function register3byte(name)
 local function reader(b0,b1,b2)
 return b0 + b1*256 + b2*65536
 end
 local function writer(value)
 local r=dwordToByteTable(value)
 r[4]=nil
 return r
 end
 local ok, err = pcall(registerCustomTypeLua, name, 3, reader, writer)
 end
 register3byte("3 Bytes")
 end
 
 | 
 _________________
 
 Do not ask me about online cheats. I don't know any and wont help finding them.
 Like my help? Join me on Patreon so i can keep helping
 |  |  
		| Back to top |  |  
		|  |  
		| AylinCE Grandmaster Cheater Supreme
 
  Reputation: 37 
 Joined: 16 Feb 2017
 Posts: 1527
 
 
 | 
			
				|  Posted: Mon Sep 15, 2025 2:34 pm    Post subject: |   |  
				| 
 |  
				| My opinion; 
 
  	  | Code: |  	  | do local function register3byte(name)
 local function reader(address)
 local ok, b = pcall(readBytes, address, 3, true)
 if not ok or not b or #b < 3 then return 0 end
 local b0 = b[1] or 0
 local b1 = b[2] or 0
 local b2 = b[3] or 0
 return b0 + b1*256 + b2*65536
 end
 
 local function writer(address, value)
 local v = tonumber(value) or 0
 if v < 0 then v = 0 end
 if v > 0xFFFFFF then v = 0xFFFFFF end
 local b0 = v % 256
 local b1 = math.floor(v/256)    % 256
 local b2 = math.floor(v/65536) % 256
 pcall(writeBytes, address, b0, b1, b2)
 end
 
 local ok, err = pcall(registerCustomTypeLua, name, 3, reader, writer)
 if not ok then
 showMessage("Failed to register 3 Bytes custom type: " .. err)
 end
 end
 
 register3byte("3 Bytes")
 end
 | 
 _________________
 
 |  |  
		| Back to top |  |  
		|  |  
		| Dark Byte Site Admin
 
  Reputation: 470 
 Joined: 09 May 2003
 Posts: 25807
 Location: The netherlands
 
 | 
			
				|  Posted: Mon Sep 15, 2025 3:15 pm    Post subject: |   |  
				| 
 |  
				| as I mentioned in the previous post, the first parameter is not the address, so this will fail _________________
 
 Do not ask me about online cheats. I don't know any and wont help finding them.
 Like my help? Join me on Patreon so i can keep helping
 |  |  
		| Back to top |  |  
		|  |  
		| Birdi Expert Cheater
 
  Reputation: 0 
 Joined: 08 Jun 2020
 Posts: 124
 Location: Migrating
 
 | 
			
				|  Posted: Mon Sep 15, 2025 5:33 pm    Post subject: |   |  
				| 
 |  
				|  	  | Dark Byte wrote: |  	  | Don't use address. bytes come first 
 
  	  | Code: |  	  | do
 local function register3byte(name)
 local function reader(b0,b1,b2)
 return b0 + b1*256 + b2*65536
 end
 local function writer(value)
 local r=dwordToByteTable(value)
 r[4]=nil
 return r
 end
 local ok, err = pcall(registerCustomTypeLua, name, 3, reader, writer)
 end
 register3byte("3 Bytes")
 end
 
 | 
 | 
 
 Many thanks, worked perfectly!
 
 For the Writer section, setting the 4th Byte to Nil seems odd to me.
 I see in practice it just ignores that one when writing, but my brain tells me it should be "nothing" -- is it that it writes until the end of table or until Nil is met? Just for future cases, should I need to ignore n and m bytes in an array for some reason (probably just easier to do offset +n secondary write tbh)
 Edit: Well, rereading the documentation and implementation here I see that bytecount should've been the intended number of bytes and not the size.
 _________________
 
 Trying to learn!
 Add me on Discord if you want hands-on help:
 Birdi.
 |  |  
		| Back to top |  |  
		|  |  
		| Csimbi I post too much
 
  Reputation: 97 
 Joined: 14 Jul 2007
 Posts: 3327
 
 
 | 
			
				|  Posted: Tue Sep 16, 2025 1:23 pm    Post subject: |   |  
				| 
 |  
				|  	  | Birdi wrote: |  	  | For the Writer section, setting the 4th Byte to Nil seems odd to me.
 
 | 
 You do need the last octet of the DWORD to be ignored.
 (it will always be 0x40 and so, you are not writing that octet)
 |  |  
		| Back to top |  |  
		|  |  
		|  |  
  
	| 
 
 | 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
 
 |  |