Modules & Director Class

Good Evening (for me),

I’ve been building the basis for my game, i have:
-background & walls
-main character.
-onscreen dpad

I’ve been using the Director class to handle screen transitions.

I’ve been reading into Peach Pellen’s modular classes tutorials recently and i’ve decided it’s a Neccessity that i have my main character and dpad in their own .lua files.

I’ll probably need some help with this over the next few days, if your indulge me.

Q1: I think putting dpad and character into the same .lua would be best.
How should i begin?
Reading Peach’s tutorials, states director class use can steadily increase memory usage over time, and mine does.
(Only a single time per new screen by small increment.)

Should i start out by stripping director class from all my modules?
Or would it be okay to modulise my character and dpad code still with the director?

Sorry the post is so long, i’m trying to keep it concise. [import]uid: 91798 topic_id: 15868 reply_id: 315868[/import]

Hey there,

I love seeing my name in bold :wink:

For director, I suppose it depends on where your memory use is at after awhile playing - I use director a LOT and the memory issues have never been, well, an issue. It really depends on how big/intense your game is.

These links may prove very handy in the coming days for you;
http://blog.anscamobile.com/2011/07/using-external-modules-in-corona/
and
http://blog.anscamobile.com/2011/09/a-better-approach-to-external-modules/

Peach :slight_smile: [import]uid: 52491 topic_id: 15868 reply_id: 58699[/import]

Hello, it’s me again.(who else)

Now i don’t want to alarm anyone, but i think i may have done it
o.o
Hope so anyway.

*I’ll omit the common director stuff*
This is my external module, it contains the spawning of the main character and the dpad on screen and controls his movement.

module(..., package.seeall);  
  
function RobotPlayer()  
 local localGroup = display.newGroup(); -- usual director group  
...  
end  

And in the lvel i want to spawn him i used:

module(..., package.seeall);  
  
local Player = require "Player"  
  
function new()  
 local localGroup = display.newGroup();  
  
Player.RobotPlayer();  
...  
end  

Is this okay for being localized?
And a somewhat related note, should my texture memory reduce when i leave a screen?
Because even though i add all display objects to the director using ‘localGroup:insert()’ it remains the at 4.25 on any subsequent screens.
(Although it doesn’t increase anymore if the same images are used on different levels.)

Thanks a bunch

Matt. ¸.·´¯·.´¯·.¸¸.·´¯`·.¸ >

[import]uid: 91798 topic_id: 15868 reply_id: 58722[/import]

[lua]local functions = {}
local function RobotPlayer()
local localGroup = display.newGroup(); – usual director group

end
functions[1] = RobotPlayer
return functions[/lua]
Now this makes your functions in player.lua local. No global functions!
You can call RobotPlayer function as shown below
[lua]local Player = require “Player”
local localRobotPlayer = Player[1]

function new()
local localGroup = display.newGroup();
localRobotPlayer.RobotPlayer();

end[/lua]

Pretty Sure this was what Jon Beebe wanted to explain here
http://blog.anscamobile.com/2011/09/a-better-approach-to-external-modules/
Someone correct me if I’m wrong! :slight_smile:
[import]uid: 64174 topic_id: 15868 reply_id: 58735[/import]

Hi again,

I’m having a slight problem in my level.lua file referencing my character.x from my moduled player.lua.

Level.lua

-- Create master display group (for global "camera" scrolling effect)  
 local game = display.newGroup();  
 game.x = 0  
...  
Player.RobotPlayer(); -- controls the character 'RobotPlayer' from Player.lua  
...  
-- Camera follows RobotPlayer automatically  
local function moveCamera()  
 if (RobotPlayer.x \> 240 and RobotPlayer.x \< 800) then  
 game.x = -RobotPlayer.x + 240;   
 end  
end  
  
Runtime:addEventListener( "enterFrame", moveCamera )  
  

But i get an error: 'attempt to index global ‘RobotPlayer’ (A nil value)
This is from the 'if (RobotPlayer.x > 240 line.

I’ve tried exchanging it for Player.RobotPlayer.x & just Player.x.

How should i be referencing this .x from a different .lua file?

Thanks
Matt! [import]uid: 91798 topic_id: 15868 reply_id: 58891[/import]

@thehivetyrant, when I get an error like that, I check to see if RobotPlayer is forward referenced. If not (and if the definition of RobotPlayer appear later than line 240), it can cause the error. This may not be your problem, but it’s worth looking into.

Also, at your line #5, you have Player.RobotPlayer() – what does this do? Does it spawn a RobotPlayer? if so you may want to try changing it to:

[lua]local RobotPlayer = Player.RobotPlayer();[/lua]

Good luck. [import]uid: 67217 topic_id: 15868 reply_id: 58905[/import]

Hi,

Modulising is making every thing more difficult for me.

If i have my character code in every level file would it really be so bad?
As i can seem to code a camera and physics alot easier in the same file.

An honest answer please. ^-^

PS:(If it is a bad problem, maybe i could pay someone to modulise it later on, think that’s viable? or would they want to modulise it earlier on?) [import]uid: 91798 topic_id: 15868 reply_id: 58933[/import]

modularizing is just a way to make your life easier and your code less messy, its not necessary to use it [import]uid: 16142 topic_id: 15868 reply_id: 58938[/import]

Even though going modular isn’t necessary, I found that when you have a huge code set of 3000 + lines I really REALLY don’t want to copy and paste level 1 through 1000 logic every…single…time.
I’m still learning programming, and I think as long as you can dynamically work with data then it makes life easier.

Right now, I have my game, and all the logic I want is external logic (a work in progress, still working on some things Jon Beebe was going over).

Then when I go to make the level, I only have to worry about putting my display objects to create the level, and running a few functions that signal game over, game won, next level, and things like main menu etc.

Before this, I wasn’t using director or anything and I had ONE HUGE ASS main.lua file and it was a freaking mess! Now, it’s 50 to 100 lines on each level.lua file and all the crazy logic stuff is in my gamelogic.lua file
So yes, it’s not necessary but it would seem if you want to release your app to the app store and do updates, this would help.

Example - lets say you make an app, and you have 100 levels, you release the app and the code is in every file, hardcoded and each one is different for the level.

People are rating and telling you it sucks, giving 1 star, then you have to go through EVERY SINGLE FILE fixing the bug (can’t just do a replace here, since it’s different every time) and it takes you FOREVER…
vs - having one central area, where the logic is handled (or a few like logic.lua, options.lua, sound.lua etc) and if you had to change something you could just do it one place and fix it everywhere? I’ve already made MANY mistakes, and i’ve had to go fix a bunch of stuff I thought worked, then it didn’t and I wasted hours fixing things.

Now, I can do it in a few minutes :slight_smile:
My 6 cents on it (2 cents wasn’t enough obviously). Not necessary, but damn nice!

:slight_smile:

[import]uid: 61600 topic_id: 15868 reply_id: 58965[/import]

Thanks for your 6 cents, i’ll buy myself some sweeties! =)

Also thanks for your thoughts,

It seems i already do have some modulised code;

  • Menu
  • Level Select
  • W1-1, W1-15, W2-1 etc.
    Through Director class.

I was trying to put my Character in it’s own file.
That was causing issues.

Since that particular parts not neccessary, i’ve been continuing without it and it’s been going Great!

But i understand what you mean, if there’s an issue with my character later on, i’ll have to go through each level and change it.

I think i’ll risk that to be honest. As my mind explodes when i try to modulise him.
However i can reduce the risk of that happening in a number of ways, Since he should be the same on all levels, i SHOULD only have to work on a single level and then copy paste him into the others when done and tested.

Again thanks for your advice, good luck with your games everyone. [import]uid: 91798 topic_id: 15868 reply_id: 58968[/import]

Although it’s slightly unrelated. But unworthy of a new thread.

Is there a way to reference the touch.x and touch.y

As in get the x and y co-ordinates of where the user touches? [import]uid: 91798 topic_id: 15868 reply_id: 59204[/import]

Yes;

[lua]local function printTouch (event)
print (event.x, event.y)
end
background:addEventListener(“touch”, printTouch)[/lua]

Just as an example :slight_smile:

Peach [import]uid: 52491 topic_id: 15868 reply_id: 59271[/import]

Oh Peachy poo

I think you forgot something :slight_smile:
“if event.phase == “began” then”

Also you forgot the double “end” hehe. See I did learn something after all! (the day is coming when I will yell "I GOT A MAC I GOT A MAC WOO HOO!)

[code]

local physics = require(“physics”)
physics.start()
physics.setGravity(0, 0)
physics.setPositionIterations(32)

–shortcut variables
local _W = display.contentWidth
local _H = display.contentHeight

local background = display.newRect (0, 0, _W, _H)
background:setFillColor (0,255,255)

local function printTouch (event)
if event.phase == “began” then
print (event.x, event.y)
end
end

background:addEventListener(“touch”, printTouch)
[/code] [import]uid: 61600 topic_id: 15868 reply_id: 59327[/import]

Evening G$,

I did not forget, it was merely an example to be played with and used to understand how event.x and event.y worked - he didn’t specify whether he wanted it to print once on start, end or every time the touch moved :wink:

Peach [import]uid: 52491 topic_id: 15868 reply_id: 59451[/import]

Touche’
Well, I still think magic space monkeys approve either way.

:slight_smile: [import]uid: 61600 topic_id: 15868 reply_id: 59504[/import]

I thought I should mention that Director class as I know it and use it doesn’t cause memory leak. If I’m not mistaken, the issue is more to do with the usage of module(…, package.seeall) in each lua file.

After investigating this and following discussions relating to memory leak and how-to-properly-modularize, I came to a conclusion that if I open a module that starts with module(…, package.seeall), I need to un-require it.

I’m a too much of a newbie (in terms of programing/coding) to understand the fine workings behind it, but because I happen to use Spriteloq and it also comes with utility tool that has unrequire function, all I do is, when I changescene, I call unrequire.

So, when I am on level1.lua, and is ready to change scene to level2.lua, I include the following lines:

[lua]director:changeScene(“level2”, “flip”);
unrequire(“level1”);[/lua]

This pretty much made the memory management of my game sound and healthy, and I don’t feel I need to worry about it. (Please note, I am a customer of Spriteloq, and I’m a very happy customer at that, and I have no trouble recommending highly of the product, but I do not work for Spriteloq or have any relationship with them, other than the fact I bought it and have benefited from it.)

Director manages texture memory incredibly well. Whether I use unrequire or not, it made no difference in terms of texture memory management. In my project, director always kept the size of texture memory consistent (i.e., I have not seen any leakage associated with texture memory).

So… I think if you are concerned over memory leakage associated with modular structure of your project, you may want to find the answers to the memory leakage issue associated with module(…, package.seeall) function and find the way to minimize the problem (rather than worrying about Director class as being the cause of it – because, honestly, Director class isn’t the cause of it, and it is too awesome a class not to use, and honestly, because I use Director in every single scene, I don’t bother localizing Director, but I require it in as global module in main.lua and never even think about unrequiring it.)

I hope I haven’t confused anyone or caused any additional headache, but I thought I should mention what I thought of Director (being a staunch fan of this class).

P.S. If I’m mistaken, please let me know. I’m always interested in figuring out how I may make my project completely and utterly memory-leak-free.

[import]uid: 67217 topic_id: 15868 reply_id: 59548[/import]

A quick follow up on this topic – please see Ricardo’s comment here:

http://developer.anscamobile.com/forum/2011/09/28/director-15-what-do-you-want#comment-59563

It may pretty much clear up any memory leakage concerns that anyone using Director class may have. [import]uid: 67217 topic_id: 15868 reply_id: 59564[/import]