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 


Am I getting the loop syntax wrong in this 'bot'?

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

Joined: 23 Sep 2017
Posts: 23
Location: United Kingdom

PostPosted: Tue May 18, 2021 8:12 am    Post subject: Am I getting the loop syntax wrong in this 'bot'? Reply with quote

I don't know if you'd call it a bot, nothing scary that'll run overnight or something. I'll paste the code below but explain what I'm trying to achieve, the different tables are a set of coordinates I need to be at for specific treasure points, and I want to values to each one, sleep, and then set the values to another table, etc.

I thought by wrapping it up all up in a for loop would enable me to control how many times it did this, but since I implemented, it's acting funny. Syntax wise, it works, but it makes the game act weird because it doesn't seem to be doing what I want in order.

It was fine before I wrapped it up in the loop with J as the iterator. Why would it change?

Edit: Oh I get it, it's running twice isn't it? because 0 and 1 are counted as two separate loops.

Code:
{$lua}
GetLuaEngine().MenuItem5.doClick()

[ENABLE]

local addressList = getAddressList()

function leftClick(x, y)
  setMousePos(x, y)
  Sleep(100)
  mouse_event(MOUSEEVENTF_LEFTDOWN)
  mouse_event(MOUSEEVENTF_LEFTUP)
end

local fStar = {3857, 66, 3857, 3537, 66, 3537, 3364, 47, 2059, 35, 2117, 1567, 35, 1567, 1450, 16}
local sStar = {4147, 71, 4147, 3827, 71, 3827, 3654, 52, 551, 9, 609, 59, 9, 59, 0, -10}
local tStar = {841, 14, 841, 521, 14, 521, 348, -5, 3103, 53, 3161, 2611, 53, 2611, 2494, 34}
local boss = {841, 14, 841, 521, 14, 521, 348, -4, 4031, 69, 4089, 3250, 69, 3250, 3132, 50}

for j = 0, 1
do
  for i = 0, 16
  do
     addressList[i].Value = fStar[i]
  end

  Sleep(250)

  for i = 0, 16
  do
     addressList[i].Value = sStar[i]
  end

  Sleep(250)

  for i = 0, 16
  do
     addressList[i].Value = tStar[i]
  end

  Sleep(250)

  for i = 0, 16
  do
     addressList[i].Value = boss[i]
  end

  Sleep(500)

  leftClick(1130, 304)

  Sleep(200)

  leftClick(1130, 304)
end

[DISABLE]
Back to top
View user's profile Send private message
ParkourPenguin
I post too much
Reputation: 140

Joined: 06 Jul 2014
Posts: 4289

PostPosted: Tue May 18, 2021 11:14 am    Post subject: Reply with quote

Bunny_Wabbit wrote:
Oh I get it, it's running twice isn't it? because 0 and 1 are counted as two separate loops.

Code:
for i=0,1 do
  print(i)
end
--[[output:
0
1
]]

Also, you're running that code during the syntaxcheck. Since that Lua block doesn't modify any AA code, it's fine to just return. Add this:
Code:
{$lua}
if syntaxcheck then return end
...

_________________
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
Bunny_Wabbit
Newbie cheater
Reputation: 0

Joined: 23 Sep 2017
Posts: 23
Location: United Kingdom

PostPosted: Tue May 18, 2021 1:22 pm    Post subject: Reply with quote

What's the Syntaxcheck?
Back to top
View user's profile Send private message
ParkourPenguin
I post too much
Reputation: 140

Joined: 06 Jul 2014
Posts: 4289

PostPosted: Tue May 18, 2021 2:15 pm    Post subject: Reply with quote

AA scripts are primarily for AA code. {$lua} blocks are supplemental in that they can add AA code to the script by returning a string. e.g.:
Code:
[ENABLE]
{$lua}
return 'define(foo,5)'
{$asm}
...

AA scripts go through a syntax check when saved (and just before executing IIRC) that checks and makes sure there aren't any obvious syntax errors in the script. Any {$lua} blocks would obviously need to be executed here in order to check the syntax of any code it returned. If the {$lua} block is only used for its side effects and doesn't return any code, it's often a good idea to forego execution of the Lua code during a syntax check.

CE assigns a true/false value to the variable syntaxcheck prior to executing the {$lua} block for this purpose.

_________________
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
Bunny_Wabbit
Newbie cheater
Reputation: 0

Joined: 23 Sep 2017
Posts: 23
Location: United Kingdom

PostPosted: Tue May 18, 2021 2:48 pm    Post subject: Reply with quote

I don't know if it's just a coincidence but ever since adding in that line of code, scripts have stopped executing after I click "okay" when modifying it.

I think I get it, so am I put everything under [ENABLE]? Because I'm sure I saw tutorials on YouTube saying to put it above.

Edit: I'm now getting a whole new problem, I've stopped using the outer loop because I'm only running once to test, but after line 79 Cheat Engine crashes thus pausing the bot. All I'm doing is using sleep() and my left click function. Do long scripts tend to crash after sometime?
Back to top
View user's profile Send private message
TheyCallMeTim13
Wiki Contributor
Reputation: 50

Joined: 24 Feb 2017
Posts: 976
Location: Pluto

PostPosted: Tue May 18, 2021 3:59 pm    Post subject: Reply with quote

Bunny_Wabbit wrote:
I don't know if it's just a coincidence but ever since adding in that line of code, scripts have stopped executing after I click "okay" when modifying it.
...

Yes, that's when the syntax check takes place.


Bunny_Wabbit wrote:
...
I think I get it, so am I put everything under [ENABLE]? Because I'm sure I saw tutorials on YouTube saying to put it above.
...

Depend on when you want to script code to run. If you want it to run when enabling and disabling, put it in the main section. If you want it to run when enabling then put it in the enable section. And if you want it to run when disabling then put it in the disable section.


Bunny_Wabbit wrote:
...
Edit: I'm now getting a whole new problem, I've stopped using the outer loop because I'm only running once to test, but after line 79 Cheat Engine crashes thus pausing the bot. All I'm doing is using sleep() and my left click function. Do long scripts tend to crash after sometime?

The script you posted only has 57 lines of code, so you must have added something to the script.

_________________
Back to top
View user's profile Send private message Visit poster's website
Bunny_Wabbit
Newbie cheater
Reputation: 0

Joined: 23 Sep 2017
Posts: 23
Location: United Kingdom

PostPosted: Tue May 18, 2021 4:13 pm    Post subject: Reply with quote

TheyCallMeTim13 wrote:

The script you posted only has 57 lines of code, so you must have added something to the script.


I did yeah, I just didn't want to take up too much space. But anyway I'll post it here. It just seems to give up after a few left clicks. The last click after Sleep(100) was just clicking on Google's stopwatch on my second monitor, not that it matters because it crashes before then.

Code:
{$lua}
if syntaxcheck then return end

GetLuaEngine().MenuItem5.doClick()

[ENABLE]

local addressList = getAddressList()

function leftClick(x, y)
  setMousePos(x, y)
  sleep(100)
  mouse_event(MOUSEEVENTF_LEFTDOWN)
  mouse_event(MOUSEEVENTF_LEFTUP)
end

local fStar = {3857, 66, 3857, 3537, 66, 3537, 3364, 47, 2059, 35, 2117, 1567, 35, 1567, 1450, 16}
local sStar = {4147, 71, 4147, 3827, 71, 3827, 3654, 52, 551, 9, 609, 59, 9, 59, 0, -10}
local tStar = {841, 14, 841, 521, 14, 521, 348, -5, 3103, 53, 3161, 2611, 53, 2611, 2494, 34}
local boss = {841, 14, 841, 521, 14, 521, 348, -4, 4031, 69, 4089, 3250, 69, 3250, 3132, 50}

for i = 0, 16
do
  addressList[i].Value = fStar[i]
end

Sleep(250)

for i = 0, 16
do
  addressList[i].Value = sStar[i]
end

Sleep(250)

for i = 0, 16
do
  addressList[i].Value = tStar[i]
end

Sleep(250)

for i = 0, 16
do
  addressList[i].Value = boss[i]
end

Sleep(500)

leftClick(1130, 304)

Sleep(200)

leftClick(1130, 304)

Sleep(2750)

leftClick(930,160)

Sleep(2450)
leftClick(797, 804)

Sleep(12700)
leftClick(797, 804)

Sleep(2500)
leftClick(1105, 412)

Sleep(800)
leftClick(1105, 412)

Sleep(10490)
leftClick(920, 473)

Sleep(10420)
leftClick(920, 473)

Sleep(10420)
leftClick(935, 909)

Sleep(100)
leftClick(2148, 490)

[DISABLE]
Back to top
View user's profile Send private message
ParkourPenguin
I post too much
Reputation: 140

Joined: 06 Jul 2014
Posts: 4289

PostPosted: Tue May 18, 2021 4:50 pm    Post subject: Reply with quote

Lua arrays by convention start at index 1. Unfortunately, CE doesn't adhere to that convention and starts many array-like structures at 0.

That script assumes the first 17 entries in the address list will always be ordered a certain way.

You're accessing 17 address list objects in each loop but you only have arrays of 16 numbers to read from.

_________________
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
Bunny_Wabbit
Newbie cheater
Reputation: 0

Joined: 23 Sep 2017
Posts: 23
Location: United Kingdom

PostPosted: Wed May 19, 2021 4:02 am    Post subject: Reply with quote

ParkourPenguin wrote:
Lua arrays by convention start at index 1. Unfortunately, CE doesn't adhere to that convention and starts many array-like structures at 0.

That script assumes the first 17 entries in the address list will always be ordered a certain way.

You're accessing 17 address list objects in each loop but you only have arrays of 16 numbers to read from.


Hah, oh yeah, I forgot about the table indexing. The script is still freezing through before it even finishes. I'm not sure if it's normal for Cheat Engine to be unclickable while it runs, I think I read it is. Problem is though is that I measured the miliseconds which are mostly accurate (minus 100 miliseconds as you can see in my script before putting the timer on), so I know something is struggling.
Back to top
View user's profile Send private message
Dark Byte
Site Admin
Reputation: 458

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

PostPosted: Wed May 19, 2021 4:06 am    Post subject: Reply with quote

make sure the memory record is set to be an async record as you should never use sleep() in the main GUI

(but do use synchronize() to access the addresslist)

alternatively, use timers that spawn other timers in sequence till done
Code:

createTimer(1000,function()
  print("after 1 second")
  createTimer(1000, function()
    print("after 2 seconds")

    createTimer(2000,function()
      print("after 4 seconds")
    end)

  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
Bunny_Wabbit
Newbie cheater
Reputation: 0

Joined: 23 Sep 2017
Posts: 23
Location: United Kingdom

PostPosted: Wed May 19, 2021 4:12 am    Post subject: Reply with quote

Aha! I figured out my freezing problem, nothing was broken I had incorrectly used 10 second Sleeps which was like half a minute. I thought Sleep() -was- the timer, lol. If I replaced all my sleep functions with timers, would I have to rewrite the whole thing?

(Like if you're using ImageSearch in AutoHotkey, you end up with a million nested loops)
Back to top
View user's profile Send private message
Dark Byte
Site Admin
Reputation: 458

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

PostPosted: Wed May 19, 2021 4:28 am    Post subject: Reply with quote

yeah, rewrite with a bunch of nested calls.
or as I said, mark the record as an async ("Execute asynchronious") record so CE stays active

but stuff that does access the GUI needs to be wrapped in a synchronize call
e.g
Code:

for i = 0, 16
do
  addressList[i].Value = fStar[i]
end

should then be
Code:

synchronize(function()
  for i = 0, 16
  do
    addressList[i].Value = fStar[i]
  end
end)


(Stuff like leftClick is fine though)

_________________
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