Sign in to follow this  

Loading multiple LUA tables as data

This topic is 814 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey everyone, I've spent the past 4 hours hammering away at this problem to no avail (Either I'm really bad at understanding the lua stack, or it's something else entirely)

 

Suppose I have an lua file structured like this

ID1 = {

    name = "Description 1",

    mod = 20

},



ID2 = {

    name = "Description 2",

    mod = -15

}

And now suppose I need to get this loaded into a structure I have.

 

And my question is, how do I do it? How can I iterate though all these tables dynamically and load these values in. I've tried using lua_next and other methods, but all they do is spit out access violation reading exceptions.

 

Edit: Whoops, wrong subforum. Could I have this moved to API?

Edited by GutenTag

Share this post


Link to post
Share on other sites

Create a read function, then enclose your data  with the read which handles the data.

Example main.lua

my_data_container = {}

read = function( _chunk)
  -- get id
  local id = _chunk.id
  -- assign table to container
  my_data_container[id] = _chunk

.. do some processing..
end


do_file("my_data_file.lua")


You data should look like this

read({
    id = "ID1",
    name = "Description 1",
    mod = 20
})



read({
    id = "ID2",
    name = "Description 2",
    mod = -15
})

When calling a function with just a table you can leave the parentheses away:

read{
    id = "ID1",
    name = "Description 1",
    mod = 20
}



read{
    id = "ID2",
    name = "Description 2",
    mod = -15
}

Alternative for full-data table without the need of any processing just setup your data like this:

my_data_container = 
{
  ID1 =
  {
    name = "Description 1",
    mod = 20
  },
  ID2 = 
  {
    name = "Description 2",
    mod = -15
  },
}

Edited by Ashaman73

Share this post


Link to post
Share on other sites

This is actually much better than what I had, thank you!

 

Edit: Small question, If I follow that last suggestion, how do I access the tables contained within "my_data_container"

 

(Sorry, lua just seems very needlessly obscure about what I need to do to access items in the stack)

Edited by GutenTag

Share this post


Link to post
Share on other sites


Edit: Small question, If I follow that last suggestion, how do I access the tables contained within "my_data_container"

If you just declare a variable in a file using dofile, it is automatically global. Therefor you can handle it like any other global variable

local item = my_data_container.ID1
local item2 = my_data_container["ID1"]

Share this post


Link to post
Share on other sites

 


Edit: Small question, If I follow that last suggestion, how do I access the tables contained within "my_data_container"

If you just declare a variable in a file using dofile, it is automatically global. Therefor you can handle it like any other global variable

local item = my_data_container.ID1
local item2 = my_data_container["ID1"]

Thanks!

 

I also finally managed to get it to work after enough fiddling with the stack and more review of the reference manual.

Share this post


Link to post
Share on other sites

This topic is 814 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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

Sign in to follow this