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 


__eq fails

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

Joined: 22 Jun 2021
Posts: 510

PostPosted: Sun Sep 04, 2022 10:17 am    Post subject: __eq fails Reply with quote

Code:
function PVehicle.__eq(a, b)
  if type(b) == "number" then
    return tonumber(a:Get()) == tonumber(getAddress(b))
  end
  return a:Get() == b:Get()
end

return VehicleArray.GetPlayerVehicle() == getAddress(addr) <- false
return PVehicle.__eq(VehicleArray.GetPlayerVehicle(), getAddress(addr)) <- true

i can't understand why it fails giving me false
Back to top
View user's profile Send private message
Dark Byte
Site Admin
Reputation: 470

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

PostPosted: Sun Sep 04, 2022 11:32 am    Post subject: Reply with quote

__eq must be part of the metatable of the object GetPlayerVehicle() returns

in doubt, you can always print out info

_________________
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


Last edited by Dark Byte on Sun Sep 04, 2022 11:34 am; edited 1 time in total
Back to top
View user's profile Send private message MSN Messenger
Frouk
Grandmaster Cheater
Reputation: 5

Joined: 22 Jun 2021
Posts: 510

PostPosted: Sun Sep 04, 2022 11:33 am    Post subject: Reply with quote

function returns PVehicle class
maybe something wrong with constructor
Code:
function PVehicle:new(addr)
  local o = {}
  o.addr = getAddress(addr)
  self.__index = self
  setmetatable(o, self)
  return o
end
Back to top
View user's profile Send private message
Dark Byte
Site Admin
Reputation: 470

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

PostPosted: Sun Sep 04, 2022 11:35 am    Post subject: Reply with quote

does self.__eq exist at that point?
_________________
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
Frouk
Grandmaster Cheater
Reputation: 5

Joined: 22 Jun 2021
Posts: 510

PostPosted: Sun Sep 04, 2022 11:39 am    Post subject: Reply with quote

Dark Byte wrote:
does self.__eq exist at that point?

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

Joined: 06 Jul 2014
Posts: 4702

PostPosted: Sun Sep 04, 2022 12:05 pm    Post subject: Reply with quote

May you provide a better example? Preferably an SSCCE
example:
Code:
local t = {}

function t.__eq(lhs, rhs)
  return lhs.x == rhs.x
end

function t:new(x)
  self.__index = self
  return setmetatable({x = x}, self)
end

print(tostring(t:new(1) == t:new(1)))  -- prints "true"
print(tostring(t:new(1) == t:new(2)))  -- prints "false"
This works fine for me.
_________________
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
Frouk
Grandmaster Cheater
Reputation: 5

Joined: 22 Jun 2021
Posts: 510

PostPosted: Sun Sep 04, 2022 12:13 pm    Post subject: Reply with quote

Code:
PVehicle = {}
VehicleArray = {}

function PVehicle:new(addr)
  local o = {}
  o.addr = getAddress(addr)
  self.__index = self
  setmetatable(o, self)
  return o
end

function PVehicle.__eq(a, b)
  if type(b) == "number" then
    return tonumber(getAddress(a.addr)) == tonumber(getAddress(b))
  end
  return a:Get() == b:Get()
end

function PVehicle:Get()
  return getAddress(self.addr)
end

function VehicleArray.GetArray()
  local maxCount = readInteger(0xA9F158 + 0x8)
  local currentCount = readInteger(0xA9F158 + 0xC)
  local array = {}
  for i = 0, currentCount - 1 do
    array[i] = readPointer(0xA9F158 + 0x10 + i * 4)
  end
  return array
end

function VehicleArray.GetPVehicle(index)
  local arr = VehicleArray.GetArray()
  return PVehicle:new(arr[index] - 0xB8)
end

function VehicleArray.GetPlayerVehicle()
  return VehicleArray.GetPVehicle(0)
end

i'm trying to compare object instance to address which fails
Code:
--[[
output:
local player = VehicleArray.GetPlayerVehicle()
--[0xA9F168] - 0xB8
local player1 = PVehicle:new("[0xA9F168] - 0xB8")
return player == getAddress("[0xA9F168] - 0xB8")

1:false // fails

local player = VehicleArray.GetPlayerVehicle()
--[0xA9F168] - 0xB8
local player1 = PVehicle:new("[0xA9F168] - 0xB8")
return player == player1

1:true // ?
--]]
Back to top
View user's profile Send private message
Dark Byte
Site Admin
Reputation: 470

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

PostPosted: Sun Sep 04, 2022 12:41 pm    Post subject: Reply with quote

try replacing
Code:

return tonumber(getAddress(a.addr)) == tonumber(getAddress(b))

with

Code:
 
print("b==number")
return a.addr == b

_________________
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
ParkourPenguin
I post too much
Reputation: 152

Joined: 06 Jul 2014
Posts: 4702

PostPosted: Sun Sep 04, 2022 12:55 pm    Post subject: Reply with quote

Oh, now I see it.

Lua 5.3 reference manual:
Quote:
__eq: the equal (==) operation. Behavior similar to the addition operation, except that Lua will try a metamethod only when the values being compared are either both tables or both full userdata and they are not primitively equal. The result of the call is always converted to a boolean.
(added emphasis is mine)

So you can't use __eq to compare a table with an integer (getAddress returns an integer).

_________________
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
Frouk
Grandmaster Cheater
Reputation: 5

Joined: 22 Jun 2021
Posts: 510

PostPosted: Sun Sep 04, 2022 12:56 pm    Post subject: Reply with quote

^^^
edit:
well, can't seem do anything with this
edit 2:
i need to create function that compares the address
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