Passing a parameter in a function

Hi there!

I’m still loving Corona after a month of development, but I’m running into problems. The language is very clear and simple to me in its building blocks, but constructing an elegant and optimised architecture for my platform game, with modules and scopes etc… proves to be quite the challenge for me.

One of the things I’m wondering is how much processing power goes into passing a local parameter along in a function (from another module), as an argument, in comparison to using globals. I’m asking because I keep running into (a LOT) of situations where a local scope doesn’t work for me.

For instance: every frame I need to compare the position of my camera object to the position of my enemy creatures (note to self: I should call this not every frame but every 10 frames or so), to see if the distance is so big that they are offscreen, in which case I stop some calculations for this enemy object. The x and y of these object are in different modules thus out of scope, so I need to make these global values, which is advised against. My other options seems to be to pass along the variables of the enemy x&y as arguments to a function in the camera module.

So… I’m wondering: which is more efficient? And even more: am I doing something very wrong in this way of programming?

Thanks!
t

p.s. I come from a PHP background where require or include are almost like just copy-pasting extra code into another document, and it has nothing to do with scope, and from a OOP background where you just define in every class to what level a function or variable is visible (to whole app, to nothing, to same class etc…). Lua scoping proves difficult for me to grasp. Although the basic explanation seems logical (global = visible everywhere, local = visible inside the code block) everything becomes unclear as soon as I start to use modules. [import]uid: 70134 topic_id: 14272 reply_id: 314272[/import]

Try encapsulating the logic/data for the camera object and enemy object(s) into a Lua function that acts as an OOP class. The function definitions can be in whichever lua files best suit you.

Then in your main game lua file, create instances of these classes:

-- Get references to your Lua modules  
local camera = require("camera")  
local enemy = require("enemies")  
  
-- Create a camera object  
local myCamera = camera.newCamera()  
  
-- Create an enemy object  
local myEnemy = enemies.newEnemy()  
  
-- Compare x and y  
  
if (myCamera.x == myEnemy.x) then  
 -- Do something  
end  
  

Does that help you? [import]uid: 26769 topic_id: 14272 reply_id: 52663[/import]

Aha! That looks like it’ll help a lot!

I actually have my code set up in that way, sort of:

function StartLevel(level)  
  
 -- build array holding physics and graphics tiles  
 maplib.TmxToTileArray(level) -- this fills global tileArray and mapWidth and mapHeight  
  
 -- add base background image  
 local Background = display.newImage("level-"..level.."-BG.jpg", 0,0)  
  
 -- add Parallax object  
 local Parallax = parallaxlib.AddParallaxToLevel(level)  
  
 -- add BGtiles object and add behaviour  
 BGtiles = bgtileslib.AddTileGroupToLevel(level)  
  
 -- add fpsPointer object in color given and add behaviour  
 local fpsPointer = fpspointerlib.AddFpsPointerToLevel(0, 255, 255)  
  
 -- add Hero object  
 Hero = herolib.AddHeroToLevel(150,150)  
  
 -- add Camera object  
 Camera = cameralib.AddCamToLevel()  
  
 -- add Render behaviour  
 renderlib.AddRendererToLevel()  
  
end  

But I didn’t know or forgot I could access variables of these by using dot syntax. I’ll try that tonight!

Thanks,
Thomas [import]uid: 70134 topic_id: 14272 reply_id: 52665[/import]

Hey Lord Mooch,

Your solution only works in this scenario, I’m afraid:

  • from within the scope of a block you add two local objects, and then you perform an operation on these two from within that same block. Everything is nicely in scope.

Unfortunately, what I need to do is the following:

  • from within a code block add two local objects, and add a GameLogic function to the frameloop (= listening to the enterframe event) from another module. The problem is that the gamelogic function can’t see the two objects, because they are out of scope.

My only solution so far has been making the two first object global, but this feels wrong, especially since I will have to do this every frame for every object in my game.

Any ideas?
thx,
thomas [import]uid: 70134 topic_id: 14272 reply_id: 52702[/import]

Without knowing the full architecture of your Lua files, it’s hard for me to say. Might need a Corona expert for this! [import]uid: 26769 topic_id: 14272 reply_id: 52703[/import]