Search code examples
lualove2d

Error: src/StateMachine.lua:18: attempt to call method 'enter' (a nil value)


I am using this to establish some basic OO concepts in my lua project: https://github.com/vrld/hump/blob/master/class.lua

This is the class that I am trying to inherit

BaseState = Class{}

function BaseState:init() end
function BaseState:enter() end
function BaseState:exit() end
function BaseState:update(dt) end
function BaseState:render() end

into here:

TitleState = Class{__includes = BaseState}

local highlighted = 1

function TitleState:update()
    -- toggle highlighted option if we press an arrow key up or down
    if love.keyboard.wasPressed('up') or love.keyboard.wasPressed('down') then
        highlighted = highlighted == 1 and 2 or 1
        -- gSounds['paddle-hit']:play()
    end

    -- confirm whichever option we have selected to change screens
    if love.keyboard.wasPressed('enter') or love.keyboard.wasPressed('return') then
        -- gSounds['confirm']:play()

        if highlighted == 1 then
            gStateMachine:change('play')
        else
            love.event.quit()
        end
    end
end

function TitleState:render()

    local backgroundWidth = gTextures['background']:getWidth()
    local backgroundHeight = gTextures['background']:getHeight()

    love.graphics.draw(gTextures['background'], 0, 0, 0, VIRTUAL_WIDTH / (backgroundWidth - 1), VIRTUAL_HEIGHT / (backgroundHeight - 1))
    love.graphics.setFont(gFonts['large'])
    love.graphics.printf("Welcome to adventure", 0, VIRTUAL_HEIGHT / 3, VIRTUAL_WIDTH, 'center')

    -- instructions
    love.graphics.setFont(gFonts['medium'])

    -- if we're highlighting 1, render that option blue
    if highlighted == 1 then
        love.graphics.setColor(103/255, 255/255, 255/255, 255/255)
    end
        love.graphics.printf("START", 0, VIRTUAL_HEIGHT / 2 + 70,
            VIRTUAL_WIDTH, 'center')

    -- reset the color
    love.graphics.setColor(255/255, 255/255, 255/255, 255/255)

    -- render option 2 blue if we're highlighting that one
    if highlighted == 2 then
        love.graphics.setColor(103/255, 255/255, 255/255, 255/255)
    end
    love.graphics.printf("QUIT", 0, VIRTUAL_HEIGHT / 2 + 90,
        VIRTUAL_WIDTH, 'center')

    -- reset the color
    love.graphics.setColor(255/255, 255/255, 255/255, 255/255)
end

and am getting the above error when my StateMachine attempts to call the enter function. The simple fix for this error is that I define an empty enter function in the TitleState but that doesn't fix the issue and ignores the bigger issue that for some reason I'm not inheriting the parent methods and I'm not sure why.

The while project is here if you need context: https://github.com/kwchristopher/Untitled_Rpg

I am following the examples from CS50g to create my own 2d RPG and a better working example can be found here: https://github.com/games50/zelda


Solution

  • The following line
    TitleState = Class{__includes = BaseState}
    assumes that BaseState is already defined

    But in Dependencies.lua the BaseState class is defined after TitleState

    Move require 'src/states/BaseState' two lines up