load functions from a non-storyboard file

Hi everyone,
I need some help with Storybord. It wouldn’t let me post directly in the storyboard forum so please move this topic if need be.

I have a file within my project that holds a lof of functions etc for my game. How do I make references to those functions from within storyboard?

For example, I have my storyboard scenes main, mainMenu and mainScene (original I know).
I also have a file called env.lua which holds a lot of functions and call-backs and processes.
I want to call one of the functions in the env.lua file (let’s say it’s called load()) from within my mainScene.lua file (which is a storyboard scene).

How would I got about doing that?
I’ve trided to add

require = "env"

at the beginning of the mainScene.lua file and call the load() function but I just get errors.

Can someone please shed some light on this for me?
I am new to programming and this is my first attempt at a game.

Thanks,
William. [import]uid: 88841 topic_id: 29863 reply_id: 329863[/import]

First, when using require you typically want to do:

[lua]local env = require(“env”)[/lua]

Or

[lua]require(“env”)[/lua]

As for using the functions inside “env”, if the functions are not local then you can simply call the functions with no further logic needed. This is the method I use because I am lazy. If you want to leave them as local then read up on this article http://www.coronalabs.com/blog/2011/09/05/a-better-approach-to-external-modules/.
[import]uid: 147305 topic_id: 29863 reply_id: 119746[/import]

There are several ways to do this.

  1. For a file “foobar.lua” containing this:
function foo()  
 print("In foo")  
end  
  
function bar()  
 print("In bar")  
end  

You can access foo() and bar() like this in your scene file (put this code at top):

local myFooBar = require("foobar")  

later you can access the functions thus:

myFooBar.foo()  
myFooBar.bar()  
  1. Alternately, you could make the functions local to your scene like this (this will make the execution faster):
local myFooBar = require("foobar")  
local foo = myFooBar.foo  
local bar = myFooBar.bar  

later you can access the functions thus:

foo()  
bar()  
  1. For a file “foobar.lua” containing this:
local aTable = {}  
aTable.foo = function ()  
 print("In foo")  
end  
  
aTable.bar = function()  
 print("In bar")  
end  
  
return aTable  

You can access foo() and bar() like this in your scene file (put this code at top):

local myFooBar = require("foobar")  

later you can access the functions thus:

myFooBar.foo()  
myFooBar.bar()  

Note: Example #3 is very similar to #1, but now your functions are attached to table which is generally more modular and allows for more complex code constructions. That might however be a topic for another day.

Whatever the case, I hope the above helps.
[import]uid: 110228 topic_id: 29863 reply_id: 119988[/import]

First, when using require you typically want to do:

[lua]local env = require(“env”)[/lua]

Or

[lua]require(“env”)[/lua]

As for using the functions inside “env”, if the functions are not local then you can simply call the functions with no further logic needed. This is the method I use because I am lazy. If you want to leave them as local then read up on this article http://www.coronalabs.com/blog/2011/09/05/a-better-approach-to-external-modules/.
[import]uid: 147305 topic_id: 29863 reply_id: 119746[/import]

There are several ways to do this.

  1. For a file “foobar.lua” containing this:
function foo()  
 print("In foo")  
end  
  
function bar()  
 print("In bar")  
end  

You can access foo() and bar() like this in your scene file (put this code at top):

local myFooBar = require("foobar")  

later you can access the functions thus:

myFooBar.foo()  
myFooBar.bar()  
  1. Alternately, you could make the functions local to your scene like this (this will make the execution faster):
local myFooBar = require("foobar")  
local foo = myFooBar.foo  
local bar = myFooBar.bar  

later you can access the functions thus:

foo()  
bar()  
  1. For a file “foobar.lua” containing this:
local aTable = {}  
aTable.foo = function ()  
 print("In foo")  
end  
  
aTable.bar = function()  
 print("In bar")  
end  
  
return aTable  

You can access foo() and bar() like this in your scene file (put this code at top):

local myFooBar = require("foobar")  

later you can access the functions thus:

myFooBar.foo()  
myFooBar.bar()  

Note: Example #3 is very similar to #1, but now your functions are attached to table which is generally more modular and allows for more complex code constructions. That might however be a topic for another day.

Whatever the case, I hope the above helps.
[import]uid: 110228 topic_id: 29863 reply_id: 119988[/import]