Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Use of modules and the lua require function — Gideros Forum

Use of modules and the lua require function

techdojotechdojo Guru
edited June 2012 in General questions
This is mainly a question aimed at @Atilim & @Gorkem however I'd appreciate input from any Lua experts out there.

I've noticed that the example programs do not use the Lua "require" function to load in external modules and in my own current demos I tend to wrap everything up in Classes and it just seems to work (granted I am aware of the code dependency setting in Studio), so my question is, does the code dependency setting mean that you don't need to use the require function anymore? if so how do you get round circular dependencies?

I'm starting to write a generic skinnable UI library (called widget) and normally (in Corona or other Lua frameworks) I'd plan on using the require function to load the module into the main package.loaded table to make it available either in the global namespace
require "widget"
 
button = widget.newButton(...)
or as a local reference
local widget = require "widget"
 
button = widget.newButton(...)
Where widget.lua would look something like this...
local widget = {} 
 
function widget.newButton(options)
    return Button.new(options)
end
 
-- etc etc
 
return widget
The above is basically a factory that just wraps each widget control (most probably a class that's derived from a sprite or a bitmap) and returns it in a consistent manner.

Is it worth using the require function or would it be better to just force the widget.lua function to be loaded first?

Likes: Roland_Y

WhiteTree Games - Home, home on the web, where the bits and bytes they do play!
#MakeABetterGame! "Never give up, Never NEVER give up!" - Winston Churchill
+1 -1 (+1 / -0 )Share on Facebook

Comments

  • atilimatilim Maintainer
    edited June 2012
    Both approaches are totally ok but most probably I would go with the require function.

    and afaik, this usage:
    local widget = require "widget"
    creates both local and global references. (oh and I shouldn't forget package.loaded["widget"] reference. hi @Rickyngk :) )

    But if you're planning to implement a widget library, most probably you will need clipping functionality :-\" heheh :)



    edit: hmm.. I was wrong about global reference. I mean in widget.lua, you should write as:
    widget = {}
    so that widget reference will be available as a global reference when you load the module as:
    require "widget"
  • But am I right in thinking that if you use the require function as
       local lw = require "widget"
    and have the widget file as above that a local (ie private) table called widget would be created once (regardless of the number of files that require is used) and that "lw" (in this case) would be a local reference to that table and hence be faster to access?

    How does the use "require" fit in with the "normal" Gideros setup which I assume is based on the fact that as each file is "loaded" in the order specified any globals, functions etc are parsed as they are found, added to the global namespace and as such are then recognised as needed (I was going to say required but I thought that might get confusing :) )
    WhiteTree Games - Home, home on the web, where the bits and bytes they do play!
    #MakeABetterGame! "Never give up, Never NEVER give up!" - Winston Churchill
  • I'm not sure what you are asking but I know that Gideros includes all of the files included in your project and you can right click on each file and get a popup that allows you to set which files it depends on.

    I often use my downloader on my iPad to load projects from Dropbox and bypass the Gideros IDE completely and so when I open the example projects I have to manually add the requires.
  • Can you give me a heads up on how you get it to work on the ipad without using the IDE?
    WhiteTree Games - Home, home on the web, where the bits and bytes they do play!
    #MakeABetterGame! "Never give up, Never NEVER give up!" - Winston Churchill
  • I created a Gideros app and modified the Xcode project so that it downloads the code from a url. I haven't posted the project anywhere but I have posted the file that I'm downloading. Basically my app downloads the file described in the following url, and that file downloads all of my project files. It is a little more work but I like working on my iPad.

    http://blog.magnusviri.com/gideros-downloader.html

  • I totally agree with @MikeHart's comment on your blog - pure Awesome! thanks :)
    WhiteTree Games - Home, home on the web, where the bits and bytes they do play!
    #MakeABetterGame! "Never give up, Never NEVER give up!" - Winston Churchill
  • gorkemgorkem Maintainer
    HEADS UP: Please give some love (login + give point) for James' awesomeness :

    http://news.ycombinator.com/item?id=4083768
  • I think you did just by leaving a comment!
    WhiteTree Games - Home, home on the web, where the bits and bytes they do play!
    #MakeABetterGame! "Never give up, Never NEVER give up!" - Winston Churchill
  • gorkemgorkem Maintainer
    To give a point, go to http://news.ycombinator.com/newest and find the related link in 2nd page, and click on ^ character.
  • atilimatilim Maintainer
    @Magnusviri pleasure to watch your video :)>-

    @techdojo you're totally correct. but when the users call
    require "widget"
    they expect a global reference is created. On the other hand, Lua's module system is simple and flexible. There isn't one rule or one way. A single line to the documentation is enough.

    You just need to right click on .lua file and select "Exclude from execution". So that this .lua file won't be executed at the beginning of the application and you can use require to load it anytime you want. (If you don't select "Exclude from execution", it will be executed twice: once at the beginning and when require function is used)
  • Ahhh - that makes sense, I actually came across a similar thing when I was looking at your simple tile map example - I added a print statement into the lua code that Tiled was exporting and it appeared twice in the console, once at the beginning and once when the "loadfile" command was executed.

    I must remember to reorganise my project files and then keep all "data" files in a separate folder so that I can "exclude" them from execution.

    Thanks for the tip!

    :)
    WhiteTree Games - Home, home on the web, where the bits and bytes they do play!
    #MakeABetterGame! "Never give up, Never NEVER give up!" - Winston Churchill
  • A little bit confused:
    local socket = require "socket": return socket module
    local socket = require "src/socket": return boolean value
  • looks like guava7 is full of diggers :))

    :)
    +1 -1 (+2 / -0 )Share on Facebook
  • ar2rsawseenar2rsawseen Maintainer
    edited October 2013
    probably because it can not find it?
    http://stackoverflow.com/questions/15988268/calling-functions-on-required-modules-in-lua-gives-me-attempt-to-index-local

    true is usually returned by require if module function is not used inside module and module code doesn't return a value.
    But anyway it seems strange.
  • I am having a little difficulty with this, I have a set of modules, if they are in the root folder and require("module.lua") its ok.
    but if its in a folder. require("includes/module.lua")]
    it fails.
    Any ideas?
    REAL programmers type copy con filename.exe
    ---------------------------------------
  • ar2rsawseenar2rsawseen Maintainer
    hmm, interesting I never seem to require files, rather module names.
  • amaximovamaximov Member
    edited March 2014
    @Cyberience you should use "includes.module.lua". Use . instead of / for directories when requiring files. :)
  • @Cyberience

    Are you excluding the file in your project properties?
Sign In or Register to comment.