paul44 Expert Cheater
Reputation: 3
Joined: 20 Jul 2017 Posts: 220
|
Posted: Sat Jun 27, 2026 3:38 am Post subject: Bypass error for non-existing item in table |
|
|
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 )
=> 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
|
|