質問

Code is very simple. Declaring two tables, a simple and a multi-dimensional:

Player = {X_Pos = 1, Y_Pos = 1, Current_Sprite_Num = 100}


    for j=1, Max_col_length do -- value ofMax_col_length doesn't matter here; positive integer anyway

        MapLayer_B[j] = {}

        for i=1, Max_row_length do --same here
            MapLayer_B[j][i] = 1
        end
    end

Then I try to do this operation:

MapLayer_B[Player[X_Pos]][Player[Y_Pos]] = Player[Current_Sprite_Num]

It should replace the Player[X_Pos]th element of the Player[Y_Pos]th rowtable in MapLayer_B. Instead, I got this error with LÖVE compiler:

  • Error: attempt to index field '?' (a nil value)

I don't really get, why it happens, since all elements of both MapLayer_B andPlayer tables are declared and not kept in nil.

Any ideas?

役に立ちましたか?

解決

You need to use Player.X_Pos instead of Player[X_Pos] and so on.

The brackets notation will interpret "X_Pos" as a variable and try to access taht key instead (the reason for the error is that undefined variables default to null)

t = {a = 17}

print( t.a ) --dot notation is simpler

print( t["a"] ) --bracket notation expects a string

key = "a" --that string can be from a variable
print( t[key] )
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top