Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Code Splitting — Gideros Forum

Code Splitting

Hello,

I need your help again.

Currently, my project has more 3000 lines in its largest/main file. I have read that in Lua only global functions can be shared from separate files.
Since I am using Scenemanager, most of my function declarations are local (if I understand well scoping in Lua) like:

function level1:myfunction()

So my question is: how do you split your code? Do you?

Comments

  • MoKaLuxMoKaLux Member
    edited August 2020
    I split my code.
    I think i use global functions.
    You can have a look at some sample projects, for example rrraptor gh or my gh (see signature).
    Or this http://forum.giderosmobile.com/discussion/comment/63462/#Comment_63462
    my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
  • You can use includes.
    require "YourAnotherLuaFileWithCode"
    I'd care about readability&maintainability&modularity, they're more important in modern era than old-style lua optimizations like avoiding global functions, etc.
    > Newcomers roadmap: from where to start learning Gideros
    "What one programmer can do in one month, two programmers can do in two months." - Fred Brooks
    “The more you do coding stuff, the better you get at it.” - Aristotle (322 BC)
  • DafbDafb Member
    Apollo14 said:

    You can use includes.

    require "YourAnotherLuaFileWithCode"
    Yes. But I don't think in YourAnotherLuaFileWithCode.lua you can write

    function level1:onEnterFrame(e)

    when "level1" has been created in level1.lua.

    That is just an example. I don't intend to extract the main loop from its original file. However, I have a bunch of functions that look like that.
    Apollo14 said:

    I'd care about readability&maintainability&modularity, they're more important in modern era than old-style lua optimizations like avoiding global functions, etc.

    I have to consider this.




  • hgy29hgy29 Maintainer
    Dafb said:


    Yes. But I don't think in YourAnotherLuaFileWithCode.lua you can write

    function level1:onEnterFrame(e)

    when "level1" has been created in level1.lua.

    Actually you can, as long as level1 is a global variable and level1.lua was parsed before. A few rules I try to stick to:
    - Organize your code in classes/namespaces, with all methods of the same class in the same file.
    - Avoid 'require' and use code dependencies and --!NEEDS:file.lua to deal with loading order. This is Gideros specific though, if you plan to reuse your code in another engine, then go for 'require'
    - If some code files get too big and unreadable, try to find a way to seperate it in two or more sub-functionnalities and give them their own namespace/table/class
    - Use camel case for function names and class names
    - Use upper case for other global things (instances, constants)
    - Add comments around pieces of code that are not easy to read or understand at first sight

    Likes: oleg, jdbc

    +1 -1 (+2 / -0 )Share on Facebook
  • DafbDafb Member
    sceneManager = SceneManager.new({
        --start scene
        ["start"] = start,    
        ["level1"] = level1,   
        ["gameOver"] = gameOver
    })
    hgy29 said:


    - Organize your code in classes/namespaces, with all methods of the same class in the same file.
    - Avoid 'require' and use code dependencies and --!NEEDS:file.lua to deal with loading order. This is Gideros specific though, if you plan to reuse your code in another engine, then go for 'require'

    I indeed could use more classes. Where is that "--!NEEDS:file.lua" documented? Gideros being free and crossplatform, I don't plan to port the code over other engine, so it is very interesting.
  • olegoleg Member
    @Dafb Gideros when exporting glues all lua files into one file. So don't be afraid to do local functions in different files
    my games:
    https://play.google.com/store/apps/developer?id=razorback456
    мій блог по гідерос https://simartinfo.blogspot.com
    Слава Україні!
  • rrraptorrrraptor Member
    edited August 2020
    Dafb said:

    Where is that "--!NEEDS:file.lua" documented?

    According to a source code I guess that gideros rearanges you file loading. Like if you have 2 files: "file1.lua" and "file2.lua". And lets say "file1" have --!NEEDS:file2 tag, then file2 always loads before file1, and you will have acces to all global varaibles/function in "file1" from "file2". You can do same thingm but using dependencies window:



    Also, there is --!NOEXEC tag that does not load a file, so you have to do it by yourself using require function.
    dep.png
    384 x 907 - 24K
  • hgy29hgy29 Maintainer
    Accepted Answer
  • hgy29hgy29 Maintainer
    oleg said:

    Gideros when exporting glues all lua files into one file. So don't be afraid to do local functions in different files

    Files aren't concatenated, they are merged into a single file but each original file keep its own scope. Under the hood luac adds a loader that calls each chunk in sequence, as if each source file was enclosed into a big function.

    Likes: oleg

    +1 -1 (+1 / -0 )Share on Facebook
  • rrraptorrrraptor Member
    edited August 2020
    hgy29 said:

    oleg said:

    Gideros when exporting glues all lua files into one file. So don't be afraid to do local functions in different files

    Files aren't concatenated, they are merged into a single file but each original file keep its own scope. Under the hood luac adds a loader that calls each chunk in sequence, as if each source file was enclosed into a big function.

    like that?)
    do
    --file1 body
    end
     
    do
    --file2 body
    end
  • hgy29hgy29 Maintainer
    Sort of, it is more like 'dofile', but the end result is the same I think :)
  • DafbDafb Member
    Thank you guys for your help. I really appreciate it.

    Likes: MoKaLux

    +1 -1 (+1 / -0 )Share on Facebook
  • MoKaLuxMoKaLux Member
    edited August 2020
    hgy29 said:

    About --! tags: http://forum.giderosmobile.com/discussion/comment/63010/#Comment_63010. Not sure if it mades it to the wiki.

    I put it here: https://wiki.giderosmobile.com/index.php/Lua_API
    under Loading Order of Lua Files

    Likes: SinisterSoft

    my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
    +1 -1 (+1 / -0 )Share on Facebook
  • jdbcjdbc Member
    Just use one lua file for each class you define.

    Take a look to my game Dots, the source code is available on github:
    https://github.com/jdbcdev/Dots

    Likes: MoKaLux

    +1 -1 (+1 / -0 )Share on Facebook
Sign In or Register to comment.