[RESOLVED] require "blah" in 2 seperate lua? is it good?

Here is my situation:

main.lua – requires A.lua and B.lua
A.lua – requires B.lua

is the above bad? What happens exactly when i do the above? Does B get included once or twice?

Below is what i am trying to do. A and B are modules, however B module does nothing aside from letting main.lua and A.lua share variable information.

-- main.lua  
-- click on red square to deduct health  
  
local b = require "B"  
local a = require "A"  
  
b.health = 100  
  
local function HIT( event )  
 if event.phase == "ended" then  
 a.deductHealth()  
 print( s.health )  
 end  
end  
  
local function main()  
 local img = display.newImage( "redsquare.png" )  
 img.x = display.contentWidth / 2  
 img.y = display.contentHeight/ 2  
  
 img:addEventListener( "touch", HIT )  
end  
  
main()  
-- A.lua  
local b = require "B"  
  
local a = {}  
  
local function test()  
 b.health = b.health - 1  
end  
  
a.deductHealth = test  
  
return a  

[code]
– B.lua
local b = {}

return b
[/code] [import]uid: 74723 topic_id: 26220 reply_id: 326220[/import]

Probably all you need to do is put that information into the _G table.
(The so called super global)

This is visible through your whole project
so

_G.soundOn = true
_G.customerName = ""
you don’t need to include anything special. [import]uid: 108660 topic_id: 26220 reply_id: 106384[/import]

Long answer: If something is required more than once it’s still pointing to the initial required object. When something is required in Lua it’s put in a special table called package.loaded so that the next time it’s required again it’s read from that table and not from the lua file on disk. This speeds up the loading process obviously.

Short anwer: No problems whatsoever. [import]uid: 61899 topic_id: 26220 reply_id: 106402[/import]

@jeff472
I always try to avoid using any globals in lua as that is a bad practice and a challenge to me :smiley: And i am using the above idea to do something huge. But if it is for something small i think a global won’t hurt much [: Thanks though!

@CluelessIdeas
Thanks :smiley: [import]uid: 74723 topic_id: 26220 reply_id: 106466[/import]