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 


Bypass error for non-existing item in table

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

Joined: 20 Jul 2017
Posts: 220

PostPosted: Sat Jun 27, 2026 3:38 am    Post subject: Bypass error for non-existing item in table Reply with quote

Before anything else, I've posted my solution below. But my guts feeling tells me this can be done far more easily ?! Did some googling, but nothing workable came around (or my wording is just "off")...

Type of table:
aMaterials = {
{'Fuel Resources',0x0}, {'Biological Material',0x4}, {'Common Metal',0x8},
{'Minor Metal',0xC}, {'Precious Metal',0x10}, }

Here is my problem: to get particular info from an item, i obtain it by using the format: sDescr = aMaterials[0x4][1] (for example)
=> items are added per 'id' here: eg aMaterials[0x4] = { ... }
Unfortunately, this 'Id' value is collected from the item's memory_record. and while ok in 99% of the cases, it is that 1% causing the error !

My approach: i use pcall to check/handle error (see below)?!
Unfortunately, i can not remember the exact error (not nil - but either 'index'-error or ?field- error ? or something Embarassed )
=> i can get you the original error if needed though

-----------
Solution:
local function chkArray(aArray, iVal) return aArray[iVal][1] end
local sVal = "<None>"
local status, err = pcall(function() chkArray(aBlueprnt, Dev_Req_Bluepr) end)
if status then sVal = aBlueprnt[Dev_Req_Bluepr][1] end
f.Dev_Req_Bluepr_cb.Text = sVal
Back to top
View user's profile Send private message
ParkourPenguin
I post too much
Reputation: 155

Joined: 06 Jul 2014
Posts: 4771

PostPosted: Sat Jun 27, 2026 12:23 pm    Post subject: Reply with quote

paul44 wrote:
Unfortunately, i can not remember the exact error

Attempt to index a nil value

Check if it's nil
Code:
local material = aBlueprnt[Dev_Req_Bluepr]
if not material then
  -- material does not exist in the table: either get that info or do something else
  f.Dev_Req_Bluepr_cb.Text = "<None>"
else
  f.Dev_Req_Bluepr_cb.Text = material[1]
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
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