Is it possible to dynamically required mods?

So, I was working with a bit of simple code. First I created a few mods; globalVariables, globalFunctions. Then I designed the game to start on a home.lua pointed at from a menu.lua. From there the user will be able to navigate to different scenes; zone1, zone2, etc. I also made a few veritable Lua files called game1.lua, game2.lua, game3.lua, ect. as places for the user to save there game in a type of “game slot”. The idea here is to use the require function to auto load information for each saved game, such as armor, weapons, and as in the case below, player scene location. (as the player will want to always start at the scene they last left.)

So, here is where my question comes into play. In the following code, I have attempted to require a dynamic game#.lua within my globalFunctions.lua file by implementing the following code. When I run it; however, I get an error message which says, “bad argument #1 to ‘find’ (string expected, got nil)” My thinking is the variable in the require function should point to a string value as is. Unless a variable is not an accepted argument for a require function. Which, might be what this error message is trying to say to me… If so, is there another way this can be done?

NOTE: The variable currentGameID, which is in the globalVariables Module, is a number value that will change based on what character the player chooses to play during their session. Currently, this code works for all scene changes except the ‘default’ one.

UPDATE: I changed the code below so that the defaultScene function code reads: gV.currentScene = “home”. Now, it is identical to the code in the following functions which work. Oddly I am still getting the same error message! :sweat_smile: I am now at a complete loss for understanding why this is bugged.

local composer = require("composer")
local gV = require( "globalVariables" )
local ui = require( "userInterface")
local currentGame = "game"..tostring(gV.currentGameID)
local game = require( currentGame )
local M = {}

--Scene Navigation
M.defaultScene = function()
    gV.currentScene = game.playerLocation
end
M.menuScene = function()
    gV.currentScene = "menu"
end
M.settingsScene = function()
    gV.currentScene = "settings"
end
M.creditsScene = function()
    gV.currentScene = "credits"
end
M.switchScene = function()
	composer.gotoScene( gV.currentScene, { time=800, effect="crossFade" } )
end
	
return M

Hey!

This is perfectly doable. The most likely culprit is a typo, a missing file path or a nil/unwanted value in a variable.

With these sort of issues, it’d be infinitely easier to help out if you shared a small part of your project that demonstrates this issue.

Otherwise, you can use print() to help you figure things out for yourself. For instance, before requiring currentGame or before you move on to the next scene, just print out the value of currentGame or gv.currentScene and see if they are what they should be.

I only have Notepad ++

I have tried using print, but I don’t yet have a way or understanding of how to get access to live coding. I have been using the Solar2D Simulator, which doesn’t seem to produce anything when I pass print to it. I could be putting the print code in the wrong area, as I have been using composer.

As for locating the area where the error is happening, I am at a loss, I have passed my eyes over the code several times, can’t seem to find it. the faulty code seems to be identical to the code that works. I am able to make the offending code shutoff. It is in the menu.lua file or something it points to. Here is the line of code I removed (with the arrow). It doesn’t seem to have anything wrong with it.

NOTE: I changed “defaultScene” to “playerScene” since this chat thread hoping the problem was related to global naming. It wasn’t of course.

local composer = require( "composer" )
local gF = require("globalFunctions")
local gV = require("globalVariables")

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
local music

-- -----------------------------------------------------------------------------------
-- 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
	
    local background = display.newImageRect( sceneGroup, gV.bg.default, 640, 1136 )
    background.x = display.contentCenterX
    background.y = display.contentCenterY
	
    local title = display.newImageRect( sceneGroup, gV.title, 384, 128)
    title.x = display.contentCenterX
    title.y = 100

	--Menu Buttons
    ----Play Button
	local playButton = display.newText( sceneGroup, "PLAY", display.contentCenterX, 220, native.systemFont, 60 )
    playButton:setFillColor( 0.82, 0.86, 1 )
-->    playButton:addEventListener( "tap", gF.playerScene )
	playButton:addEventListener( "tap", gF.switchScene )

	
	----Credits Button
	local creditsButton = display.newText( sceneGroup, "CREDITS", display.contentCenterX + 170, 275, native.systemFont, 30)
    creditsButton:setFillColor( 0.82, 0.86, 1 )
	creditsButton:addEventListener( "tap", gF.creditsScene )
	creditsButton:addEventListener( "tap", gF.switchScene )
	
	----Settings Button
	local settingsButton = display.newText( sceneGroup, "SETTINGS", 80, 275, native.systemFont, 30)
    settingsButton:setFillColor( 0.82, 0.86, 1 )
	settingsButton:addEventListener( "tap", gF.settingsScene )
	settingsButton:addEventListener( "tap", gF.switchScene )
	
	-- Load Audio
	music = audio.loadStream( gV.music.menu )
end

-- show()
function scene:show( event )

	local sceneGroup = self.view
	local phase = event.phase

	if ( phase == "will" ) then
		-- Code here runs when the scene is still off screen (but is about to come on screen)

	elseif ( phase == "did" ) then
		-- Code here runs when the scene is entirely on screen
		
	    -- Play music
        audio.play( music, { channel=1, loops=-1 } )
	end
end


-- hide()
function scene:hide( event )

	local sceneGroup = self.view
	local phase = event.phase

	if ( phase == "will" ) then
		-- Code here runs when the scene is on screen (but is about to go off screen)

	elseif ( phase == "did" ) then
		-- Code here runs immediately after the scene goes entirely off screen
		
        audio.stop( 1 )
	end
end


-- destroy()
function scene:destroy( event )

	local sceneGroup = self.view
	-- Code here runs prior to the removal of scene's view
	
	-- Cleanup
	audio.dispose( music )
end


-- -----------------------------------------------------------------------------------
-- Scene event function listeners
-- -----------------------------------------------------------------------------------
scene:addEventListener( "create", scene )
scene:addEventListener( "show", scene )
scene:addEventListener( "hide", scene )
scene:addEventListener( "destroy", scene )
-- -----------------------------------------------------------------------------------

return scene

Oh, this has nothing to do with your choice of coding tools, live coding, or such things, though I do recommend switching from Notepad++ to Visual Studio Code.

By sharing parts of your project, I simply meant taking the parts of your project that are crashing, putting them into a zip file and sharing that here on the forums. That way it’s infinitely easier for others to what’s going on.

Out of curiosity, what platform are you running Solar2D on? Windows, Mac or Linux? When you launch Solar2D Simulator, you should also see the Solar2D Simulator Console, which will contain all print() outputs, error messages, etc.

When your Solar2D Simulator crashes, in most cases the error message will tell you the literal line and the literal issue that is happening. For instance, you might be seeing something like: main.lua:48: attempt to index global 'myVariable' (a nil value). You can then use this information to back trace the error.

yes. this is what I have been trying to do. I have went back to the lines of code in the error message and followed the code around. It all looks fine. I have been checking capitalization, punctuation, and my print functions aren’t showing in the corona console. Is there a setting that turns them on? I will see about making a zip file.

I tried uploading a zip file but it said there was an error uploading it. ha ha, can I get a break here? LOL

hm, does the discord server allow screen sharing?

I aquired VS Code…I like… :sunglasses: thanks for the tip.

If you share the error messages, it’ll also make it easier for others to help you out. :stuck_out_tongue:

It’s exceedingly difficult trying to figure out what might be the problem without access to the whole picture or even the error messages.

Now, if you have Solar2D Simulator Console visible and its working, but you aren’t seeing any of your prints there, then it sounds to me like your prints are either in the wrong locations (in code) or you aren’t running the correct files. The print function “just works”, there’s no enabling or disabling it unless you’ve rewritten the function itself.

I’m not sure what you are trying to achieve - other than over complicating things :slight_smile:

If you want multiple levels, a lua file for each level is really not the best (or scalable) way to achieve this.

You should “define” your levels in JSON or XML. and then load the level into play.lua.

Also, if you are requiring globalVariables in every scene you could just declare it once in main.lua and every scene will have access.

What SGS said. Definitely consider using fileIO with JSON for levels, configs etc that you don’t know how many there will be of and which are anyway liable to change as you build up your project.

Reading a JSON into a lua table is very straightforward. Will be happy to offer any guidance/code if you feel this might help you.

SGS, I thought that was possible and was afraid to leave off the require codes. LOL I removed them now. Glad I don’t have to do all that extra work. as for using Json and XML. I have never used those before. I am currently trying to learn how to do these things in Lua. But, I would be open to such methods as long as I can take things slow so I don’t get confused. Currently still trying to learn Lua.

I would be interested to learn whatever would make my project function at it’s best performance.

It is probably obvious at this point, but I only recently decided to get serious about programming. I have toiled with programming in the past. Currently, I am learning Lua so I can finally build the games I have in my head. I am learning on my own though because I am currently a low income individual. I found a few great free sites for learning Lua which I am reading. There is a lot to learn, so I should also be careful not to overwhelm myself.

This program I built is only a test project so I can practice what I am learning in my Lua studies. I am not ready to build a real application at this time. If this thread is too confusing, I can just delete this project and start a new one after I have read more lessons.

It’s good that you’re learning Lua and Solar2D and being patient with it. All the best for the journey.

I can recommend some resources that might be of interest for saving user data, file i/o etc:

After you’ve taken a look at the general read-write guidelines that famousdoggstudios shared, you might want to take a look at the JSON module that former community manager Rob Miracle wrote:

My games have a lot of loading game levels and saving player progress, and that tool takes a lot of the grunt work out of it.

More generally, I taught myself programming by learning Solar2D (Corona SDK at the time), and I found this tutorial to be good:

These are great! Thanks for the information. I am defiantly going to immerse myself with it.

Wow! Thank you for this. I am looking forward to delving into this as well. I appreciate all the information, programmers!

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