Genebris Posted May 7, 2014 Share Posted May 7, 2014 I want to create an inventory. I imagine this as a table that contains names of items that player has in the inventory. When the player opens inventory menu, script loads prefab of the first item in the inventory, display it's icon and values such as damage or price, and the it does it for all other items in the table. But Prefab:Load also creates an entity. Of course for icon I can just take a name of the item and open a picture with this name, but how should I get item's stats? Quote Link to comment Share on other sites More sharing options...
Rick Posted May 7, 2014 Share Posted May 7, 2014 self.inventory = {} self.inventory["sword"] = {} self.inventory["sword"].dps = 15 self.inventory["sword"].cost = 150 self.inventory["sword"].icon = Texture::Load("") Each element in your inventory table can have a table as well that stores all your stats for that item. Ideally you'll have some kind of master inventory that loads 1 of everything possible in your game and however you give items to the player is where you copy what to give to the player inventory. That way you are just pointing to 1 loaded texture of an item so if you have 5 of those it's just pointing to the 1 texture/icon. You can have sub-tables of sub-tables etc. 1 Quote Link to comment Share on other sites More sharing options...
Genebris Posted May 7, 2014 Author Share Posted May 7, 2014 self.inventory = {} self.inventory["sword"] = {} self.inventory["sword"].dps = 15 self.inventory["sword"].cost = 150 self.inventory["sword"].icon = Texture::Load("") Each element in your inventory table can have a table as well that stores all your stats for that item. Is describing all items in the game in a script the only way to do that? Quote Link to comment Share on other sites More sharing options...
Rick Posted May 7, 2014 Share Posted May 7, 2014 Nope. It's a good way to test and get your lua table format solid. Then ideally you'd use a text file or sqlite or some kind of external file to describe the data, which when you look at it really isn't that different than a lua file really. If you have an external text file look at the IO commands LE has to open the files and reading/writing to them. 1 Quote Link to comment Share on other sites More sharing options...
Genebris Posted May 7, 2014 Author Share Posted May 7, 2014 I think using text files means that player will be able to easily change any item himself, so I wanted to use prefabs to prevent this, but thanks for help, anyway. Quote Link to comment Share on other sites More sharing options...
Rick Posted May 7, 2014 Share Posted May 7, 2014 inventory = { sword = { cost = 150, dps = 15 }, dagger = { cost = 200, dps = 10 } } print(inventory["sword"].dps) So you can make a lua file like the above to init your inventory. Notice how this isn't that different than if you made a text file to hold this information. The good thing with the above is that once you include it in your other lua files it gets evaluated and is lua and ready to use. Prefabs aren't there to prevent players from altering. Prefabs just link multiple things together. That's it. This may come as a surprise but if you are using Lua the player can see your code and alter it if they wanted. If this is a single player game then it really doesn't matter. If they want to play it that way then there isn't much you can do really and they'll only be ruining it for themselves. 1 Quote Link to comment Share on other sites More sharing options...
Genebris Posted May 7, 2014 Author Share Posted May 7, 2014 Ok, thank you. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.