View previous topic :: View next topic |
Author |
Message |
Filipe_Br Master Cheater Reputation: 3
Joined: 07 Jan 2016 Posts: 272 Location: My house
|
Posted: Tue Mar 21, 2017 8:30 am Post subject: [source] Execute quickly when a key is pressed |
|
|
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 |
|
|
ParkourPenguin I post too much Reputation: 138
Joined: 06 Jul 2014 Posts: 4275
|
Posted: Tue Mar 21, 2017 8:56 am Post subject: |
|
|
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 |
|
|
Filipe_Br Master Cheater Reputation: 3
Joined: 07 Jan 2016 Posts: 272 Location: My house
|
Posted: Tue Mar 21, 2017 9:32 am Post subject: |
|
|
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 |
|
|
ParkourPenguin I post too much Reputation: 138
Joined: 06 Jul 2014 Posts: 4275
|
Posted: Tue Mar 21, 2017 10:30 am Post subject: |
|
|
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 |
|
|
Filipe_Br Master Cheater Reputation: 3
Joined: 07 Jan 2016 Posts: 272 Location: My house
|
Posted: Tue Mar 21, 2017 11:24 am Post subject: |
|
|
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 |
|
|
++METHOS I post too much Reputation: 92
Joined: 29 Oct 2010 Posts: 4197
|
Posted: Tue Mar 21, 2017 12:50 pm Post subject: |
|
|
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.
|
|
Back to top |
|
|
|