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 


[source] Execute quickly when a key is pressed

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting
View previous topic :: View next topic  
Author Message
Filipe_Br
Master Cheater
Reputation: 3

Joined: 07 Jan 2016
Posts: 272
Location: My house

PostPosted: Tue Mar 21, 2017 8:30 am    Post subject: [source] Execute quickly when a key is pressed Reply with quote

Sometimes we want some script to run quickly when we press any specific key, so we use "createTimer". But it makes your program run much slower, so we turn to createHotkey. But the script will not run extremely fast like the Timer.
A very simple solution to this problem is the following script:
Code:
function hack()
    ::START::
    if not isKeyPressed(VK_J) then return end

    -- Your script here

    goto START
end

if key then key.destroy(); key=nil end
key = createHotkey(hack, VK_J)

_________________
...
Back to top
View user's profile Send private message
ParkourPenguin
I post too much
Reputation: 138

Joined: 06 Jul 2014
Posts: 4275

PostPosted: Tue Mar 21, 2017 8:56 am    Post subject: Reply with quote

Why not just use a while loop?
Code:
function hack()
  while isKeyPressed(VK_J) do
    -- Your script here
  end
end


Filipe_Br wrote:
it makes your program run much slower

The overhead of using a timer has never been a problem for me. What makes you say this?

_________________
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
Filipe_Br
Master Cheater
Reputation: 3

Joined: 07 Jan 2016
Posts: 272
Location: My house

PostPosted: Tue Mar 21, 2017 9:32 am    Post subject: Reply with quote

I care about trying to make a script that spends the least possible amount of CPU.
"While" as I did not think about it. It does not make much difference, I believe that of the 2 forms the assembly code will look like.

_________________
...
Back to top
View user's profile Send private message
ParkourPenguin
I post too much
Reputation: 138

Joined: 06 Jul 2014
Posts: 4275

PostPosted: Tue Mar 21, 2017 10:30 am    Post subject: This post has 1 review(s) Reply with quote

Premature optimization is the root of all evil.

If the difference an optimization makes is inconsiderable, the time spent on that optimization was wasted and the complexity that optimization adds is unwarranted. If you are doing something in which creating a timer is a slow and/or costly operation, perhaps you should reconsider using Lua and instead use assembly.

The main problem is the side effects that come from the main thread executing that code in a loop. This freezes the GUI since CE can't respond to messages, but more importantly, the CPU is executing that code as fast as it possibly can. This is almost never required, and as such, your code will make the processor execute more cycles per second than if you had just used a timer.

There's more to programming than performance. Using higher-level abstractions (i.e. while loops and timers) allows you to write safer, more usable, more readable, and/or more maintainable code faster than code using lower-level abstractions.

IMO using a timer wouldn't make a significant difference. If you don't use a timer, the GUI hangs and the CPU is less efficient in most cases, but I really don't care and I don't think most people would care either. It's just another way of doing things.

_________________
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
Filipe_Br
Master Cheater
Reputation: 3

Joined: 07 Jan 2016
Posts: 272
Location: My house

PostPosted: Tue Mar 21, 2017 11:24 am    Post subject: Reply with quote

Really will freeze the gui. But this is to be used in specific cases. Imagine that in your game you want that when a key is pressed your coordinate is increased by 1. So here I think a good reason to do that.
_________________
...
Back to top
View user's profile Send private message
++METHOS
I post too much
Reputation: 92

Joined: 29 Oct 2010
Posts: 4197

PostPosted: Tue Mar 21, 2017 12:50 pm    Post subject: Reply with quote

ParkourPenguin wrote:
Premature optimization is the root of all evil.

If the difference an optimization makes is inconsiderable, the time spent on that optimization was wasted and the complexity that optimization adds is unwarranted.
-Well said. Now if only I can overcome my OCD. Very Happy
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