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 


Boolean as a value

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

Joined: 17 Jan 2018
Posts: 205

PostPosted: Sun Aug 09, 2020 8:04 am    Post subject: Boolean as a value Reply with quote

I want to ask 1 question about boolean.
Code:
if value == 1 then
UDF1.CECheckBox1.Checked = true
elseif value == 0 then
UDF1.CECheckBox1.Checked = false
end

Is it possible not to use if elseif in the above code, like this: UDF1.CECheckBox1.Checked = value ? (without using the state property of the checkbox)
I want to use something like this: Checked = (value=='1') or Checked = tonumber(value) insted of: true, false
And how to write with checkbox checked property without using if elsif and state property ?:
Code:
UDF1.CECheckBox1.OnClick = function(sender)
if UDF1.CECheckBox1.Checked == true then
writeBytes(0x9E91B4, 1)
elseif UDF1.CECheckBox1.Checked == false then
writeBytes(0x9E91B4, 0)
end
end


2-nd question: When the form is created, the form has a KeyPreview property. What is it and how can it be used? Did not found any information about this.
Back to top
View user's profile Send private message
AylinCE
Grandmaster Cheater Supreme
Reputation: 37

Joined: 16 Feb 2017
Posts: 1549

PostPosted: Sun Aug 09, 2020 8:48 am    Post subject: Reply with quote

Code:
local value = 0

UDF1.CECheckBox1.OnClick = function(sender)
if value == 1 then
writeBytes (0x9E91B4, 1)
UDF1.CECheckBox1.Checked = true end
if value == 0 then
writeBytes (0x9E91B4, 0)
UDF1.CECheckBox1.Checked = false end
end


The "KeyPreview" event may be running in VBScript (true, false).
But I am not sure if it is active on CE Lua.
I would like to see this if there is an example.

_________________
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
Razi
Expert Cheater
Reputation: 1

Joined: 17 Jan 2018
Posts: 205

PostPosted: Sun Aug 09, 2020 10:37 am    Post subject: Reply with quote

I want to: convert boolean to value and value to boolean:
where, true = 1, false = 0:
Code:
local value = readBytes(0x9E91B4)  --value can be 1 or 0
UDF1.CECheckBox1.Checked = tostring(value)

UDF1.CECheckBox1.OnClick = function(sender)
local value = tonumber(UDF1.CECheckBox1.Checked)
writeBytes(0x9E91B4, value)
end

but it does not work
Back to top
View user's profile Send private message
ParkourPenguin
I post too much
Reputation: 153

Joined: 06 Jul 2014
Posts: 4734

PostPosted: Sun Aug 09, 2020 10:54 am    Post subject: Reply with quote

Lua isn't C. It's intended 0 will evaluate to true like any other integer. You need to be explicit about it:
Code:
something.checked = value ~= 0

value = something.checked and 1 or 0

_________________
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
Razi
Expert Cheater
Reputation: 1

Joined: 17 Jan 2018
Posts: 205

PostPosted: Sun Aug 09, 2020 11:52 am    Post subject: Reply with quote

ParkourPenguin wrote:
Code:
something.checked = value ~= 0

value = something.checked and 1 or 0


Thank you, this is what was needed.
Found three ways to do it:
1)
Code:
--read
local value1 = readBytes(0x970AF2)
UDF1.CECheckBox1.Checked = value1 ~= 0

--write
UDF1.CECheckBox1.OnClick = function(sender)
local value = UDF1.CECheckBox1.Checked and 1 or 0
writeBytes(0x970AF2, value)
end


2)
Code:
function number_to_boolean(value1)
  return value1 ~= 0
end

function boolean_to_number(value2)
  return value2 and 1 or 0
end

--read
local value1 = readBytes(0x970AF2)
UDF1.CECheckBox1.Checked = number_to_boolean(value1)

--write
UDF1.CECheckBox1.OnClick = function(sender)
local value = boolean_to_number(UDF1.CECheckBox1.Checked)
writeBytes(0x970AF2, value)
end


3)
Code:
boolean_to_number_tbl={[true]=1, [false]=0}
number_to_boolean_tbl={[1]=true, [0]=false}

--read
local value1 = readBytes(0x970AF2)
UDF1.CECheckBox1.Checked = number_to_boolean_tbl[value1]

--write
UDF1.CECheckBox1.OnClick = function(sender)
local value = boolean_to_number_tbl[UDF1.CECheckBox1.Checked]
writeBytes(0x970AF2, value)
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