Loading another lua file

Hi, since this Engine removed dofile in lua, how can I load another lua file?

For example, in main menu scene (mainmenu.lua), I want to load Options scene which is defined in options.lua [import]uid: 9190 topic_id: 2099 reply_id: 302099[/import]

Try this:

[code]
require “options” – loads options.lua

– to call a function in options.lua
options.OptionsScene() – or whatever the function is named
[/code] [import]uid: 7559 topic_id: 2099 reply_id: 6280[/import]

Hi, thanks for you supporting.

But could you please share a sample for OptionsScene(). I am totally newbie with both Corona and Lua.
Is there only 1 *display* object in game, how can I hide/show a mainmenu and show/hide other group/scene?
local mainmenu = display.newGroup();
[import]uid: 9190 topic_id: 2099 reply_id: 6284[/import]

I don’t have any samples to share but I’m sure others on this forum do. You can also search the forum because this topic has been discussed before and I know others have posted code samples.

In Corona you can either hide groups/objects or remove them (if they will not be used again). If your groups/objects are using lots of images, it may be best to remove them (object.removeSelf) and recreate them when needed because of the amount of memory they use (and limited memory on the device). You can hide a group/object by setting the object.isVisible to false.

-Tom [import]uid: 7559 topic_id: 2099 reply_id: 6285[/import]

Tom, I am working on a game that has many levels. I am planning to code the levels as individual files and do the level management from the main.lua file. However, If I require all the files from the begin of the game a huge amount of memory is going to be allocated for the graphics involved.

I am thinking of putting the “require” statements only when I need to load each level and when finishing with it to kill/deallocate every image, listener, table etc that this level uses. For the deallocation of the level I can only think of a function (inside the othefile.lua) that, when called from main.lua, would remove everything used in that file, but this sounds as too much work.

Is there any other method to implement several levels while preventing memory overload? [import]uid: 7356 topic_id: 2099 reply_id: 6324[/import]

we also have a game (rather an educational app) which we’d like to port to iphone and adroid, but it consists of 200 different activities which all take around 600kilobytes each. of course that includes gfx and sound too, but does that mean that at least we would have to load the scripts for all 200 activities at once in memory?

Can’t we just create 1 lua file per activity and let each lua file init its gfx and snd and on exit remove it from memory, but being able to dynamically loading each separate script when a certain activity is needed and not by preloading them all? if each script would be 50kb, then 200 x 50 kb = 10MB of script while you only use 50 kb of it at any time. looks like a big waste of resources, more even if we would add more activities.

removing DOFILE is actually very restricting a very nicefeature of the lua interpreter. [import]uid: 4426 topic_id: 2099 reply_id: 6448[/import]

Instead of creating 1 lua file per activity, I would create an “assets” file per activity. Each asset file would hold the levels’s graphics, sounds, etc.
When you reach a level, I would load the level asset file and load its graphics, sounds, assets, and some sort of game logic that you could then use to determine the logic for the given level.

Think of it as a text file

level1.bin

<backgroundimage><br><eventsound1><br><eventsound2><br><mp3sound1><br><object1><br><object1><object1><br><logic><br><panel x000x0000x000x000xxxxx00xxx000000><br><panel2><br></panel2></panel></logic><br>

Most games are designed that way. Crack open some of the top games and you will see that most of the data on the assets file are similar to the example above. Obviously, it will take some time to figure out the type of assets and data you will need to put into each file, but architecturally, this is no different than what most games do out there. And once you get this going, you will be able to rapidly deploy similar games without having to recode the logic. Just drop in a bunch of data files, you should then be able to crank out new levels, and games faster than before.

carlos
[import]uid: 24 topic_id: 2099 reply_id: 6462[/import]

Unfortunately thats not what I mean. I understand that LEVELS in a game can be made this way, but we are not making a game with 200 levels. Its actually 200 different blocks of logic and these have indeed datafiles lke gfx and snd which you load dynamically, but with what you suggest here it looks like all 200 blocks of logic (some are over 50KB) have to be loaded in memory at once. Thats a huge mem usage.

We are fillowing corona for some time now, but to be able to use it in our country we would really need to see how to solve this. We don’t want to sell 200 separate apps but just 1 containing them all and being able to dynamically load as needed. But it looks like you cannot load in extra scripts from the resources.

I understand Apple is picky on this to not be able to load in scripts over the air/internet, but at least you could allow loading from some kind of resource folder? That way you cannot alter the scripts once appstore aproved it. [import]uid: 4426 topic_id: 2099 reply_id: 6471[/import]

Actually, I meant you could put your “logic” on a data file. I know its difficult to showcase and interpret on a forum but as my example shows

<logic><br><type><br><object1><br><completion><br><failed><br><nextlevel><br></nextlevel></failed></completion></object1></type></logic>  

Think of this file as an XML file with a section for game logic.

Lets see if we can look at it from a different angle.

main.lua level1.bin holds the logic code

main.lua -> loads level1.bin -> parses the logic and creates a game play out of it.

Here is a better sample snipplet from a game one of our Members of the Board of Advisors wrote. Shown with his permission.

<g id="PanelTitle"><br><br>	<image width="298" height="200" id="PanelTitle.png" xlink:href="PanelTitle.png" transform="translate(11 213)"><br>	</image><br>	<rect id="DRLArea" x="38" y="319" fill="none" width="24" height="25"></rect><br></g>  

Notice above, the rectID=“DLArea”

In the game logic, when interpreting this rectangle, DLArea means something.

C [import]uid: 24 topic_id: 2099 reply_id: 6474[/import]

Carlos, do you confirm that we have to manually load and unload each “level”? I cannot see why this is not a disadvantage of Corona. The unload function has to destroy everything that the create function loads. This is double work, just to avoid memory overflow. Other platforms support a “change level” sort of method that automatically unload everything involved in a scene/level.

The mechanism you describe above may be clean to read but in its essence it also has to load and unload the assets. Moreover, there is the burden of listeners, tables etc that the developer has to destroy for the memory to be kept in balance.

Am I missing something here?

Thanks! [import]uid: 7356 topic_id: 2099 reply_id: 6475[/import]

with LOGIC I actually meant lua scripts. I don’t see how to be able to save scripts inside a datafile if you cannot load them and execute them.

[import]uid: 4426 topic_id: 2099 reply_id: 6476[/import]

Because of security restrictions you cannot load Lua scripts from a data file. The best you can do is load and unload assets (images, sound files, data, etc.). The logic that supports these assets must be preloaded.

There is not way to automatically remove a “level” from your code. You can add all the display objects into a group for a particular “level” and when you remove the group, all the children of the group are removed and the resources cleared. You still need to manually remove any listeners that were created for the level.

-Tom [import]uid: 7559 topic_id: 2099 reply_id: 6477[/import]

Actually, I am going to look into two things

a) documenting a loading/unloading logic/etc for games
b) write up some funky code.

Give me time though, there are other things brewing :wink:

me. [import]uid: 24 topic_id: 2099 reply_id: 6524[/import]

Thanks Carlos!
That would be wonderful. It seems to be the only grey area for folks. All of the Ansca Project Examples are wonderful, but none of them (game wise) change levels (or as some are calling scenes) while clearing out those assets at the next. Thanks! [import]uid: 8621 topic_id: 2099 reply_id: 6542[/import]

Yes, please can we have an example of a “proper” skeleton project for a game with levels, menu, etc.

I’m sure this would help a lot of people! [import]uid: 8353 topic_id: 2099 reply_id: 6618[/import]

FYI.

There is another forum post in the Game Edition Category requesting a Project Template with Scenes, Menus, Options and Levels.

http://developer.anscamobile.com/forum/2010/09/18/game-project-template#comment-6543

I posted a link to a blog that has a gaming style template available for free download.
I’m not sure if it’s a good one. Hope someone with more experience in LUA will check it out and let us know.

Derek [import]uid: 8621 topic_id: 2099 reply_id: 6623[/import]

I tried the same and it works but I want the same in other way ,I mean to say that it sould be on the event …

Yogesh… [import]uid: 168801 topic_id: 2099 reply_id: 118274[/import]

nm - I see it is a zombie thread :slight_smile: [import]uid: 160496 topic_id: 2099 reply_id: 118276[/import]

Hi Tom,

I got the button event now its working but i have another problem regarding the rotation follwing is the code for the same.

isRotated = false
local rotatefilter --function reserve
local rotatestart --function reserve
local rotatecomplete --function reserve
local curOrientation = mainOr --inital launch always match mainOr
local rota = 0
local isrotating = false
local rd = 0

function rotatecomplete( )
isrotating = false
if curOrientation == altOr then isRotated = false else isRotated = false end
if curOrientation ~= rotatefilter() then rotatestart(rotatefilter()) end
end

function rotatestart( val, initial )

if isrotating == false and curOrientation ~= val then
local textObject1 = display.newText( “OK” , 50, 100, nil, 30 )
if val == mainOr then
local textObject1 = display.newText( “ROTA-L” , 50, 150, nil, 30 )
rota = 180
curOrientation = mainOr
else
local textObject1 = display.newText( "ROTA -R " , 50, 350, nil, 30 )
rota = 180
curOrientation = altOr
end
isrotating = true
–if initial then
transition.to( stage, { rotation=rota, time=0, delta=true, onComplete=rotatecomplete } )
–else
–transition.to( stage, { rotation=rota, time=rd, delta=true, transition=easing.inOutQuad, onComplete=rotatecomplete } )
–end
end
–displaynew1();

–Check initial orientation and and rotate if needed
–if ( system.orientation == altOr and isrotating == false) then
–rotatestart(altOr,true)
–end
end

Runtime:addEventListener( “orientation”, rotatestart ) [import]uid: 168801 topic_id: 2099 reply_id: 118435[/import]