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 


Issue with os.time()

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

Joined: 08 Jun 2018
Posts: 25

PostPosted: Sat Dec 15, 2018 10:52 am    Post subject: Issue with os.time() Reply with quote

I'm stumped as to why my script isn't working how I want it to. In this 1st example it works properly.

local StartTime = os.time()
local EndTime = StartTime+10


timer = createTimer(nil, true)
timer_setInterval(timer, 1)
function Loop()


if os.time() >= endtime then
writeBytes ("[Game.exe + 01F5DDA4] + 34A",255)
end



end
timer_onTimer(timer, Loop)



But in this 2nd example it wont work and I cant figure out why.

local TimeToStart = 0
local StartTime = 0
local EndTime = 0


timer = createTimer(nil, true)
timer_setInterval(timer, 1)
function Loop()


if isKeyPressed (0x32) then
local StartTime = os.time()
local EndTime = StartTime+10
TimeToStart = 1
elseif os.time() >= EndTime and (TimeToStart == 1) then
writeBytes ("[Game.exe + 01F5DDA4] + 34A",255)
end



end
timer_onTimer(timer, Loop)


Any help with understanding why it isn't working in the 2nd example would be hugely appreciated. Thanks in advance
Back to top
View user's profile Send private message
Dark Byte
Site Admin
Reputation: 360

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

PostPosted: Sat Dec 15, 2018 12:21 pm    Post subject: Reply with quote

it doesn't write the value ever or constantly?

try adding a print to that write or other places in the loop to figure out what is going on


also whenever i'm not sure i add braces

e.g:
os.time() >= (EndTime and (TimeToStart == 1))
or
(os.time() >= EndTime) and (TimeToStart == 1)

_________________
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
MikeNoey
Cheater
Reputation: 0

Joined: 08 Jun 2018
Posts: 25

PostPosted: Sat Dec 15, 2018 12:40 pm    Post subject: Reply with quote

Hey darkbyte. I'm sorry I neglected to give more information on what was happening.

Well with the 1st example the code is executing after the 10 seconds has passed but with the 2nd example the code is executing immediately and ignoring (os.time() >= EndTime)

It must have something to do with running within the Create timer. that's the only real difference between the 1st and 2nd example
Back to top
View user's profile Send private message
ParkourPenguin
I post too much
Reputation: 67

Joined: 06 Jul 2014
Posts: 2143

PostPosted: Sat Dec 15, 2018 2:21 pm    Post subject: Reply with quote

You're redeclaring StartTime and EndTime in a new scope. At the elseif, EndTime is still going to be 0.
Code:
local TimeToStart = false
local StartTime
local EndTime

if timer then timer.destroy(); timer = nil end

timer = createTimer(nil, true)
timer.Interval = 1
timer.OnTimer = function(t)
  if isKeyPressed(0x32) then
    StartTime = os.time()
    EndTime = StartTime + 10
    TimeToStart = true
  elseif TimeToStart and os.time() >= EndTime then
    writeBytes("[Game.exe + 01F5DDA4] + 34A", 0xff)
  end
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
MikeNoey
Cheater
Reputation: 0

Joined: 08 Jun 2018
Posts: 25

PostPosted: Sat Dec 15, 2018 2:35 pm    Post subject: Reply with quote

Thanks a million for the help. You are 100% correct on that. Working fine now for me.
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