AndRay Posted November 14, 2020 Share Posted November 14, 2020 I've been debugging my cooking logic for a time now, and finally found a reason that my calls are returning trash. I need to get ingredients from a container, scan them for tags and add all of them. Problem is, inventory items have a different naming convention compared to prefabs, so I can't seem to figure out how to mix them. To give a concrete example, I spawn a potato via c_spawn("potato"). I put it into the container, then scan its name. As it came from an inventory, its name variable contains "Potato" with a upper-case P, which does not get registered as the same thing. Formatting can get even worse for Wet Goop ("wetgoop"), Cooked Carrot ("carrot_cooked") and Rot ("spoiled_food"). I could technically fix this by creating a huge alias table in my file, but this gets messy real quick and seems very forced, and also does not update or localize well. Is there another way to recover the prefab name from the inventory item name? Link to comment Share on other sites More sharing options...
penguin0616 Posted November 14, 2020 Share Posted November 14, 2020 (edited) The names you view come from STRINGS.NAMES, where they are structured as prefab = "display name". You can do a reverse lookup with Klei's table.reverselookup(t, lookup_value) to get the prefab name capitalized. Here's an example. local prefabUpper = table.reverselookup(GLOBAL.STRINGS.NAMES, "Rot") --> "SPOILED_FOOD" local prefab = prefabUpper:lower() --> "spoiled_food" -- ^ is standard prefab case convention Edited November 14, 2020 by penguin0616 2 Link to comment Share on other sites More sharing options...
AndRay Posted November 14, 2020 Author Share Posted November 14, 2020 Thanks a lot for the info! This will help a lot! Link to comment Share on other sites More sharing options...
AndRay Posted November 14, 2020 Author Share Posted November 14, 2020 Minor question: how do I use GLOBAL inside a component file? When calling the reverse lookup function I got "variable GLOBAL is not defined". server_log.txt Link to comment Share on other sites More sharing options...
penguin0616 Posted November 14, 2020 Share Posted November 14, 2020 Components default to getfenv(0), or Klei's Environment, so you do not need the "GLOBAL." part 1 Link to comment Share on other sites More sharing options...
AndRay Posted November 14, 2020 Author Share Posted November 14, 2020 Thanks a lot! This got me highly curious: how do environments work? This seems like a mighty useful tool to avoid lots of crashes I've been getting. Link to comment Share on other sites More sharing options...
penguin0616 Posted November 14, 2020 Share Posted November 14, 2020 (edited) Wouldn't really help you avoid crashes, nor does it have a use in most cases. http://lua-users.org/wiki/EnvironmentsTutorial#:~:text=Environments in Lua 5.1,the getfenv %2F setfenv standard functions. Edited November 14, 2020 by penguin0616 2 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now