composer coming back as a nil value?

What would cause Solar2D to do this?

composerIsNil

NOTE: I have tried to use require with and without ( ) with the same error.

Did you include?

local composer = require( "composer" )

Just declare it globally.

(Note: I do not use composer as I have my own scene manager)

yes, still does this

Have you set composer = nil somewhere in your code?

No, I have not.

It appears to me that require is trying to locate a Lua file that is missing. Where is the composer Lua file at so I can confirm it hasn’t been accidentally deleted or moved?

A composer module is always inside Solar2D,
Are you trying to create a new project and test it again?

Given that’s your main.lua and we can see lines 5 and 6 of your code, what’s happening on lines 1 through 4?

Whatever is going wrong happens there.

I just realised that the error occurred in menu.lua, not main.lua. The issue occurs somewhere before you get there.

As long as you don’t have any big files (in terms of file size), you should be able to zip the entire project and upload it here. Then someone will probably figure out the issue in a matter of minutes.

Saw your post on Discord and came to help. If you put together a sample project to show the issue that’d have been great but I’ll take a shot at it anyway.

Below, you can find two sample projects on how to scope composer in your project. I kept changed lines so you can see what’s different. If this works for you, great! If not, you can make changes to reflect what you’re trying to achieve on this project and re-upload.

sampleLocalGlobal.zip (2.1 KB)

Are you using Zerobrane?

OK, so what you suggested made everything work for a good while. So, thank you deeply for all your wisdom. Hopefully, I can ask for your help again.

Unfortunately, as I was working with modules your solution seems to have stopped working again somehow. All my requires are in main.lua and are globally set. But I am getting an error message saying one of my modules is unable to be indexed because of a nil value. Here is my flow;
Assuming these handles are assigned globally in the main lua file:
gV = require ( “globalVar”) , gM = require ( “globalMaps” )

Is then the following flow possible?:

M.var2 (in globalVar.lua) points to a gM.var1 which points to a lua obj/table
then a var3 (in game.lua) points to gV.var2

– Q1?: Can a mod2 point a var2 to a var1 in a mod1 if both mods are global?

–Q2?: Then can another lua file access a lua object/table if it points a var3 to var2 in mod2, which then points to var1 in mod1 which is responsible for having created said obj/table? (yes, this makes my brain hurt too…)

If this is not possible, then this is what might be producing the nil value I am getting.

I have attached a copy of the error if that is helpful at all. ( sM is the global var pointing to my module)
error052423

It’s really hard for me to help with your problem without getting my hands on a sample project. If you can put together a sample and upload the zip file here, that’d be great.

I’m seconding bgmadclown’s point about providing a sample project.


That being said, to answer your other questions:

Modules are just Lua tables. As long as variables have been declared and they are within scope, you can refer to them anywhere and everywhere.

Whether or not you can reference variable inside another module depends on their scope and the order of declarations. Lua is a single pass language, which means that in order to reference a variable, you must declare it (and likely assign a value to it) first.

If, for instance, you have modules A and B, and you first require module A and it references some variable from module B, then you’re likely going to get a crash because module B and its variables don’t exist yet. In these situations you can use functions within modules to assign/update values to variables when needed.

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