Calling Functions from external file

I know this might be a stupid question. I have been programming for years, and can’t figure this out in LUA. I have my main.lua file that calls functions from other files to load graphics only. Please Help…

main.lua:file

local stages = require( "stages" )  
local stage  
  
local function stageSetup(currStage, nextStage)  
  
 if stage == 1 then  
 stage = stages.stage1  
 end  
end  
  
stageSetup  
  
stages.lua:file  
local function stage1()  
 local bg\_png = display.newImage("bg.png", 0, 0)   
 local groundbtm\_pn = display.newImage("groundBtm.png", 0, 280)   
 local groundtop\_pn = display.newImage("groundTop.png", 0, 262)   
 local rock1\_png = display.newImage("rock1.png", 203, 230)   
 local rock5\_png = display.newImage("rock5.png", 221, 252)   
 local tree1\_png = display.newImage("tree1.png", 235, 226)  
end  

error:
Copyright © 2009-2010 A n s c a , I n c .
Version: 1.0.0
Build: 2010.109
The file sandbox for this project is located at the following folder:
(/Users/derwydd/Library/Application Support/Corona Simulator/game-F84EB65064B72C57CD4A94449A9F2023)
Runtime error
…/Desktop/LuckiiClover/GameName/game/main.lua:15: attempt to index local ‘stages1’ (a boolean value)
stack traceback:
[C]: ?
…/Desktop/LuckiiClover/GameName/game/main.lua:15: in main chunk
Runtime error: …/Desktop/LuckiiClover/GameName/game/main.lua:15: attempt to index local ‘stages1’ (a boolean value)
stack traceback:
[C]: ?
…/Desktop/LuckiiClover/GameName/game/main.lua:15: in main chunk
[import]uid: 7197 topic_id: 3575 reply_id: 303575[/import]

local main_menu = require(“main_menu”)
main_menu.create()
here is how I do it. Looks like the only thing different from my code is “()” after the function name.

have you tried changing line 7 to stage = stages.stage1()
another thing I just noticed that that you set up a variable for this function but the function does not return a value. I think that I would change my code to

stages.stage1()

hope this helps
[import]uid: 5687 topic_id: 3575 reply_id: 10787[/import]

This did not work! If I just require the file, then everything gets loaded, but if I place it in the function and try to call the function from main.lua, it just gives me this junk about being a boolean value? [import]uid: 7197 topic_id: 3575 reply_id: 10788[/import]

if you set stage = stages.stage1, you’ve returned a function into your variable, stage

calling stage() at that point in your main file would then essentially call the stages.stage1 function

this isn’t really how things are normally done though

[import]uid: 6645 topic_id: 3575 reply_id: 10790[/import]

I got it finally.

In the external lua file I was using local function stage1() body end
I changed it to function stage1() body end
I changed the local stage = stages.stage1 to “stage1()”

Now it works,…

Thanks guys

I guess I was doing the wrong combination everytime! [import]uid: 7197 topic_id: 3575 reply_id: 10793[/import]

FYI - I’ve just been playing about with this and (thanks to the thread here) have been able to figure it out.

though my method is a touch different

Program Code:
[lua]local f = require( “functions” )
crate:addEventListener( “touch”, startDrag);[/lua]

LUA FILE NAMED: functions.lua
[lua]-- A basic function for dragging physics objects
function startDrag( event )
local t = event.target
local phase = event.phase
if “began” == phase then
display.getCurrentStage():setFocus( t )
t.isFocus = true
– Store initial position
t.x0 = event.x - t.x
t.y0 = event.y - t.y
– Make body type temporarily “kinematic” (to avoid gravitional forces)
event.target.bodyType = “kinematic”
– Stop current motion, if any
event.target:setLinearVelocity( 0, 0 )
event.target.angularVelocity = 0
elseif t.isFocus then
if “moved” == phase then
t.x = event.x - t.x0
t.y = event.y - t.y0
elseif “ended” == phase or “cancelled” == phase then
display.getCurrentStage():setFocus( nil )
t.isFocus = false
– Switch body type back to “dynamic”, unless we’ve marked this sprite as a platform
if ( not event.target.isPlatform ) then
event.target.bodyType = “dynamic”
end
end
end
– Stop further propagation of touch event!
return true
end[/lua]

Note that I’ve also been working with organizing my external files a little more, if you want to move the file functions into a subfolder (ie: ./pkg/) you can change the require statement to

[lua]local f = require( “pkg/functions” )[/lua]

thanks for the input. this really helped me [import]uid: 14327 topic_id: 3575 reply_id: 16994[/import]