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 


[Scrip Request] Loop with Wait for Gamepad Input

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting
View previous topic :: View next topic  
Author Message
paulce
How do I cheat?
Reputation: 0

Joined: 30 Jul 2018
Posts: 5

PostPosted: Sat Aug 04, 2018 9:56 pm    Post subject: [Scrip Request] Loop with Wait for Gamepad Input Reply with quote

This kind of goes with the question I asked a few days ago in regards to auto changing character names in games. Anyways, I figured out how to make it constantly update using a loop. Unfortunately, the loop in infinite and slows down the game. I also have to forcefully terminate cheat engine to exit the program.

I was wondering if anyone could add some kind of a function in the below script that will allow a pause and wait for any button pressed gamepad input so that it will still loop but will wait until a button from a gamepad is pressed. If I don't use the loop, the script only updates once and when new data comes in the new character names don't change in the dialogue unless I update manually again.

Code:
{$lua}
While true do
function replaceString(string_in,string_out,ignore_length)
   if (not ignore_length) then
      if (not(string_in and string_out and #string_in >= #string_out)) then
         return print("Not recommended to override shorter string with a longer string");
      end
   end
   local bytes_in = {};
   local bytes_out = {};
   for i=1,(#string_in >= #string_out and #string_in or #string_out) do -- lazy to copy paste same loop for string_out so just looping both and inserting if possible
      if (i <= #string_in) then
         table.insert(bytes_in,string.format("%x", tonumber(string.byte(string.sub(string_in,i,i)))));
      end
      if (i <= #string_out) then
         -- table.insert(bytes_out,'0x' .. string.format("%x", tonumber(string.byte(string.sub(string_out,i,i)))));
         table.insert(bytes_out,tonumber(string.byte(string.sub(string_out,i,i))));
      end
   end
   local object = AOBScan(table.concat(bytes_in," "));
   if object then
      for entry = 0, object.Count -1 do
         writeBytes(object.getString(entry), unpack(bytes_out));
      end
      object.destroy();
      return true
   end
   return false
end
-- replaceString(String to find(string), string to replace with(string), ignore replace string length(true/false))
-- returns true/false as result
replaceString("Old Name ","New Name",true);
end


Last edited by paulce on Sat Aug 04, 2018 11:09 pm; edited 1 time in total
Back to top
View user's profile Send private message
TheyCallMeTim13
Wiki Contributor
Reputation: 51

Joined: 24 Feb 2017
Posts: 976
Location: Pluto

PostPosted: Sat Aug 04, 2018 10:41 pm    Post subject: Reply with quote

It's late and the code is difficult to read, and I gave up on reading it.
Try and use code blocks, might get you better results in the future.
Code:
[code][/code]

_________________
Back to top
View user's profile Send private message Visit poster's website
TheyCallMeTim13
Wiki Contributor
Reputation: 51

Joined: 24 Feb 2017
Posts: 976
Location: Pluto

PostPosted: Sun Aug 05, 2018 7:14 am    Post subject: Reply with quote

That looks better, thank you.

You could just use "sleep" to allow CE to update, but I would use a timer for this.

And use "getXBox360ControllerState" and it's "PacketNumer" to detect changes.

And there is no reason to but the function declaration in the loop, it will only slow it down more.
Code:
function replaceString(string_in,string_out,ignore_length)
   if (not ignore_length) then
      if (not(string_in and string_out and #string_in >= #string_out)) then
         return print("Not recommended to override shorter string with a longer string");
      end
   end
   local bytes_in = {};
   local bytes_out = {};
   for i=1,(#string_in >= #string_out and #string_in or #string_out) do -- lazy to copy paste same loop for string_out so just looping both and inserting if possible
      if (i <= #string_in) then
         table.insert(bytes_in,string.format("%x", tonumber(string.byte(string.sub(string_in,i,i)))));
      end
      if (i <= #string_out) then
         -- table.insert(bytes_out,'0x' .. string.format("%x", tonumber(string.byte(string.sub(string_out,i,i)))));
         table.insert(bytes_out,tonumber(string.byte(string.sub(string_out,i,i))));
      end
   end
   local object = AOBScan(table.concat(bytes_in," "));
   if object then
      for entry = 0, object.Count -1 do
         writeBytes(object.getString(entry), unpack(bytes_out));
      end
      object.destroy();
      return true
   end
   return false
end


--------
-------- Timer
--------
RunState = true
ControllerPackets = {}
local aTimer = nil
local aTimerInterval = 1000
local aTimerTicks = 0
local function aTimer_tick(timer)
   if not RunState then
      timer.destroy()
   end
   local xbcState = getXBox360ControllerState()
   if ControllerPackets[xbcState.ControllerID] ~= xbcState.PacketNumber then
      ControllerPackets[xbcState.ControllerID] = xbcState.PacketNumber
      replaceString("Old Name ","New Name",true);
   end
end
aTimer = createTimer(MainForm)
aTimer.Interval = aTimerInterval
aTimer.OnTimer = aTimer_tick


https://wiki.cheatengine.org/index.php?title=Lua:getXBox360ControllerState

EDIT:
Then just set "RunState" to false somewhere to stop the timer.

_________________
Back to top
View user's profile Send private message Visit poster's website
paulce
How do I cheat?
Reputation: 0

Joined: 30 Jul 2018
Posts: 5

PostPosted: Sun Aug 05, 2018 9:31 am    Post subject: Reply with quote

Thank you very much for the help. I apologize for not initially putting the script in the code box.
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