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 


Lau Decreaseing value

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

Joined: 25 Jan 2014
Posts: 73

PostPosted: Tue Aug 11, 2015 8:25 am    Post subject: Lau Decreaseing value Reply with quote

Hi, i try to decrease float value with lia script but cant
I want while key pressed value will decrease

this script i tryed
**********************************************
al = getAddressList()
FORWARDLEFTUP = al.getMemoryRecordByDescription("FORWARDLEFTUP")
function checkKeys(timer)

if (isKeyPressed(VK_NUMPAD1) and isKeyPressed(VK_SUBTRACT)) then
A = FORWARDLEFTUP.Value
B = "0.003"
C = A-B
FORWARDLEFTUP.Value=C
end
end
t=createTimer(nil)
timer_setInterval(t, 100)
timer_onTimer(t, checkKeys)
timer_setEnabled(t, true)
*********************************************************

Value alaways decrease to aboute zero but under zero (for example -1 or - 1.1) cant. If i try increase it work fine. In firstly FORWARDLEFTUP.Value has value aboue 0.5.
Back to top
View user's profile Send private message
Redouane
Master Cheater
Reputation: 3

Joined: 05 Sep 2013
Posts: 363
Location: Algeria

PostPosted: Tue Aug 11, 2015 12:48 pm    Post subject: Re: Lua Decreaseing value Reply with quote

I think this is a bug,because it works just fine with variables declared with Lua,here is a way to reproduce it:
FORWARDLEFTUP is a memory record.
Code:

x = FORWARDLEFTUP.Value; -- equals (-0.001)
FORWARDLEFTUP.Value = FORWARDLEFTUP.Value - 0.003
x = x - 0.003;
FORWARDLEFTUP.Value = FORWARDLEFTUP.Value - 0.003
x = x - 0.003;
FORWARDLEFTUP.Value = FORWARDLEFTUP.Value - 0.003
x = x - 0.003;
FORWARDLEFTUP.Value = FORWARDLEFTUP.Value - 0.003
x = x - 0.003;
FORWARDLEFTUP.Value = FORWARDLEFTUP.Value - 0.003
x = x - 0.003;
return x == FORWARDLEFTUP.Value

@DarkByte
the code prints false to the console !

A few tips to improve your code:
Code:

FORWARDLEFTUP = getAddressList().getMemoryRecordByDescription("FORWARDLEFTUP")
local hotkey = createHotkey(function ()
            -- No need for A,B and C
            FORWARDLEFTUP.Value = FORWARDLEFTUP.Value - 0.003;
            end,VK_1,VK_SUBTRACT);
Back to top
View user's profile Send private message
Zanzer
I post too much
Reputation: 126

Joined: 09 Jun 2013
Posts: 3278

PostPosted: Tue Aug 11, 2015 5:33 pm    Post subject: Reply with quote

Sounds like a rounding issue when converting between floats and strings. Use:
Code:
writeFloat(FORWARDLEFTUP.CurrentAddress, readFloat(FORWARDLEFTUP.CurrentAddress) - 0.003)
Back to top
View user's profile Send private message
AntonVit
Advanced Cheater
Reputation: 0

Joined: 25 Jan 2014
Posts: 73

PostPosted: Wed Aug 12, 2015 3:00 am    Post subject: Reply with quote

Thanks
Tryed
*******************************************
Code:
al = getAddressList()
FORWARDLEFTUP = al.getMemoryRecordByDescription("FORWARDLEFTUP")
function checkKeys(timer)
if (isKeyPressed(VK_NUMPAD1) and isKeyPressed(VK_SUBTRACT)) then

x = FORWARDLEFTUP.Value; -- equals (-0.001)
FORWARDLEFTUP.Value = FORWARDLEFTUP.Value - 0.003
x = x - 0.003;
FORWARDLEFTUP.Value = FORWARDLEFTUP.Value - 0.003
x = x - 0.003;
FORWARDLEFTUP.Value = FORWARDLEFTUP.Value - 0.003
x = x - 0.003;
FORWARDLEFTUP.Value = FORWARDLEFTUP.Value - 0.003
x = x - 0.003;
FORWARDLEFTUP.Value = FORWARDLEFTUP.Value - 0.003
x = x - 0.003;
return x == FORWARDLEFTUP.Value
end
end
t=createTimer(nil)
timer_setInterval(t, 100)
timer_onTimer(t, checkKeys)
timer_setEnabled(t, true)


but value decrease again to aboute zero and not under


and tryed
Code:
al = getAddressList()
FORWARDLEFTUP = al.getMemoryRecordByDescription("FORWARDLEFTUP")
function checkKeys(timer)
if (isKeyPressed(VK_NUMPAD1) and isKeyPressed(VK_SUBTRACT)) then

writeFloat("53F6BF44", readFloat("53F6BF44") - 0.003)
end
end
t=createTimer(nil)
timer_setInterval(t, 100)
timer_onTimer(t, checkKeys)
timer_setEnabled(t, true)


work fine

Thanks You all for help. Now it work
Back to top
View user's profile Send private message
Dark Byte
Site Admin
Reputation: 458

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

PostPosted: Wed Aug 12, 2015 3:20 am    Post subject: Reply with quote

It's fixed in the next lua version that comes with ce

also, this script is the same:
Code:

v="-0.001"
x=v
v=tostring(v-0.003)
x=x-0.003
v=tostring(v-0.003)
x=x-0.003
v=tostring(v-0.003)
x=x-0.003
v=tostring(v-0.003)
x=x-0.003

print(x)
print(v)

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

Joined: 25 Jan 2014
Posts: 73

PostPosted: Thu Aug 20, 2015 4:25 am    Post subject: Reply with quote

Dark Byte wrote:
It's fixed in the next lua version that comes with ce

also, this script is the same:
Code:

v="-0.001"
x=v
v=tostring(v-0.003)
x=x-0.003
v=tostring(v-0.003)
x=x-0.003
v=tostring(v-0.003)
x=x-0.003
v=tostring(v-0.003)
x=x-0.003

print(x)
print(v)


In main page of site link (button) for new version of CE (there is 6.4) Or i need wait some for new CE (6.5) in which new fixed lua?
Back to top
View user's profile Send private message
Dark Byte
Site Admin
Reputation: 458

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

PostPosted: Thu Aug 20, 2015 4:35 am    Post subject: Reply with quote

Yes, wait for 6.5
For now just work around it

_________________
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
mgr.inz.Player
I post too much
Reputation: 218

Joined: 07 Nov 2008
Posts: 4438
Location: W kraju nad Wisla. UTC+01:00

PostPosted: Fri Aug 21, 2015 1:22 pm    Post subject: Reply with quote

In Lua5.1 and before, tonumber function and automatic string conversion (from string to number) has a bug.
It doesn't like "0" (zero) between "-" (minus) and "." (decimal separator).

This bug was reported here:
http://cheatengine.org/mantis/view.php?id=328




We can fix tonumber function with this:
Code:
if original_tonumber==nil then
  original_tonumber=tonumber
end

function tonumber(value)
  value = value:gsub( "%-0+%." , "-." ):gsub("%-%-","")
  return original_tonumber(value)
end

first gsub replaces all "-0." with "-." (also any amount of zeros between minus and decimal separator)
second gsub replaces all "--" into empty string

From now on, it will recognize "-0.234" and "-000.234" and "---0.234" and ..... (you get the idea)
as number -0.234




Now just use corrected tonumber function (automatic conversion is still broken):
Code:
v="-0.001"
v=tostring(tonumber(v)-0.003)
v=tostring(tonumber(v)-0.003)
v=tostring(tonumber(v)-0.003)
v=tostring(tonumber(v)-0.003)





So, for you it will be:
Code:
FORWARDLEFTUP.Value = tostring(tonumber(FORWARDLEFTUP.Value) - 0.003)

_________________
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