Possible Scope Issue?

Hello all. I was experimenting with the idea of creating multiple character lua files. Then require only the character file the player is using at game time. This would make it possible for my users to select from multiple saved characters without using up memory at game time. The code for it was working fine and then as I was tinkering it broke. Below is the runtime error I keep getting.
runtimeErrorOn031523
I am not too used to the debugging process yet, but it appears I have some sort of scope issue because of an unexpected nil value. I checked each page I am working with and they all appear to be written correctly to my own knowledge. I am not using any system variables by mistake that I could find. All variables are spelled and capped correctly, that I can tell.

Here is some code snippets. If you need to see more, please let me know:

globalVariables.lua

local M = {}
M.currentLevel = 1
M.currentPlayerType = 1


return M

player1.lua

local M = {}
M.mainSprites =
{ --Expand to edit Main Character Sprites
    frames =
    {
	    --Walk East
        {   -- 1) Walk-E1
            x = 0,
            y = 0,
            width = 36,
            height = 52
        },
--etc..
       {   -- 55) Crouch-3
            x = 72,
            y = 312,
            width = 36,
            height = 52
        }
    }
}

M.mainSheet = graphics.newImageSheet( "sprites/SS-charMain.png", M.mainSprites )

return M

game.lua

--Use Composer
local composer = require( "composer" )
--Use Psudo-global modulation
local gV = require( "globalVariables" )

--Set player type
local pT = require( "player1" )


local scene = composer.newScene()

-- -----------------------------------------------------------------------------------
-- Code outside of the scene event functions below will only be executed ONCE unless
-- the scene is removed entirely (not recycled) via "composer.removeScene()"
-- -----------------------------------------------------------------------------------

-- Initialize variables
	--Actors
	local player

-- -----------------------------------------------------------------------------------
-- Scene event functions
-- -----------------------------------------------------------------------------------

-- create()
function scene:create( event )
 
    local sceneGroup = self.view
    -- Code here runs when the scene is first created but has not yet appeared on screen
 
    -- Set up display groups
    backGroup = display.newGroup()  -- Display group for the background image
    sceneGroup:insert( backGroup )  -- Insert into the scene's view group
 
    mainGroup = display.newGroup()  -- Display group for the ship, asteroids, lasers, etc.
    sceneGroup:insert( mainGroup )  -- Insert into the scene's view group

    -- Load the player
    player = display.newImageRect( mainGroup, pT.MainSheet, 1, 36, 52 )
    player.x = display.contentCenterX  --HERE is where the problem is happening
    player.y = display.contentCenterY
    player.myName = "player"
	
    local function endGame()
    composer.gotoScene( "menu", { time=800, effect="crossFade" } )
	end

    -- Load Listeners
    player:addEventListener( "tap", endGame )
	
end


--etc.
return scene

Sounds like the actual image creation doesn’t happen on the line before that. If those files look exactly like that (aside from the omitted parts), it seems it should be pT.mainSheet in the .newImageRect call in game.lua (or M.MainSheet at the end of the player1.lua).

yes, that is how it is setup. It was working before. I am not sure why it is creating a nil value. I am going to try writing a separate test program and move along one step at a time. I was testing in the simulator the whole time, but I wonder if it wasn’t refreshing correctly? Can a connection issue cause that? I am sure I will find my error eventually. I will let you know what happens with the the step-by-step test app.

You have declared your image sheet as “mainSheet” in player1, but reference it as “MainSheet” in your game script. This will not work as Lua is case sensitive.

Best regards

Wow! I checked my variables several times. I can’t believe I missed that! Thank you for being an extra set of eyes, Method_Mobile! Everything is working again! :smiley: …of course.

You’re Welcome :slight_smile:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.