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

1
local logic = require("logic")

By reload do you mean reloading the same file director has currently loaded from within that file?

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

1
local logic = require("logic")

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!)

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.

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.

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

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.

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

***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:

1
2
3
local logic = require("logic")
 
local logicAgain = require("logic")

Ok so if that is true, then there shouldn't be any memory leaks because it wont load it twice if its already loaded?

Right. You can read about it here:

  • http://www.lua.org/pil/8.1.html
  • views:1520 update:2011/11/25 8:45:21
    corona forums © 2003-2011