How to dynamically choose external lua module

Hi guys.

I just discovered that Corona does not dynamically load physics data like Images or Sprites.

So when you do:

local physicsData = (require "obstacle").physicsData() local obstacle = display.newImageRect("images/obstacle.png", 50, 50) obstacle.x = centerX obstacle.y = centerY physics.addBody( obstacle, "static", physicsData:get(obstacle))

Corona always loads obstacle.lua, and never obstacle @2x.lua and obstacle @4x.lua

I am trying to fix this like this:

local imageSuffix = display.imageSuffix local imageSuffixVar = "obstacle" .. imageSuffix local physicsData = (require "imageSuffixVar").physicsData() local obstacle = display.newImageRect("images/obstacle.png", 50, 50) obstacle.x = centerX obstacle.y = centerY physics.addBody( obstacle, "static", physicsData:get(imageSuffixVar))

This solution does not work, I get “no field package imageSuffixVar preload” error.

How to dynamically choose external lua module?

Many thanks!

Ivan

You’re close :

local physicsData = require(moduleName).physicsData()

, where ‘moduleName’ contains a string which is the name of a valid file (or path)  minus the .lua extension.

Note: I don’t agree with what you’re trying to do though.  You should not need to manually load @X resources or their scripts.

Hi Ed,

I posted another thread today.

  • physics data @1x.lua is always loaded, no matter that @4x Image is loaded  :frowning:

Physics data is not dynamically selected based on image suffix.

Hmmm… Sorry to hear that.  I pretty much never use @X image selection so I don’t have any good examples of this or a way to check on this.  It sounds like you need to make a very basic example that demonstrates this and file a bug.

I’d also post a link to the sample here so folks can check it out and offer suggestions:

  1. Just make the three files: main.lua, config.lua, build.settings

  2. Include your spritesheets (png and lua files)

  3. in main.lua make a object using those sheets and add a body.

You’re trying to work around the problem and that’s not going to work out.  Give us a sample that demonstrates the issue and we (those who want to help and look) can help determine if it is an error on your part or a bug.

OK. I will file a bug tomorrow morning (here is 11.20 PM).

In the mean time I will try to do dynamic selection.

Thanks Ed.

Please make the example and post a link to the code here.  You’re going to need the example for your bug filing anyways.   Better to get help here first.  Again, we may see an error in your code/assumptions.

Hi Ed,

Try this  :smiley:

https://www.dropbox.com/s/4rb4532j76ftyx5/Physics%20data%20test.zip?dl=0

I added special print line in star.lua, star@2x.lua and star@4x.lua, so you see which one is loaded.

star@1x.lua is always loaded when @2x and @4x png`s are loaded (different device selected in Simulator)   :wacko:

Should I file a bug report?  :o

** UPDATE - Ah, I see you have the print line ** 

As far as I can tell, this is working fine.  I don’t see any issues.

https://www.youtube.com/watch?v=CXSf65QqYzw&feature=youtu.be

Note: From what I’m seeing. this means that the @2x and @4X lua files are redundant.  You get the proper physics body regardless.

I fail to see that there is a real issue here.  All you really care about is that the proper graphic resolution be selected and that the body be the right shape and size.  That works.

I’d just leave the @2x and @4x lua files in place, but ignore the fact they aren’t used/needed.

PS - If I were to warrant a guess, this is actually the correct behavior and the (redundant) files generated by the physics tool are not supposed to be generated and not needed.

Hi Ed,

My bad :slight_smile: !

Debug mode fooled me :frowning:

You are right!

So for future reference I just make @1x physics lua file, and Corona expands this to @2x and @4x?
I do not have to make @2x and @4x physics lua files?

Did I got this right? :slight_smile:

Many many thanks!
Ivan

So for future reference I just make @1x physics lua file, and Corona expands this to @2x and @4x?
I do not have to make @2x and @4x physics lua files?

No, that’s not what I meant.  

  1. You’re using a tool to generate these files right?

  2. That tool generates 2X and 4X lua files?

  3. If 1 and 2 are yes, just:

  • let the tool generate the files,
  • include them in your project as you have been
  • code as if you’re only dealing with the 1x files.  i.e. Let Corona worry about selecting the right images from those you provided (from the tool).  Don’t worry if the @2x, @4x, etc Lua files are not used.

The only thing you care about here is getting the right:

  • graphic selected for the right resolution (I verified this is working).
  • physics body to wrap that shape (again I verified this).

Question: What triggered this whole conversation?  i.e. What made you think this wasn’t working the the first place?  Was it the use of ‘debug’ instead of ‘hybrid’ draw modes?

Also, remember my tip about modifying the 1/2/4 X images (during debug) so you can visually verify which one is being used.

I am using Texture packer and Physics editor (yes 1 and 2).

Whole conversation was triggered by use of debug mode, and the fact that Corona never used @2x and @4x lua files.

Thanks Ed! :slight_smile:

You’re close :

local physicsData = require(moduleName).physicsData()

, where ‘moduleName’ contains a string which is the name of a valid file (or path)  minus the .lua extension.

Note: I don’t agree with what you’re trying to do though.  You should not need to manually load @X resources or their scripts.

Hi Ed,

I posted another thread today.

  • physics data @1x.lua is always loaded, no matter that @4x Image is loaded  :frowning:

Physics data is not dynamically selected based on image suffix.

Hmmm… Sorry to hear that.  I pretty much never use @X image selection so I don’t have any good examples of this or a way to check on this.  It sounds like you need to make a very basic example that demonstrates this and file a bug.

I’d also post a link to the sample here so folks can check it out and offer suggestions:

  1. Just make the three files: main.lua, config.lua, build.settings

  2. Include your spritesheets (png and lua files)

  3. in main.lua make a object using those sheets and add a body.

You’re trying to work around the problem and that’s not going to work out.  Give us a sample that demonstrates the issue and we (those who want to help and look) can help determine if it is an error on your part or a bug.

OK. I will file a bug tomorrow morning (here is 11.20 PM).

In the mean time I will try to do dynamic selection.

Thanks Ed.

Please make the example and post a link to the code here.  You’re going to need the example for your bug filing anyways.   Better to get help here first.  Again, we may see an error in your code/assumptions.

Hi Ed,

Try this  :smiley:

https://www.dropbox.com/s/4rb4532j76ftyx5/Physics%20data%20test.zip?dl=0

I added special print line in star.lua, star@2x.lua and star@4x.lua, so you see which one is loaded.

star@1x.lua is always loaded when @2x and @4x png`s are loaded (different device selected in Simulator)   :wacko:

Should I file a bug report?  :o

** UPDATE - Ah, I see you have the print line ** 

As far as I can tell, this is working fine.  I don’t see any issues.

https://www.youtube.com/watch?v=CXSf65QqYzw&feature=youtu.be

Note: From what I’m seeing. this means that the @2x and @4X lua files are redundant.  You get the proper physics body regardless.

I fail to see that there is a real issue here.  All you really care about is that the proper graphic resolution be selected and that the body be the right shape and size.  That works.

I’d just leave the @2x and @4x lua files in place, but ignore the fact they aren’t used/needed.

PS - If I were to warrant a guess, this is actually the correct behavior and the (redundant) files generated by the physics tool are not supposed to be generated and not needed.