Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Requiring composer in a module?
Started by d.mach Nov 17 2018 02:42 AM

3 replies to this topic
[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

d.mach

[GLOBAL: userInfoPane.html]
d.mach
  • Contributor

  • 757 posts
  • Corona SDK

Is it possible to use

local composer=require "composer"

in a module without the scene functions of composer, so I have access to (for example) the composer.state tables and values inside the module?

 

Can this cause problems during a scene change?

 

With module I mean something like this:

local mod={}
local composer=require "composer"

loca dostuff=function()

    -- code here
    composer.state.money=1000
end

mod.dostuff=dostuff

return mod

I then require the module inside a "normal" composer scene at top of this scene with:

local module=require("module")

Can this be done without "problems" during a scene change, reload of the scene, etc.?



[TOPIC: post.html]
#2

nick_sherman

[GLOBAL: userInfoPane.html]
nick_sherman
  • Corona Geek

  • 1,637 posts
  • Corona SDK

I think that should be okay, I do something similar when catching Runtime errors so I can send some info about the scene to my logging service.

 

Another alternative is to pass composer into any function that needs to do something with it.

 

 
local dostuff=function(composer)    -- code here
    composer.state.money=1000
end
 

 

 
module.doStuff(composer)
 



[TOPIC: post.html]
#3

davebollinger

[GLOBAL: userInfoPane.html]
davebollinger
  • Corona Geek

  • 1,259 posts
  • Enterprise

you can safely do this anywhere, as all it does it pull in a reference to the already-loaded* composer module.

 

*the only time you might encounter something weird is if you include a module (that includes composer) early on, in main.lua, before main.lua itself had required composer.  in that case your module's require will load composer and main's require will pull the reference.  even that should work properly for 99% of all "normal" uses.  only if you have some very special requirements about the timing of the composer load (because it does do some work at load time) might this be an issue.  if this makes no sense to you then you're not in the 1% that needs to even consider it.



[TOPIC: post.html]
#4

d.mach

[GLOBAL: userInfoPane.html]
d.mach
  • Contributor

  • 757 posts
  • Corona SDK

Thanks for your help!




[topic_controls]
[/topic_controls]