table without object ?

hello, for a game i need a count but the table doesnt accept a value without a object (display.new…)

is there a way to make a table without specialy an object ?

local level = {} level[1] = 1 level[1].time = 60 level[2] = 2 level[2].time = 30 local function printime(b)         if level[b].time \> 0 then level[b].time =level[b].time-1 print(level[b].time) return level[b].time end if level[b].time == 0 then     print("gameover") end end     timer.performWithDelay( 1000, printime(1),-1)

>>>>  attempt to index field ‘?’ (a number value)

this works but i dont want this :

local level = {} level[1] = display.newRect(100,100,100,100) level[1].time = 60 level[2] = display.newRect(100,100,100,100) level[2].time = 30

You’re already doing it, you just don’t realize it.

This creates a table:

local level = {}

So to make an element of the level table another table, do this:

level[1] = {}

Simple enough :slight_smile:

  • Caleb

hi Calep P, thanks for your reply but i don’t understand…

In a previous game i do this and it works perfectly :

it’s the same than

level[1]…

level[2]…

local Character = {} x = 1 local timex = 500 local myIdx = 0 for a= 1,60 do timex = timex +200 myIdx = myIdx + 1 Character[a] = display.newSprite( myGroupCharacter, mySheet, sequenceData ) Character[a].x = mathr(300,500) Character[a].y = mathr(300,500) Character[a].alpha = 1 Character[a].xScale=mathr(3,5)\*0.1 Character[a].yScale = Character[a].xScale Character[a]:setSequence( "anim" ) Character[a]:play() Character[a].myId = myIdx Character[a].flag = 1

and when i type your solution :

local level = {} level[1] = {} level[1].time = 60 level[2] = {} level[2].time = 30 function printime(b)     if level[b].time \> 0 then level[b].time =level[b].time-1 print(level[b].time) return level[b].time end if level[b].time == 0 then print("gameover")  end end     timer.performWithDelay( 1000, printime(2), -1)

i have no count the result is only 29. and not 29…28…27…26…25…24…

I’ve not looked at everything, but you’re not using performWithDelay() correctly.  You can’t call a function as you are coding it.  Pass a closure instead:

Do this,

timer.performWithDelay( 1000, function() printime(2) end , -1)

NOT this:

timer.performWithDelay( 1000, printime(2), -1)

Hi Roaminggamer,

I did not know it :wink: Thanks

You’re already doing it, you just don’t realize it.

This creates a table:

local level = {}

So to make an element of the level table another table, do this:

level[1] = {}

Simple enough :slight_smile:

  • Caleb

hi Calep P, thanks for your reply but i don’t understand…

In a previous game i do this and it works perfectly :

it’s the same than

level[1]…

level[2]…

local Character = {} x = 1 local timex = 500 local myIdx = 0 for a= 1,60 do timex = timex +200 myIdx = myIdx + 1 Character[a] = display.newSprite( myGroupCharacter, mySheet, sequenceData ) Character[a].x = mathr(300,500) Character[a].y = mathr(300,500) Character[a].alpha = 1 Character[a].xScale=mathr(3,5)\*0.1 Character[a].yScale = Character[a].xScale Character[a]:setSequence( "anim" ) Character[a]:play() Character[a].myId = myIdx Character[a].flag = 1

and when i type your solution :

local level = {} level[1] = {} level[1].time = 60 level[2] = {} level[2].time = 30 function printime(b)     if level[b].time \> 0 then level[b].time =level[b].time-1 print(level[b].time) return level[b].time end if level[b].time == 0 then print("gameover")  end end     timer.performWithDelay( 1000, printime(2), -1)

i have no count the result is only 29. and not 29…28…27…26…25…24…

I’ve not looked at everything, but you’re not using performWithDelay() correctly.  You can’t call a function as you are coding it.  Pass a closure instead:

Do this,

timer.performWithDelay( 1000, function() printime(2) end , -1)

NOT this:

timer.performWithDelay( 1000, printime(2), -1)

Hi Roaminggamer,

I did not know it :wink: Thanks