Help with tables inside tables

Hello there, 

i’m using data.lua to store my variables that i have to access from multiple modules.

Now i’m trying to store a table with game stats in there so i can easily save and load them.

So my data.lua looks something like this:

[lua]local data = {}

data.gameStats = {
    level = 1,
    maxHP = 1,
    ShootSpeedUpgrade = 1,
    BulletDamage = 1
}

return data[/lua]

But now i can’t access those variables with data.gameStats.level for example,

Does this even work?

Help would be appreciated  :slight_smile:

Best regards

Alex

Hi,

Yes, it does/can work, but it’s missing a little context. Are you calling this value from elsewhere via a function? Where does the ‘return’ go?

Cheers.

As a test, something like this should work.

[lua]

local data =

{

  gameStats =

  {

    level = 1,

    maxHp = 1,

    ShootSpeedUpgrade = 1,

    BulletDamage = 1

  }

}

print( data.gameStats.level ) --> 1

[/lua]

Something to note is that the ‘data’ will be local to this Lua chunk. You will need to use a module if you want this to persist in your app.

Cheers.

Thanks for the quick answer! :slight_smile:

But i seem to be missing something here, so what i’m basically trying to do is define this table in the data.lua wich now looks something like this:

[lua]

local data = 

{

gameStats = 

{

level = 1,

maxHP = 1,

ShootSpeedUpgrade = 1,

BulletDamage = 1

}

}

return data

[/lua]

Now i’m trying to access this data from my collision detection module like this:

[lua]

local data = require( “data” )

function onCollision( event )

if(event.object1.myName==“enemy” and event.object2.myName==“laser”) then

event.object1.hp = event.object1.hp - data.gameStats.BulletDamage

event.object2:removeSelf( )

event.object2.myName=nil

end

end

[/lua]

But it’s always returning a nil value.

Best regards

Alex

The module looks fine.  What exactly is returning nil?  If you print data.gameStats.BulletDamage separately what do you get?  

Hey there,

i just got it to work, seems like there is something i still don’t understand about handling tables ^^

I changed the code in my collision module like this:

[lua]

local data = require( “data” )

local gameStats = data.gameStats

function onCollision( event )

if(event.object1.myName==“enemy” and event.object2.myName==“laser”) then

event.object1.hp = event.object1.hp - gameStats.BulletDamage

event.object2:removeSelf( )

event.object2.myName=nil

end

end

[/lua]

and now everything runs as it should :slight_smile:

Thank you guys for your help!

Best regards

Alex

Hi,

Yes, it does/can work, but it’s missing a little context. Are you calling this value from elsewhere via a function? Where does the ‘return’ go?

Cheers.

As a test, something like this should work.

[lua]

local data =

{

  gameStats =

  {

    level = 1,

    maxHp = 1,

    ShootSpeedUpgrade = 1,

    BulletDamage = 1

  }

}

print( data.gameStats.level ) --> 1

[/lua]

Something to note is that the ‘data’ will be local to this Lua chunk. You will need to use a module if you want this to persist in your app.

Cheers.

Thanks for the quick answer! :slight_smile:

But i seem to be missing something here, so what i’m basically trying to do is define this table in the data.lua wich now looks something like this:

[lua]

local data = 

{

gameStats = 

{

level = 1,

maxHP = 1,

ShootSpeedUpgrade = 1,

BulletDamage = 1

}

}

return data

[/lua]

Now i’m trying to access this data from my collision detection module like this:

[lua]

local data = require( “data” )

function onCollision( event )

if(event.object1.myName==“enemy” and event.object2.myName==“laser”) then

event.object1.hp = event.object1.hp - data.gameStats.BulletDamage

event.object2:removeSelf( )

event.object2.myName=nil

end

end

[/lua]

But it’s always returning a nil value.

Best regards

Alex

The module looks fine.  What exactly is returning nil?  If you print data.gameStats.BulletDamage separately what do you get?  

Hey there,

i just got it to work, seems like there is something i still don’t understand about handling tables ^^

I changed the code in my collision module like this:

[lua]

local data = require( “data” )

local gameStats = data.gameStats

function onCollision( event )

if(event.object1.myName==“enemy” and event.object2.myName==“laser”) then

event.object1.hp = event.object1.hp - gameStats.BulletDamage

event.object2:removeSelf( )

event.object2.myName=nil

end

end

[/lua]

and now everything runs as it should :slight_smile:

Thank you guys for your help!

Best regards

Alex