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 


Assigning Lua script to key

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting
View previous topic :: View next topic  
Author Message
mikey1993
Newbie cheater
Reputation: 0

Joined: 20 Jul 2017
Posts: 15

PostPosted: Thu Jul 20, 2017 5:13 am    Post subject: Assigning Lua script to key Reply with quote

Hi guys, I'm new to Cheat Engine and extremely new to Lua scripting. I want to thank anyone that might be able to help me in advance.

I've written a Lua script and it's all working exactly how I want. When I execute the script everything is perfect. What I am having a heap of trouble with is figuring out how to assign the script to Numpad4 key.

I want to be able to play the game and press 4 and the script executes.

Please if anyone can help I would really appreciate it.
Back to top
View user's profile Send private message
Dark Byte
Site Admin
Reputation: 342

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

PostPosted: Thu Jul 20, 2017 5:38 am    Post subject: Reply with quote

createHotkey(scriptfunction, VK_4)
_________________
Do not ask me about online cheats. I don't know any and wont help finding them.
Back to top
View user's profile Send private message MSN Messenger
mikey1993
Newbie cheater
Reputation: 0

Joined: 20 Jul 2017
Posts: 15

PostPosted: Thu Jul 20, 2017 5:53 am    Post subject: Reply with quote

Thank you so much Dark. I did what you said and there must be one more thing I'm missing. I'll paste the script below. When I press 4 I get a pop up box with the message ":Error attempt to call a nill value"

createHotkey(scriptfunction, VK_NUMPAD4)
DeadlyShock = 65575
DemonStrike = 65576
WhirlingDervish = 65577
BastardSwing = 65578
local PercentageSkill = readInteger("0x221101e")
local PercentageSkill2 = readInteger("0x2211032")
if (PercentageSkill == DeadlyShock) then
writeInteger("0x221101e",DemonStrike)
elseif (PercentageSkill == DemonStrike) then
writeInteger("0x221101e",DeadlyShock)
end
if (PercentageSkill2 == WhirlingDervish) then
writeInteger("0x2211032",BastardSwing)
elseif (PercentageSkill2 == BastardSwing) then
writeInteger("0x2211032",WhirlingDervish)
end
Back to top
View user's profile Send private message
FreeER
Master Cheater
Reputation: 12

Joined: 09 Aug 2013
Posts: 344

PostPosted: Thu Jul 20, 2017 6:18 am    Post subject: Reply with quote

mikey1993 wrote:
When I press 4 I get a pop up box with the message ":Error attempt to call a nill value"


That's because scriptfunction wasn't defined to be a function (so it was nil which causes lua to try and call a nil value, nil is lua's version of javascript's undefined aka not set), try

Code:
scriptfunction = function()
  DeadlyShock = 65575
  DemonStrike = 65576
  WhirlingDervish = 65577
  BastardSwing = 65578
  local PercentageSkill = readInteger("0x221101e")
  local PercentageSkill2 = readInteger("0x2211032")
  if (PercentageSkill == DeadlyShock) then
    writeInteger("0x221101e",DemonStrike)
  elseif (PercentageSkill == DemonStrike) then
    writeInteger("0x221101e",DeadlyShock)
  end
  if (PercentageSkill2 == WhirlingDervish) then
    writeInteger("0x2211032",BastardSwing)
  elseif (PercentageSkill2 == BastardSwing) then
    writeInteger("0x2211032",WhirlingDervish)
  end
end

createHotkey(scriptfunction, VK_NUMPAD4)


or

Code:
createHotkey(function()
  ... -- your code here
end, VK_NUMPAD4)
Back to top
View user's profile Send private message
mikey1993
Newbie cheater
Reputation: 0

Joined: 20 Jul 2017
Posts: 15

PostPosted: Thu Jul 20, 2017 7:02 am    Post subject: Reply with quote

Ok, I've tried both of them and neither work. For the 2nd example you showed me I get the error msg

"[string "createHotkey(function()
..."]:17: syntax error near ')'

It's referring to line 17 which I'll copy paste down below.
end VK_NUMPAD4)

sorry for being so nub. The solution is probably simple and I'm not seeing it Razz.

Thank you again for the help your giving me guys.
Back to top
View user's profile Send private message
FreeER
Master Cheater
Reputation: 12

Joined: 09 Aug 2013
Posts: 344

PostPosted: Thu Jul 20, 2017 7:18 am    Post subject: Reply with quote

there should be a comma between end and VK_NUMPAD4 to separate the two arguments (the function to run when the hotkey is run and the key that triggers the hotkey) Smile
Back to top
View user's profile Send private message
mikey1993
Newbie cheater
Reputation: 0

Joined: 20 Jul 2017
Posts: 15

PostPosted: Thu Jul 20, 2017 11:53 pm    Post subject: Reply with quote

Thank you so much FreeER. It's working now. It's not consistent though. 1 in 3 times that I press 4 nothing happens. The point of the script I have written is to free up 2 slots in my skill bar. I have 4 skills and 2 off them require a specific kind of weapon to use them so I was trying to set things up so that when I switch weapons the skills also switch out. Now when I switch weapons the weapon switches fine but the skills sometimes aren't switching. Is there a solution to this that you might know of ?

edit: I think I've found the issue. I checked my process list and there were 4-5 cheat engine processes running. Once I closed them all down and restarted the consistency appears to be 100%.

thank you for all the help you all have provided me. I very much appreciate it.
Back to top
View user's profile Send private message
mikey1993
Newbie cheater
Reputation: 0

Joined: 20 Jul 2017
Posts: 15

PostPosted: Tue Aug 01, 2017 8:12 pm    Post subject: Reply with quote

Is it possible to do something like this with hotkeys and if I haven't done it right could someone correct it for me.


createHotKey(function()

DoStuff


end, VK_NUMPAD1 + VK_LMENU)


In doing so give myself more options key options aka ALT + 1 through to 0 and so on. thanks for any replies.
Back to top
View user's profile Send private message
FreeER
Master Cheater
Reputation: 12

Joined: 09 Aug 2013
Posts: 344

PostPosted: Tue Aug 01, 2017 9:39 pm    Post subject: Reply with quote

Absolutely, you should just need a comma between the key codes so that they are separate parameters (up to 4 or 5 keys are supported if I recall correctly, though not sure if that's a software limit or simply the fact that most keyboards aren't designed to detect an unlimited number of keys being pressed)
Back to top
View user's profile Send private message
mikey1993
Newbie cheater
Reputation: 0

Joined: 20 Jul 2017
Posts: 15

PostPosted: Sat Aug 05, 2017 12:09 am    Post subject: Reply with quote

Thank you again FreeER. You were very right about that xd
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