Jump to content

Can I load a prefab without creating an entity? (Lua)


Genebris
 Share

Recommended Posts

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?

Link to comment
Share on other sites

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.

  • Upvote 1
Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

  • Upvote 1
Link to comment
Share on other sites

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.

  • Upvote 1
Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...