Calling require module multiple times, help!

Hey guys,
so previously in my game i have put all my game logic code into every separate level in my game, obviously this is not the best method, seeing as if i change anything in my logic i have to change it in every level file. So now im trying to modularize my code, i have a single “logic” file and then each level file is its own separate file.

What i currently am doing is loading my level using director class, and at the top of my level i say
[lua]local logic = require(“logic”)[/lua]

so in turn this loads my level up and runs my logic code and it seems to work great, the issue comes when i want to try and reload the same level if i want to retry the level.

Do i need to unload the “logic” that i required, before i can again require it? i guess i just am not sure what needs to be done here. Any help or ideas would be great, thanks!

here is my error that i get
…arc/Desktop/TIRE NEW WORKINGS/iPad/Source/level6.lua:1192: attempt to index field ‘?’ (a nil value)
stack traceback:
[C]: ?
…arc/Desktop/TIRE NEW WORKINGS/iPad/Source/level6.lua:1192: in function ‘buildLevel’
…arc/Desktop/TIRE NEW WORKINGS/iPad/Source/level6.lua:1204: in main chunk
[C]: in function ‘require’
…c/Desktop/TIRE NEW WORKINGS/iPad/Source/director.lua:310: in function ‘loadScene’
…c/Desktop/TIRE NEW WORKINGS/iPad/Source/director.lua:639: in function ‘changeScene’
…rc/Desktop/TIRE NEW WORKINGS/iPad/Source/loading.lua:229: in function ‘callback’
…/TIRE NEW WORKINGS/iPad/Source/transitionManager.lua:35: in function <… new workings>
(tail call): ?
?: in function <?:1138>
?: in function <?:215>
Runtime error
…c/Desktop/TIRE NEW WORKINGS/iPad/Source/director.lua:345: bad argument #-2 to ‘insert’ (Proxy expected, got nil)
stack traceback:
[C]: ?
[C]: in function ‘insert’
…c/Desktop/TIRE NEW WORKINGS/iPad/Source/director.lua:345: in function ‘_listener’
?: in function <?:501>
?: in function <?:215>
[import]uid: 19620 topic_id: 18000 reply_id: 318000[/import] </…>

By reload do you mean reloading the same file director has currently loaded from within that file? [import]uid: 84637 topic_id: 18000 reply_id: 68802[/import]

Yea so basically, when i choose to replay the level, all the objects that i created in my level file should get deleted, so then to recreate them i try to run my level file again to build the level, and at the top i have the
[lua]local logic = require(“logic”)[/lua]
to allow the two files to communicate between each other, it works the first time i boot up the game, but after retrying the level, its now called the level file twice(to recreate my level) but its calling that logic = require(“logic”) again

And thats were issues start, does that make sense? [import]uid: 19620 topic_id: 18000 reply_id: 68816[/import]

Why not leave off the local and just load it once?

Yea, I know globals are evil, but you can always localize it in your module.
main.lua:
logic = require(“logic”)
level1.lua
local logic = logic
or something like that.

(NOTE, still not 100% confident with my module knowledge!) [import]uid: 19626 topic_id: 18000 reply_id: 68825[/import]

Hmm yea this is all just starting to become a confusing mess, i guess im not sure what the best/correct way is to be able to have my logic in one file, and my levels in separate files and be able to rebuild/call the same level file as many times as i want without issues. [import]uid: 19620 topic_id: 18000 reply_id: 68847[/import]

Another thought would be to have your game logic “require” the appropriate level data and unrequire it when you’re done. I guess a lot depends on how much code is in each. But if your level file is mostly table data, I’d consider going this route.
[import]uid: 19626 topic_id: 18000 reply_id: 68922[/import]

Ok so that was kinda my original question, if i want to try and unload or unrequire a module, whats the syntax to do that? ive never seen how to code that, thanks [import]uid: 19620 topic_id: 18000 reply_id: 68936[/import]

I’ve seen it in a blog post or forum post somewhere…

But googling for it finds that most people write their own unrequire function which basically nils two table entries and then garbage collects.

[import]uid: 19626 topic_id: 18000 reply_id: 68937[/import]

Ok ill have to look into it, i just barely at least got my code running like i wanted, so now i have one logic file and it requires whatever level file i want to open, works great! But ill have to see if there are any memory leaks going on, anyways thanks for the tips and ideas, always appreciate your expertise =p [import]uid: 19620 topic_id: 18000 reply_id: 68943[/import]

***level6.lua:1192***

The problem is on line 1192 of level6.lua. Without knowing what that is, it’s difficult to say what’s going.

However, I doubt it has to do with requiring logic.lua twice.

The require statement checks to see if a module is already loaded and if it is it will NOT load it a second time as long as you use the same variable.

Only doing something like this will load it twice:

local logic = require("logic")  
  
local logicAgain = require("logic")  
  

[import]uid: 40137 topic_id: 18000 reply_id: 68966[/import]

Ok so if that is true, then there shouldn’t be any memory leaks because it wont load it twice if its already loaded? [import]uid: 19620 topic_id: 18000 reply_id: 68967[/import]

Right. You can read about it here:

[import]uid: 40137 topic_id: 18000 reply_id: 68970[/import]