Need Tutorial on Require()

Hello i was just wondering if someone can give me a quick rundown on the require() command because from what i know it can help use less code in a level based game.
I really don’t know how to begin using this in my game,

can anyone help?

Thanks,

-Boxie [import]uid: 113909 topic_id: 29066 reply_id: 329066[/import]

That’s called modularizatoin - take a look at this tute; http://www.coronalabs.com/blog/2011/07/06/using-external-modules-in-corona/

Peach :slight_smile: [import]uid: 52491 topic_id: 29066 reply_id: 117023[/import]

I looked at the tutorial,

It was very helpful in explaining everything i needed to know.

I am having a bit of trouble now, in my game i have a character moving function and all the other function relating to the movement and controls of the hero and i put it into a new lua file called HeroCode and i did all the module(…, package.seeall) at the top and in the game file i put the local HeroCode = require(“HeroCode”). I then did the made each function in the HeroCode file and made them local to the game file. I tried to play them using a runtime event listener but when they were played it gives me an error that says “a nil value” in places in the HeroCode file. Those values and such are stated in the game file but not the HeroCode file, like i have the function for movehero in the HeroCode file but not the hero’s sprites, and physics and all that stuff in that file.
Do i need to state the Hero’s sprite and physics in the HeroCode file, or am i just doing something wrong?
Thanks,

-Boxie [import]uid: 113909 topic_id: 29066 reply_id: 117034[/import]

If it’s referring to a nil value then it can’t “see” whatever you are referring to. Review some module samples and you should be able to spot the issue - too hard to say without combing through code. [import]uid: 52491 topic_id: 29066 reply_id: 117218[/import]

ok thanks, i’ll get right on it, doing one thing at a time and making sure everything works, and i’ll look at some example codes.

thanks
-boxie [import]uid: 113909 topic_id: 29066 reply_id: 117227[/import]

Can you show me how i would use external modules on something like this?
here’s part of the moveHero function in your zombie map example game you sent me a while ago and i was wondering how you would make the entire function in an external module and how to play this function on an enterFrame event listener.

this would really help me

[code]
local function moveHero (event)
if direction == “left” then
if mapGroup.x <= 235 and hero.x == 240 then
mapGroup.x = mapGroup.x - motionx
heroShadow.x = heroShadow.x + motionx
elseif mapGroup.x <= 236 and hero.x > 20 then
hero.x = hero.x + motionx
heroShadow.x = heroShadow.x + motionx
end
end

–then it continues, this is just the “left” direction part [import]uid: 113909 topic_id: 29066 reply_id: 117315[/import]

You need to be able to access each thing you are referring to in the module, so if you get a nil error on mapGroup you aren’t requiring the file it is in, or you are but you have “local” in front of it.

You need to go through each element and see that your module can see it. [import]uid: 52491 topic_id: 29066 reply_id: 117398[/import]

ok, thanks I will let you know if i run into any troubles.
thanks
[import]uid: 113909 topic_id: 29066 reply_id: 117429[/import]