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 


How to wait for two seconds in each loop of Lua's for loop?

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting
View previous topic :: View next topic  
Author Message
etioplmld
Advanced Cheater
Reputation: 0

Joined: 09 Feb 2021
Posts: 72

PostPosted: Thu May 11, 2023 12:53 am    Post subject: How to wait for two seconds in each loop of Lua's for loop? Reply with quote

When using the for loop to process some addresses, it is necessary to wait for each address, loop wait.

first,Define a function.

Functions like this
Set breakpoints on an address
Check if the address has been accessed until two seconds timeout

Then use the for function to execute the function for each address。

But the result of such code execution is that the for function did not wait and directly checked all addresses
Back to top
View user's profile Send private message
Dark Byte
Site Admin
Reputation: 457

Joined: 09 May 2003
Posts: 25262
Location: The netherlands

PostPosted: Thu May 11, 2023 1:50 am    Post subject: Reply with quote

it's better to split up the for loop into different pieces of code and use createTimer(time,function) to create 1 time run timers

But if you insist (warning: crappy bad code here)
Code:

local a=getApplication()
killme=false
for i=1,1000 do
  print(i)

  local t=getTickCount()

  while getTickCount()<t+2000 do --check if 2000 milliseconds have passed
    if killme then return end
    a.processMessages()
  end
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
View user's profile Send private message MSN Messenger
etioplmld
Advanced Cheater
Reputation: 0

Joined: 09 Feb 2021
Posts: 72

PostPosted: Thu May 11, 2023 3:16 am    Post subject: Reply with quote

There is a lot of JavaScript code in the search results for this question. Can it be converted to Lua?

Code:

for (var i = 0; i < 5; i++) {
    (function (j) {
       setTimeout(function() {
          console.log(new Date, j);
       }, 1000 * j);
    })(i);
 }
 setTimeout(function() { 
    console.log(new Date, i);
 }, 1000 * i);



Code:

function time(val)
{
for(var i = val;i&lt;7;i++)
{
alert(i)
window.setTimeout("time("+ ++i +")",1000)
break;
}
}
time(0)


Code:

for (var i = 0; i < 6; i++) { 
      setTimeout(fn(i),1000); 
    }
    function fn(a){
        return function(){
            console.log(a);
        }
    }


Code:

for (var i = 0; i < 6; i++) {
    (function (t, data) {   
        setTimeout(function () {
            console.log(`this is ${t} times,this is other:${data}`);
        }, 1000 * t);   
    })(i, 'other')   
}

Code:

for (let i = 1; i <= 5; i++) {
  setTimeout(function timer() {
    console.log(i);
  }, i * 1000);
}
Back to top
View user's profile Send private message
Dark Byte
Site Admin
Reputation: 457

Joined: 09 May 2003
Posts: 25262
Location: The netherlands

PostPosted: Thu May 11, 2023 4:08 am    Post subject: Reply with quote

maybe an example like this will help:
Code:

for i=1,20 do
  local uv=i*1000
  createTimer(1000*i,function()
    print("handling timer "..i.." uv="..uv)
  end)
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
View user's profile Send private message MSN Messenger
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