-- Function to read a 32-bit integer from memory function readInt(address) local value =…
- User
-- Function to read a 32-bit integer from memory function readInt(address) local value = 0 if cheatengine.isPointer(address) then value = tonumber(cheatengine.readInteger(address)) else value = tonumber(cheatengine.readInteger(cheatengine.resolvePointer(address))) end return value end
-- Function to write a 32-bit integer to memory function writeInt(address, value) if cheatengine.isPointer(address) then cheatengine.writeInteger(address, value) else cheatengine.writeInteger(cheatengine.resolvePointer(address), value) end end
-- Function to perform an initial scan for a specific value function initialScan(value, valueType, scanType) cheatengine.scanNew() cheatengine.setScanType(scanType) cheatengine.setValueType(valueType) cheatengine.setScanValue(value) cheatengine.firstScan() return cheatengine.getResultsCount() > 0 end
-- Function to narrow down the results function narrowDownScan(value, valueType, scanType) cheatengine.setScanType(scanType) cheatengine.setValueType(valueType) cheatengine.setScanValue(value) cheatengine.nextScan() return cheatengine.getResultsCount() > 0 end
-- Function to find the enemy's position function findEnemyPosition() -- Initial scan for the enemy's X position (replace with the actual value) local enemyXValue = 1000 -- Replace with the actual value if not initialScan(enemyXValue, "4 bytes", "Exact Value") then print("Failed to find initial enemy X position.") return nil, nil, nil end
-- Narrow down the results (replace with the new value after moving the enemy) local newEnemyXValue = 1500 -- Replace with the new value if not narrowDownScan(newEnemyXValue, "4 bytes", "Changed Value") then print("Failed to narrow down enemy X position.") return nil, nil, nil end -- Get the address of the enemy's X position local enemyXAddress = cheatengine.getResult(0).address -- Read the enemy's Y and Z positions local enemyYAddress = enemyXAddress + 4 local enemyZAddress = enemyXAddress + 8 local enemyX = readInt(enemyXAddress) local enemyY = readInt(enemyYAddress) local enemyZ = readInt(enemyZAddress) return enemyX, enemyY, enemyZ, enemyXAddressend
-- Function to find the player's aim position function findPlayerAimPosition() -- Initial scan for the player's aim X position (replace with the actual value) local playerAimXValue = 2000 -- Replace with the actual value if not initialScan(playerAimXValue, "4 bytes", "Exact Value") then print("Failed to find initial player aim X position.") return nil, nil, nil end
-- Narrow down the results (replace with the new value after moving the aim) local newPlayerAimXValue = 2500 -- Replace with the new value if not narrowDownScan(newPlayerAimXValue, "4 bytes", "Changed Value") then print("Failed to narrow down player aim X position.") return nil, nil, nil end -- Get the address of the player's aim X position local playerAimXAddress = cheatengine.getResult(0).address -- Read the player's aim Y and Z positions local playerAimYAddress = playerAimXAddress + 4 local playerAimZAddress = playerAimXAddress + 8 local playerAimX = readInt(playerAimXAddress) local playerAimY = readInt(playerAimYAddress) local playerAimZ = readInt(playerAimZAddress) return playerAimX, playerAimY, playerAimZ, playerAimXAddressend
-- Function to calculate the aim offset function calculateAimOffset(enemyX, enemyY, enemyZ, playerAimX, playerAimY, playerAimZ) -- Calculate the offset to aim at the enemy's head local offsetX = enemyX - playerAimX local offsetY = enemyY - playerAimY local offsetZ = enemyZ - playerAimZ
-- Adjust the aim (this is a simple example, you may need to fine-tune the values) local newAimX = playerAimX + offsetX local newAimY = playerAimY + offsetY local newAimZ = playerAimZ + offsetZ return newAimX, newAimY, newAimZend
-- Main loop while true do -- Find the enemy's position local enemyX, enemyY, enemyZ, enemyXAddress = findEnemyPosition() if not enemyX or not enemyY or not enemyZ then print("Failed to find enemy position. Retrying...") os.sleep(1) goto continue end
-- Find the player's aim position local playerAimX, playerAimY, playerAimZ, playerAimXAddress = findPlayerAimPosition() if not playerAimX or not playerAimY or not playerAimZ then print("Failed to find player aim position. Retrying...") os.sleep(1) goto continue end -- Calculate the aim offset local newAimX, newAimY, newAimZ = calculateAimOffset(enemyX, enemyY, enemyZ, playerAimX, playerAimY, playerAimZ) -- Write the new aim position back to memory writeInt(playerAimXAddress, newAimX) writeInt(playerAimXAddress + 4, newAimY) writeInt(playerAimXAddress + 8, newAimZ) ::continue:: -- Sleep to avoid high CPU usage os.sleep(0.01)end mportant Notes: Values: You need to replace the placeholder values (1000, 1500, 2000, 2500) with automatic process to get them from the game " (replace with the actual value)" replace this with automatic process
- Assistant
To automate the retrieval of in-game values (such as enemy and player positions) instead of manually replacing placeholder values, you can modify your Cheat Engine Lua script to perform unknown initial value scans followed by unknown value changing scans. This approach allows the script to dynamically identify and track the required memory addresses based on how their values change during gameplay.
Below is a comprehensive guide on how to achieve this:
Overview
-
Initial Scan (Unknown Initial Value): Start by scanning for all possible addresses that match the desired data type without specifying a value. This will return a large list of potential candidates.
-
Narrow Down Scans (Unknown Value Changed): As the game state changes (e.g., the enemy moves), perform scans that filter out addresses whose values have not changed. This reduces the list of candidates step by step until only the correct address remains.
-
Automate the Scanning Process: Implement loops and condition checks to automate these scans without manual intervention.
Step-by-Step Implementation
1. Modify Scan Functions to Handle Unknown Values
First, adjust your scan functions to accommodate unknown initial values and unknown value changes.
-- Function to perform an initial unknown value scan function initialScanUnknown(scanType) cheatengine.scanNew() cheatengine.setScanType(scanType) -- Typically, "Unknown Initial Value" cheatengine.setValueType("4 Bytes") -- Adjust as needed cheatengine.unknownInitialScan() return cheatengine.getResultsCount() > 0 end -- Function to perform a narrow down unknown value changed scan function narrowDownScanUnknown(scanType) cheatengine.setScanType(scanType) -- Typically, "Unknown Value Changed" cheatengine.setValueType("4 Bytes") -- Adjust as needed cheatengine.unknownUpdateScan() return cheatengine.getResultsCount() > 0 end2. Automate the Scanning Process for Enemy and Player Positions
Create functions that handle the scanning process automatically without predefined values.
-- Function to automatically find a pointer for a given base address function findPointer(address) if cheatengine.isPointer(address) then return address else return cheatengine.resolvePointer(address) end end -- Function to automatically find the enemy's position function findEnemyPosition() -- Initial unknown value scan if not initialScanUnknown("Unknown Initial Value") then print("Initial unknown scan failed for enemy position.") return nil, nil, nil end -- Wait for the enemy to move or change position in the game here -- You might need to prompt the user to move the enemy or wait for an event print("Initial scan completed. Change the enemy's position and press Enter to continue...") io.read() -- Narrow down with unknown value changed if not narrowDownScanUnknown("Unknown Value Changed") then print("Failed to narrow down enemy position.") return nil, nil, nil end -- Repeat narrowing down until results are manageable while cheatengine.getResultsCount() > 1 do print("Multiple results found. Make another change to the enemy's position and press Enter to continue...") io.read() if not narrowDownScanUnknown("Unknown Value Changed") then print("Failed to narrow down enemy position.") return nil, nil, nil end end -- Assuming only one result is left if cheatengine.getResultsCount() == 1 then local enemyXAddress = cheatengine.getResult(0).address -- Read Y and Z positions based on known offsets local enemyYAddress = enemyXAddress + 4 local enemyZAddress = enemyXAddress + 8 local enemyX = readInt(enemyXAddress) local enemyY = readInt(enemyYAddress) local enemyZ = readInt(enemyZAddress) print(string.format("Enemy Position Found: X=0x%X, Y=0x%X, Z=0x%X", enemyXAddress, enemyYAddress, enemyZAddress)) return enemyX, enemyY, enemyZ, enemyXAddress else print("Multiple or no results found for enemy position.") return nil, nil, nil end end -- Function to automatically find the player's aim position function findPlayerAimPosition() -- Initial unknown value scan if not initialScanUnknown("Unknown Initial Value") then print("Initial unknown scan failed for player aim position.") return nil, nil, nil end -- Wait for the player to move or change aim in the game here -- You might need to prompt the user to move the aim or wait for an event print("Initial scan completed. Change the player's aim and press Enter to continue...") io.read() -- Narrow down with unknown value changed if not narrowDownScanUnknown("Unknown Value Changed") then print("Failed to narrow down player aim position.") return nil, nil, nil end -- Repeat narrowing down until results are manageable while cheatengine.getResultsCount() > 1 do print("Multiple results found. Make another change to the player's aim and press Enter to continue...") io.read() if not narrowDownScanUnknown("Unknown Value Changed") then print("Failed to narrow down player aim position.") return nil, nil, nil end end -- Assuming only one result is left if cheatengine.getResultsCount() == 1 then local playerAimXAddress = cheatengine.getResult(0).address -- Read Y and Z positions based on known offsets local playerAimYAddress = playerAimXAddress + 4 local playerAimZAddress = playerAimXAddress + 8 local playerAimX = readInt(playerAimXAddress) local playerAimY = readInt(playerAimYAddress) local playerAimZ = readInt(playerAimZAddress) print(string.format("Player Aim Position Found: X=0x%X, Y=0x%X, Z=0x%X", playerAimXAddress, playerAimYAddress, playerAimZAddress)) return playerAimX, playerAimY, playerAimZ, playerAimXAddress else print("Multiple or no results found for player aim position.") return nil, nil, nil end end3. Update the Main Loop to Handle Automatic Scans
Modify your main loop to incorporate the automated scanning functions.
-- Function to calculate the aim offset function calculateAimOffset(enemyX, enemyY, enemyZ, playerAimX, playerAimY, playerAimZ) -- Calculate the offset to aim at the enemy's head local offsetX = enemyX - playerAimX local offsetY = enemyY - playerAimY local offsetZ = enemyZ - playerAimZ -- Adjust the aim (this is a simple example, you may need to fine-tune the values) local newAimX = playerAimX + offsetX local newAimY = playerAimY + offsetY local newAimZ = playerAimZ + offsetZ return newAimX, newAimY, newAimZ end -- Main loop while true do -- Find the enemy's position local enemyX, enemyY, enemyZ, enemyXAddress = findEnemyPosition() if not enemyX or not enemyY or not enemyZ then print("Failed to find enemy position. Retrying...") os.sleep(1) goto continue end -- Find the player's aim position local playerAimX, playerAimY, playerAimZ, playerAimXAddress = findPlayerAimPosition() if not playerAimX or not playerAimY or not playerAimZ then print("Failed to find player aim position. Retrying...") os.sleep(1) goto continue end -- Calculate the aim offset local newAimX, newAimY, newAimZ = calculateAimOffset(enemyX, enemyY, enemyZ, playerAimX, playerAimY, playerAimZ) -- Write the new aim position back to memory writeInt(playerAimXAddress, newAimX) writeInt(playerAimXAddress + 4, newAimY) writeInt(playerAimXAddress + 8, newAimZ) print(string.format("Aim adjusted to X=0x%X, Y=0x%X, Z=0x%X", newAimX, newAimY, newAimZ)) ::continue:: -- Sleep to avoid high CPU usage os.sleep(0.5) -- Increased sleep time to allow manual changes end4. Additional Enhancements and Considerations
-
User Prompts: The script prompts the user to make changes in the game (e.g., moving the enemy or changing the player's aim) and then press Enter to continue scanning. This manual step is essential for narrowing down addresses during the scanning process.
-
Automated Event Detection: For a fully automated experience, integrating game event hooks or memory change listeners would be ideal. However, this requires more advanced scripting and deeper integration with the game's API, which might not always be feasible.
-
Error Handling: Ensure that the script gracefully handles scenarios where multiple addresses are found or no addresses match the criteria.
-
Performance Optimization: Adjust the
os.sleepduration in the main loop to balance between responsiveness and CPU usage. -
Memory Offsets: Ensure that the offsets (
+4for Y and+8for Z) are correct for your specific game. These offsets can vary between games and even different versions of the same game.
Complete Revised Script
Here’s the complete revised script incorporating the automated scanning process:
-- Function to read a 32-bit integer from memory function readInt(address) local value = 0 if cheatengine.isPointer(address) then value = tonumber(cheatengine.readInteger(address)) else value = tonumber(cheatengine.readInteger(cheatengine.resolvePointer(address))) end return value end -- Function to write a 32-bit integer to memory function writeInt(address, value) if cheatengine.isPointer(address) then cheatengine.writeInteger(address, value) else cheatengine.writeInteger(cheatengine.resolvePointer(address), value) end end -- Function to perform an initial unknown value scan function initialScanUnknown(scanType) cheatengine.scanNew() cheatengine.setScanType(scanType) -- "Unknown Initial Value" cheatengine.setValueType("4 Bytes") -- Adjust as needed cheatengine.unknownInitialScan() return cheatengine.getResultsCount() > 0 end -- Function to perform a narrow down unknown value changed scan function narrowDownScanUnknown(scanType) cheatengine.setScanType(scanType) -- "Unknown Value Changed" cheatengine.setValueType("4 Bytes") -- Adjust as needed cheatengine.unknownUpdateScan() return cheatengine.getResultsCount() > 0 end -- Function to find the enemy's position function findEnemyPosition() print("Starting initial unknown scan for enemy X position...") if not initialScanUnknown("Unknown Initial Value") then print("Initial unknown scan failed for enemy position.") return nil, nil, nil end print("Initial scan completed. Change the enemy's position and press Enter to continue...") io.read() if not narrowDownScanUnknown("Unknown Value Changed") then print("Failed to narrow down enemy position.") return nil, nil, nil end -- Repeat narrowing down until one result is found while cheatengine.getResultsCount() > 1 do print("Multiple results found. Change the enemy's position again and press Enter to continue...") io.read() if not narrowDownScanUnknown("Unknown Value Changed") then print("Failed to narrow down enemy position.") return nil, nil, nil end end if cheatengine.getResultsCount() == 1 then local enemyXAddress = cheatengine.getResult(0).address print(string.format("Enemy X Address Found: 0x%X", enemyXAddress)) -- Read Y and Z positions based on known offsets local enemyYAddress = enemyXAddress + 4 local enemyZAddress = enemyXAddress + 8 local enemyX = readInt(enemyXAddress) local enemyY = readInt(enemyYAddress) local enemyZ = readInt(enemyZAddress) print(string.format("Enemy Position: X=%d, Y=%d, Z=%d", enemyX, enemyY, enemyZ)) return enemyX, enemyY, enemyZ, enemyXAddress else print("Failed to uniquely identify enemy position.") return nil, nil, nil end end -- Function to find the player's aim position function findPlayerAimPosition() print("Starting initial unknown scan for player aim X position...") if not initialScanUnknown("Unknown Initial Value") then print("Initial unknown scan failed for player aim position.") return nil, nil, nil end print("Initial scan completed. Change the player's aim and press Enter to continue...") io.read() if not narrowDownScanUnknown("Unknown Value Changed") then print("Failed to narrow down player aim position.") return nil, nil, nil end -- Repeat narrowing down until one result is found while cheatengine.getResultsCount() > 1 do print("Multiple results found. Change the player's aim again and press Enter to continue...") io.read() if not narrowDownScanUnknown("Unknown Value Changed") then print("Failed to narrow down player aim position.") return nil, nil, nil end end if cheatengine.getResultsCount() == 1 then local playerAimXAddress = cheatengine.getResult(0).address print(string.format("Player Aim X Address Found: 0x%X", playerAimXAddress)) -- Read Y and Z positions based on known offsets local playerAimYAddress = playerAimXAddress + 4 local playerAimZAddress = playerAimXAddress + 8 local playerAimX = readInt(playerAimXAddress) local playerAimY = readInt(playerAimYAddress) local playerAimZ = readInt(playerAimZAddress) print(string.format("Player Aim Position: X=%d, Y=%d, Z=%d", playerAimX, playerAimY, playerAimZ)) return playerAimX, playerAimY, playerAimZ, playerAimXAddress else print("Failed to uniquely identify player aim position.") return nil, nil, nil end end -- Function to calculate the aim offset function calculateAimOffset(enemyX, enemyY, enemyZ, playerAimX, playerAimY, playerAimZ) -- Calculate the offset to aim at the enemy's head local offsetX = enemyX - playerAimX local offsetY = enemyY - playerAimY local offsetZ = enemyZ - playerAimZ -- Adjust the aim (this is a simple example, you may need to fine-tune the values) local newAimX = playerAimX + offsetX local newAimY = playerAimY + offsetY local newAimZ = playerAimZ + offsetZ return newAimX, newAimY, newAimZ end -- Main loop while true do -- Find the enemy's position local enemyX, enemyY, enemyZ, enemyXAddress = findEnemyPosition() if not enemyX or not enemyY or not enemyZ then print("Failed to find enemy position. Retrying in 1 second...") os.sleep(1) goto continue end -- Find the player's aim position local playerAimX, playerAimY, playerAimZ, playerAimXAddress = findPlayerAimPosition() if not playerAimX or not playerAimY or not playerAimZ then print("Failed to find player aim position. Retrying in 1 second...") os.sleep(1) goto continue end -- Calculate the aim offset local newAimX, newAimY, newAimZ = calculateAimOffset(enemyX, enemyY, enemyZ, playerAimX, playerAimY, playerAimZ) -- Write the new aim position back to memory writeInt(playerAimXAddress, newAimX) writeInt(playerAimXAddress + 4, newAimY) writeInt(playerAimXAddress + 8, newAimZ) print(string.format("Aim adjusted to X=%d, Y=%d, Z=%d", newAimX, newAimY, newAimZ)) ::continue:: -- Sleep to avoid high CPU usage os.sleep(0.5) -- Adjust as needed endImportant Notes
-
Manual Interaction for Scanning:
- The automated scanning process requires manual interaction to change in-game states (like moving the enemy or altering the player's aim) and then pressing Enter to continue scanning. Fully automating this without user input would require more advanced techniques, such as hooking into game functions or memory change notifications, which is beyond the scope of this guide.
-
Scan Accuracy:
- The success of unknown value scans heavily depends on how distinct the changes are. Ensure that when changing positions or aiming, only the relevant values are altered to minimize false positives.
-
Address Stability:
- Game memory layouts can change between sessions or updates. Implement pointer scanning or signature scanning if addresses are not stable.
-
Ethical and Legal Considerations:
- Modifying game memory can violate the terms of service of many games and can lead to bans or other penalties. Ensure that you have the right to modify the game's memory and that you're not violating any laws or agreements.
-
Performance Impact:
- Continuously scanning and writing to memory can impact game performance. Optimize scan types and intervals to balance functionality and performance.
-
Cheat Engine API:
- Familiarize yourself with the Cheat Engine Lua scripting API for more advanced functionalities and better control over the scanning and writing processes.
By implementing the above modifications, your script will automatically handle the acquisition of in-game values, making it more dynamic and reducing the need for manual intervention. Ensure thorough testing to refine the scanning process and adapt it to the specific game's memory structure.
-