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 do I write this script so that I can use it twice?

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

Joined: 09 Feb 2022
Posts: 4

PostPosted: Wed Feb 09, 2022 8:14 pm    Post subject: How do I write this script so that I can use it twice? Reply with quote

I have very little knowledge of how to write these scripts, I copied this one from the forums here and obviously changed the address and file name...but other than that I didn't change anything else

Here's the script:
tableWithValues = {}
for line in io.lines("my.txt") do
table.insert(tableWithValues,tonumber(line))
end

hk1 = createHotkey("writeValueFromList", VK_A, ...)

function writeValueFromList()
writeInteger('216CD224', tableWithValues[math.random(1, #tableWithValues)] )
end

My I tried copy pasting it underneath it, but also changing the hotkey, the file name, the address and changing "TableWithValues" to "TableWithValues2"

But what this did is assign both hotkeys to the 2nd address

Obviously I'm doing something wrong, can you tell me how to make copies of this script so that I can affect multiple addresses through multiple files?

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

Joined: 06 Jul 2014
Posts: 4702

PostPosted: Wed Feb 09, 2022 10:29 pm    Post subject: Reply with quote

Pass an anonymous function to createHotkey:
Code:
createHotkey(function()
  ...
end, VK_WHATEVER, ...)

Also, make sure you're destroying the previous hotkey.
Code:
if hk1 then hk1.destroy(); hk1 = nil end
hk1 = createHotkey(...)

_________________
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
LeFiXER
Grandmaster Cheater Supreme
Reputation: 20

Joined: 02 Sep 2011
Posts: 1069
Location: 0x90

PostPosted: Thu Feb 10, 2022 8:49 am    Post subject: Reply with quote

orwasaker wrote:

When you put "..." between the end and the createhotkey function...do you mean that there's something I need to put there that you assume I already know?

I don't know where exactly to put any of this, can you just copy paste the code I wrote and insert what you wrote inside it so I know where it fits?


That's right, you put the code of the function you want to execute upon pressing the hotkey.
Code:

writeInteger('216CD224', tableWithValues[math.random(1, #tableWithValues)] )
Back to top
View user's profile Send private message
orwasaker
How do I cheat?
Reputation: 0

Joined: 09 Feb 2022
Posts: 4

PostPosted: Thu Feb 10, 2022 8:52 am    Post subject: Reply with quote

LeFiXER wrote:
orwasaker wrote:

When you put "..." between the end and the createhotkey function...do you mean that there's something I need to put there that you assume I already know?

I don't know where exactly to put any of this, can you just copy paste the code I wrote and insert what you wrote inside it so I know where it fits?


That's right, you put the code of the function you want to execute upon pressing the hotkey.
Code:

writeInteger('216CD224', tableWithValues[math.random(1, #tableWithValues)] )


Sorry I deleted the previous message because I thought you hadn't replied to it yet

Anyway I got what I want by simple changing the "WriteFromList" part...by just adding an S to it...that's it that's what made both codes now work as intended (and obviously still changing the address and the hotkey and naming it hk2)

_________________
none
Back to top
View user's profile Send private message
AylinCE
Grandmaster Cheater Supreme
Reputation: 37

Joined: 16 Feb 2017
Posts: 1522

PostPosted: Thu Feb 10, 2022 9:22 am    Post subject: Reply with quote

You should see the entire code you copied.
Examples may not be complete code.

Code:
tableWithValues = {}
for line in io.lines("my.txt") do --my.txt ? (ERROR!=No such file or directory) Path + my.txt ("C:\Users\UserName\Folder\YourList.txt")
table.insert(tableWithValues,tonumber(line))
end


Except for a few things that are wrong, the shortcut functions should be:

Code:
function writeValueFromList1()
writeInteger('216CD224', tableWithValues[math.random(1, #tableWithValues)] ) --This is a very complex value assignment. I assume you know what this is doing!
end

function writeValueFromList2()
writeInteger('216CD224', tableWithValues[math.random(1, #tableWithValues)] )
end

--or
function allWriteValueFromList()
writeValueFromList1()
writeValueFromList2()
end

if hk1 then hk1.destroy(); hk1 = nil end
hk1 = createHotkey(writeValueFromList1, VK_A)

if hk2 then hk2.destroy(); hk2 = nil end
hk2 = createHotkey(writeValueFromList2, VK_B)

if hk3 then hk3.destroy(); hk3 = nil end
hk3 = createHotkey(allWriteValueFromList, VK_C)

_________________
Hi Hitler Different Trainer forms for you!
https://forum.cheatengine.org/viewtopic.php?t=619279
Enthusiastic people: Always one step ahead
Do not underestimate me Master: You were a beginner in the past
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
orwasaker
How do I cheat?
Reputation: 0

Joined: 09 Feb 2022
Posts: 4

PostPosted: Thu Feb 10, 2022 9:42 am    Post subject: Reply with quote

AylinCE wrote:
You should see the entire code you copied.
Examples may not be complete code


It was the entire code, and it did the job I wanted it to do

AylinCE wrote:
This is a very complex value assignment. I assume you know what this is doing!


I...do? is it really complex if all it does it pick a random value from the already created table of values, and assigns it to the address?

About the rest: thank you very much for that, though as I explained in the previous reply I already fixed it so that it does what I want it to do

Since I have you guys here, I have an additional question:

In this line: writeInteger('216CD1D4', tableWithValues[math.random(1, #tableWithValues)] )

How do I write it so that it gives the SAME random value to TWO addresses?

_________________
none
Back to top
View user's profile Send private message
AylinCE
Grandmaster Cheater Supreme
Reputation: 37

Joined: 16 Feb 2017
Posts: 1522

PostPosted: Thu Feb 10, 2022 10:02 am    Post subject: Reply with quote

Code:
function writeValueFromList()
local val=tableWithValues[math.random(1, #tableWithValues)]
writeInteger('216CD224', tonumber(val)) --address 1
writeInteger('216CD224', tonumber(val)) --address 2
end

_________________
Hi Hitler Different Trainer forms for you!
https://forum.cheatengine.org/viewtopic.php?t=619279
Enthusiastic people: Always one step ahead
Do not underestimate me Master: You were a beginner in the past
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
orwasaker
How do I cheat?
Reputation: 0

Joined: 09 Feb 2022
Posts: 4

PostPosted: Thu Feb 10, 2022 10:13 am    Post subject: Reply with quote

AylinCE wrote:
Code:
function writeValueFromList()
local val=tableWithValues[math.random(1, #tableWithValues)]
writeInteger('216CD224', tonumber(val)) --address 1
writeInteger('216CD224', tonumber(val)) --address 2
end


Thanks a lot, this worked perfectly Smile

_________________
none
Back to top
View user's profile Send private message
panraven
Grandmaster Cheater
Reputation: 62

Joined: 01 Oct 2008
Posts: 958

PostPosted: Thu Feb 10, 2022 11:25 am    Post subject: Reply with quote

Code:

--
hkStore = hkStore or {}

-- if dataFile is unique for each hotkey, it can be used as id like
--function hkRndWrite(dataFile, address, ...) -- ... is key combination
--  local id = dataFile
--  local hk , data= hkStore[id], {}

function hkRndWrite(id, dataFile, address, ...) -- ... is key combination
  local hk, data = hkStore[id], {}
  if hk and hk.Destroy then hkStore[id]=nil, hk.Destroy()end-- remove old hotkey object of same id
  -- for test, replace: for l in io.lines(dataFile)do data[1+#data] = tonumber(l) end
  for l in dataFile:gmatch'[%+%-]?%d+'do data[1+#data] = tonumber(l)end
  if #data == 0 then
    print('no data from '..dataFile)
    return
  else
    hk = createHotkey(function(hkself)
      --- for test, replace: writeInteger(address, data[math.random(1,#data)])
      print(address, data[math.random(1,#data)])
    end, ... ) --<-- setup key combination here
  end
  hkStore[id] = hk
  return hk
end
-- define above once somewhere
--
--
-- test
--
-- reuse function
local a = hkRndWrite('test1','123 456 789', 'Addr1', VK_MBUTTON , VK_LCONTROL)
local b = hkRndWrite('test2','-123 -456 -7a89', 'Addr2', VK_MBUTTON , VK_LMENU)
createTimer(20000, function()for k,v in pairs(hkStore) do hkStore[k]=nil,v.Destroy() end print'gone' end)

_________________
- Retarded.
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