Jump to content

Using table.insert in order to build an inventory system?


Pancakes
 Share

Recommended Posts

Would that work out okay? I am looking at this simple lua info and I am just wondering whether this is the right path to go. Right now I use arrays for everything in my lua code including a finite state system implementation. Where I basically just say...

 

--State System
stateSys={}
stateSys[1] = {name = "Break", switch = "false"}
stateSys[2] = {name = "CamSwitching", switch = "true"}
stateSys[10] = {name = "In Battle", switch = "true"}
stateSys[11] = {name = "Battle Mode", switch = "active"}
stateSys[12] = {name = "Base Damage Cal", switch = "set to nil"}
stateSys[13] = {name = "Friend or Foe", switch = "friend"}
stateSys[21] = {name = "pa_commandprompt", switch = "disabled"}
stateSys[22] = {name = "Count?", switch = "count"}

 

It's the same for the weapons, characters, monster data...

 

Everything in my lua code is structured around manipulated arrays like this. It's not elegant but it works and it runs fast. I tried fancy loops but I just ended up slowing things down. Well anyway, everything is running and working fine except I don't have a dynamic inventory system that is capable of sorting itself and being sorted by the player in game.

 

Is the table.insert command the right way to go for building something like this into my array laden code? If so, does anyone have tips on how to use it effectively so that I can have a sortable inventory?

 

Thanks

Core I5 2.67 / 16GB RAM / GTX 670

Zbrush/ Blender / Photoshop CS6 / Renoise / Genetica / Leadwerks 3

Link to comment
Share on other sites

Is the table.insert command the right way to go for building something like this into my array laden code?

 

Yes. table.insert() works with arrays only. It increases array length by 1. It doesn't do anything with no-number indexes.

table.insert(array, object) 

is the same (if you have no nil values inside array) as

array[#array + 1] = object

 

For sorting table you can use table.sort() function.

Link to comment
Share on other sites

@Pancakes, those are really "tables" not arrays. Note that you can use anything for the "index" or "key". It can be nice sometimes to use strings as the key, but you can even use another table if you want.

 

The below link shows how you can sort tables in various different ways.

http://lua-users.org...LibraryTutorial

 

For an inventory I might look at using a string name of the item and the value could be the count (all depending on how you work your inventory though, stacking, splitting stacks, etc).

 

inventory["wood"] = 50 for example

 

When I'm working in Lua I almost always found it easier to use strings as the keys for many different things. Also, in Lua tables (or you calling them arrays) is basically how one works in Lua. Pretty much everything revolves around tables (array) so you are fine :)

Link to comment
Share on other sites

Would that work out okay? I am looking at this simple lua info and I am just wondering whether this is the right path to go. Right now I use arrays for everything in my lua code including a finite state system implementation. Where I basically just say...

 

--State System
stateSys={}
stateSys[1] = {name = "Break", switch = "false"}
stateSys[2] = {name = "CamSwitching", switch = "true"}
stateSys[10] = {name = "In Battle", switch = "true"}
stateSys[11] = {name = "Battle Mode", switch = "active"}
stateSys[12] = {name = "Base Damage Cal", switch = "set to nil"}
stateSys[13] = {name = "Friend or Foe", switch = "friend"}
stateSys[21] = {name = "pa_commandprompt", switch = "disabled"}
stateSys[22] = {name = "Count?", switch = "count"}

 

It's the same for the weapons, characters, monster data...

 

Everything in my lua code is structured around manipulated arrays like this. It's not elegant but it works and it runs fast. I tried fancy loops but I just ended up slowing things down. Well anyway, everything is running and working fine except I don't have a dynamic inventory system that is capable of sorting itself and being sorted by the player in game.

 

Is the table.insert command the right way to go for building something like this into my array laden code? If so, does anyone have tips on how to use it effectively so that I can have a sortable inventory?

 

Thanks

That seems a little complex, when you can just do stuff like this:

stateSys.break = false
stateSys.CamSwitching = true

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

You're right, but when I started making this script I didn't know that. And I also set it up so that any numbered switch can do any number of different things and then I can view dubug text that tells what it's doing. Why? Lack of experience. I was just making it up as I went along.

 

I was using my experience with CryEngine Flowgraphs and Blender Logic Bricks to come up with the design for it though. My entire script is just a bunch of text nodes like in a flowgraph, that you designate with set state commands just like in Blender GE, which is how I see it now.

Core I5 2.67 / 16GB RAM / GTX 670

Zbrush/ Blender / Photoshop CS6 / Renoise / Genetica / Leadwerks 3

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...