functionlualove2d

using a function from an outside script in love2d gives me an error


I'm making an action RPG in Love2d, and I moved all of my player code into a separate Lua script for the sake of organization - but now upon attempting to use player.load, I get this error:

Error

main.lua:22: attempt to index global 'player' (a boolean value)


Traceback

main.lua:22: in function 'load'
[C]: in function 'xpcall'
[C]: in function 'xpcall'

this is my main.lua script:

--      RPG PROJECT IN LOVE2D

-- debug mode
debug = true    -- SET FALSE BEFORE SHIPPING

--      ROOM CHART:
-- 0 - Title
-- 1 - Overworld
-- 2 - Shop
-- 3 - Boss Room

Room = 0

--      PLAYER STATE CHART:
-- 0 - free
-- 1 - attacking
-- 3 - can't move

function love.load(dt)
    
    player = require 'Scripts/player'
    player.load()
    
    sti = require 'Libraries/sti'
    
    gameMap = sti('Maps/overworld.lua')
    
    menuImg = love.graphics.newImage('Assets/Menu.png')
    
    love.window.setMode(800, 600)
    
end

function love.draw(dt)
    
    if Room == 0 then
        love.graphics.draw(menuImg, 0, 0)
    end
   
   if Room == 1 then 
        gameMap:draw()
        player.draw()
    end
   
    if debug == true then
        love.graphics.print("Current FPS: "..tostring(love.timer.getFPS( )), 10, 10)
    end
    
end

function love.update(dt)
    
    if Room == 0 then
        if love.keyboard.isDown('space') then
            Room = 1
        end
    end
    
    if Room == 1 then
        player.Update(dt)
        if love.keyboard.isDown('escape') then
            Room = 0
        end
        
    end
    
    if love.keyboard.isDown('t') then
        debug = not debug
    end
    
end

and this is my player.lua script:

-- PLAYER SCRIPT
player = {x = 50, y = 50, speed = 3, state = 0, img = nil}

function player.load()
    
    playerRight = love.graphics.newImage('Assets/playerRight.png')
    playerLeft = love.graphics.newImage('Assets/playerLeft.png')
    playerUp = love.graphics.newImage('Assets/playerUp.png')
    playerDown = love.graphics.newImage('Assets/playerDown.png')
    
    player.img = playerDown
    
end

function GetInput()
    
    if player.state == 0 or player.state == 1 then
        if love.keyboard.isDown('w') then
            player.y = player.y - player.speed
            player.img = playerUp
        elseif love.keyboard.isDown('s') then
            player.y = player.y + player.speed
            player.img = playerDown
        end
        
        if love.keyboard.isDown('a') then
            player.x = player.x - player.speed
            player.img = playerLeft
        elseif love.keyboard.isDown('d') then
            player.x = player.x + player.speed
            player.img = playerRight
        end
    end
    
end

function player.update(dt)
    GetInput()
end

function player.draw()
    love.graphics.draw(player.img, player.x, player.y)
end

Any help would be much appreciated!

Here's my folders as well, just in case it's a path issue: my folders - it says 25 instead of Menu.png for no reason.


Solution

  • I have severall Hints for you

    Unclear
    It looks you write it under Linux but should it also run under Windows?
    If so, use OS independent folder delimiter ( the dot ) in require().
    Example

    player = require 'Scripts.player'
    

    In General: Dont use Slash / Backslash \ in require()
    Tip: LÖVE uses LuaJIT with modified Lua 5.1 check the Windows OS is easy
    (For deciding to use Slash or Backslash (not in/for require()))

    Clear
    As @Luke100000 mentioned...
    A Script for require has to return something.
    If not than only a true wil be cached and a next require returns only true.
    Therefore your player.lua content should be...

    -- player.lua
    local player = {x = 50, y = 50, speed = 3, state = 0, img = nil}
    -- player.load() will be local
    -- GetInput() wil be global
    -- And the last line...
    return(player)