Jump to content

Recommended Posts

hi,

so my code has a loop that goes through all inventory items. I verified that item is not nil already however I got a rare crash where this code :

if (item["Light"] or recipes[item.prefab].tab == RECIPETABS.LIGHT then

returned an error because can't index nil. so I'm wondering if I should check that item.prefab is not nil globally or locally

e.g. can a valid item have no prefab ???

I'm wondering cause so far I can't reproduce the crash.

Thanks.

well I don't think it's possible since there's a : "if item then" before that

and ok that means I should be safe to verify that all items in inventory have a prefab globally before it process any of them.

if item and item.prefab then

thanks.

Edited by whismerhill

well I agree with you darkxero

however why would that be a problem at all ?

you mean it can't do the operation : " nil == somevalue " and so  return false ?

so before doing that I should add something like

if (item["Light"] or function(item,recipes) if recipes[item.prefab] then return (recipes[item.prefab].tab == RECIPETABS.LIGHT) else return false end end) then

ho while writing it I realize that maybe what it doesn't like is the ".tab" over a nil value ?

sorry I'm a bit of a newb :p

Edited by whismerhill
2 hours ago, whismerhill said:

however why would that be a problem at all ?

Just do something like you do for whether the item exists at all:

if (item["Light"] or (recipes[item.prefab] and recipes[item.prefab].tab == RECIPETABS.LIGHT) then

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
  • Create New...