0

I've been trying my hand at lua and tried to draw a rect to the screen

-----------------------------------------------------------------------------------------
--
-- main.lua
--
-----------------------------------------------------------------------------------------
-- Variable definition:
    local rect_shape
    
-- Initialization
    rect_shape.w = 20
    rect_shape.h = 40
    rect_shape.x = (display.contentWidth/2)-(rect_shape.w/2)
    rect_shape.y = (display.contentHeight/2)-(rect_shape.h/2)
    rect_shape = display.newRect(rect_shape.x, rect_shape.y, rect_shape.w, rect_shape.h)
    rect_shape:setFillColor(49,49,49)

    print("value of rect_shape:", rect_shape, "value of w:", rect_shape.w, "value of h:", rect_shape.h, "value of x:", rect_shape.x, "value of y:", rect_shape.y)

when I attempt to run this script, however,

'main.lua:10: attempt to index local 'rect_enemy' (a nil value)'

is returned. So somehow it seems I need to define the components of the rect before defining the rect itself because you can't access an empty rect?

Might anyone be able to explain and help me fix this?

  • Try to begin with: ```local rect_shape = {} -- Empty table``` – koyaanisqatsi May 19 '23 at 14:07
  • Thanks, that solved the error! However, rect_enemy.h and rect_enemy.w are both printed as being nil, when I'd expect them to be 40 and 20 respectively. What might be causing that? – Kaj van Veen May 19 '23 at 14:12
  • The Error say: ```rect_enemy``` is nil. That means: Undefined - Define: ```local rect_enemy = {['h'] = 40, ['w'] = 20}``` before using it. – koyaanisqatsi May 19 '23 at 14:35

1 Answers1

0

You redefine rect_shape completely with...

rect_shape = display.newRect(rect_shape.x, rect_shape.y, rect_shape.w, rect_shape.h)

My feeling says it should be...

local rect_enemy = display.newRect(rect_shape.x, rect_shape.y, rect_shape.w, rect_shape.h)
rect_enemy:setFillColor(49,49,49)

Full Example

-----------------------------------------------------------------------------------------
--
-- main.lua
--
-----------------------------------------------------------------------------------------
-- Variable definition:
    local rect_shape = {}
    
-- Initialization
    rect_shape.w = 20
    rect_shape.h = 40
    rect_shape.x = (display.contentWidth/2)-(rect_shape.w/2)
    rect_shape.y = (display.contentHeight/2)-(rect_shape.h/2)
    rect_enemy = display.newRect(rect_shape.x, rect_shape.y, rect_shape.w, rect_shape.h)
    rect_enemy:setFillColor(49,49,49)

    print("value of rect_shape:", rect_shape, "value of w:", rect_shape.w, "value of h:", rect_shape.h, "value of x:", rect_shape.x, "value of y:", rect_shape.y)

Here rect_enemy will be global.
You have to try if it works too if you define it localonly.

koyaanisqatsi
  • 2,585
  • 2
  • 8
  • 15
  • after changing my code to `----------------------------------------------------------------------------------------- -- -- main.lua -- ----------------------------------------------------------------------------------------- local rect_enemy = display.newRect(rect_shape.x, rect_shape.y, rect_shape.w, rect_shape.h) rect_enemy:setFillColor(49,49,49) print("value of rect_enemy:", rect_enemy, "value of w:", rect_enemy.w, "value of h:", rect_enemy.h, "value of x:", rect_enemy.x, "value of y:", rect_enemy.y)` I get the same result (sorry for poor formatting) – Kaj van Veen May 19 '23 at 19:40
  • No - Thats not what i mean - If you use ```rect_shape``` in ```rect_enemy``` than you have of course define ```rect_shape``` first. - Extending my Answer to full Example. – koyaanisqatsi May 20 '23 at 10:43
  • oh, it seems I've been using both rect_enemy and rect_shape when I meant to change them all to rect_enemy – Kaj van Veen May 22 '23 at 21:45