Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Are subfolder Particle Systems possible?
Started by richard9 Jul 26 2014 06:05 AM

17 replies to this topic
display.newventemitter particles particle folder
[TOPIC CONTROLS]
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

richard9

[GLOBAL: userInfoPane.html]
richard9
  • Corona Geek

  • 1,118 posts
  • Enterprise

I remember seeing a small discussion of this on the announcement page, and ran into the same core problem myself:

 

The Corona particleDesigner library doesn't support subfolders.

 

Assuming a basic ParticleDesigner 2 setup; both the textures and json file need to be in the root directory or bust.

 

Is there a bug filed for this? Or a relatively common workaround?



[TOPIC: post.html]
#2

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 25,471 posts
  • Enterprise

Hi Richard.  I did a query on the bug data base for particle and folder and didn't find anything.  Perhaps you should file one.

 

Rob



[TOPIC: post.html]
#3

ingemar

[GLOBAL: userInfoPane.html]
ingemar
  • Corona Geek

  • 2,733 posts
  • Enterprise

I have ParticleDesigner json and png files in a subfolder without issues.

 

Use this function for a fix:

-- -------------------------------
-- emitter functions
-- -------------------------------

local newEmitter = function(emitterFile, baseDir)
    baseDir = baseDir or system.ResourceDirectory

    local filePath = system.pathForFile(emitterFile, baseDir)
    local f = io.open(filePath, "r")
    local fileData = f:read("*a")
    f:close()

    local emitterParams = json.decode(fileData)

    -- fix start --------------------------------
    -- Corona builds do not support particle textures in subfolders
    -- the code below fixes this issue
    local slashPos = nil
    local tmpPos = 0

    -- find last slash in input string
    repeat 
        tmpPos = emitterFile:find("/", tmpPos + 1)

        if (tmpPos) then
            slashPos = tmpPos
        end
    until not tmpPos

    if (slashPos) then  
        local subfolder = emitterFile:sub(1, slashPos)

        -- future-proofing in case CoronaLabs fixes this issue
        if (not emitterParams.textureFileName:find("/")) then
            emitterParams.textureFileName = subfolder .. emitterParams.textureFileName
        end
    end
    -- fix end ----------------------------------

    local emitter = display.newEmitter(emitterParams)

    return emitter
end

Usage:

local emitter = newEmitter("img/emitters/MyAwesomeEmitter.json")

This will search for the json and png in the img/emitters folder in the ResourceDirectory.



[TOPIC: post.html]
#4

richard9

[GLOBAL: userInfoPane.html]
richard9
  • Corona Geek

  • 1,118 posts
  • Enterprise

Thanks ingemar but on trying your code it definitely doesn't work in cases where there are multiple subfolders. (i.e.: "assets/particles") I can get your code to find the JSON in a deep subfolder but that's it, the image must be in root for it to work.



[TOPIC: post.html]
#5

ingemar

[GLOBAL: userInfoPane.html]
ingemar
  • Corona Geek

  • 2,733 posts
  • Enterprise

I'll have to fix that in my code...

One quick and dirty fix you can do is to edit the json file and point the png to the directory you want.
This is the root of the problem. The json file doesn't include the file path for the png.
Once you've manually added the path to the png, you should be fine.

[TOPIC: post.html]
#6

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,516 posts
  • Corona SDK

Quick chime in.  Hopefully I understood the question, but "Corona" can handle having the particle images in subfolders no problem.  I use the exact technique Ingemar just mentions (overriding image location before creating particle).



[TOPIC: post.html]
#7

ingemar

[GLOBAL: userInfoPane.html]
ingemar
  • Corona Geek

  • 2,733 posts
  • Enterprise

@richard

 

I tested my function, and it works perfectly with nested subfolders.

One thing though. The function above replaces the call to display.newEmitter() so you shouldn't call the latter yourself.

 

There are two options to fix this issue:

1) Use my function above instead of display.newEmitter()

2) Edit the json file in a text editor and add the path to the png entry



[TOPIC: post.html]
#8

richard9

[GLOBAL: userInfoPane.html]
richard9
  • Corona Geek

  • 1,118 posts
  • Enterprise

Tore my code apart 3 times, and finally got it to work. About all I found out is that appending strings inside of a system.pathForFile() call doesn't seem to work. But thanks ingemar, finally got it functional.

 

From a purely "Corona user" perspective I would suggest building your function differently (99% of users won't need to specify a baseDir, but I bet 50% want to specify custom folders) - I was initially expecting it to work as newEmitter(file, subDir, baseDir) but even just showing the usage method there helps a lot.

 

I will try and file a bug with Corona later today, though; they seem to operate under this just working so may as well make it official that it does not. :)



[TOPIC: post.html]
#9

ingemar

[GLOBAL: userInfoPane.html]
ingemar
  • Corona Geek

  • 2,733 posts
  • Enterprise

@richard

 

Ah! I think I see the source of confusion...

 

Yes, the 'baseDir' in my function is only for the base directory, not the subdirectories of the files. The baseDir there, as you found out the hard way, is just the base directory, which only needs to be the ResourceDirectory here and never needs to be changed. If you want to specify multiple subdirectories within the base dir, you specify those as in my Usage example above.

 

I hoped that the "Usage" example would've clarified where to put the subfolders for the asset itself.

(Note: The json and png must be in the same subfolder)

 

Sorry about that...



[TOPIC: post.html]
#10

andrew.shaw

[GLOBAL: userInfoPane.html]
andrew.shaw
  • Observer

  • 15 posts
  • Corona SDK

I can confirm that changing the image file path does fix the issue, however, their is a case when there is not a fix.  I haven't have the time to provide an accurate test app to reproduce, but when the particle effect and image is another directory folder other than Resources it errors.  For example, if you extract a zip file to CacheDirectory that contains your particles, it will fail.



[TOPIC: post.html]
#11

ingemar

[GLOBAL: userInfoPane.html]
ingemar
  • Corona Geek

  • 2,733 posts
  • Enterprise

I've modified my function above to take the baseDir as an argument.

If you use my function above and pass system.CachesDirectory as the second argument, it should work. 

(This assumes that the PNG and JSON is in the same place though)



[TOPIC: post.html]
#12

pau7

[GLOBAL: userInfoPane.html]
pau7
  • Enthusiast

  • 40 posts
  • Corona SDK

@ingemar

 

Thank you for your solution, but it seems that you can only place the texture file in the ResourcesDirectory. I tried to place both .json and .png files in the DocumentsDirectory and it crashed trying to find the .png file. Looking at emitterParams table, I didn't find any option to set the directory. Any suggestion would be really appreciated. Thank you.



[TOPIC: post.html]
#13

ingemar

[GLOBAL: userInfoPane.html]
ingemar
  • Corona Geek

  • 2,733 posts
  • Enterprise

@pau7

What does your call to newEmitter look like?



[TOPIC: post.html]
#14

pau7

[GLOBAL: userInfoPane.html]
pau7
  • Enthusiast

  • 40 posts
  • Corona SDK

This is how I call your function:

local emitter = newEmitter("library/book001/leaves.json", system.DocumentsDirectory)

And it reads leaves.json without any problem.

 

It crashes at the end of your constructor:

local emitter = display.newEmitter(emitterParams)

This is the error message: CAN'T find texture name: library/book001/leaves.png

The file leaves.png is in this folder.

 

Thank you in advance!



[TOPIC: post.html]
#15

ingemar

[GLOBAL: userInfoPane.html]
ingemar
  • Corona Geek

  • 2,733 posts
  • Enterprise

@pau7

If I can ask one more thing:

Open your leaves.json file. What does the "textureFileName" line look like?

Is it something like this: "textureFileName" : "leaves.png" ?



[TOPIC: post.html]
#16

ingemar

[GLOBAL: userInfoPane.html]
ingemar
  • Corona Geek

  • 2,733 posts
  • Enterprise

@pau7

 

Bummer. It looks like there's nothing I can do to fix this.

Corona's display.newEmitter() expects to find the jpg in the Resource directory and I can't find any way around it.



[TOPIC: post.html]
#17

pau7

[GLOBAL: userInfoPane.html]
pau7
  • Enthusiast

  • 40 posts
  • Corona SDK

@ingemar

 

I understand. Thank you anyway. I will post a feature request that let us choose where to place texture file.



[TOPIC: post.html]
#18

pau7

[GLOBAL: userInfoPane.html]
pau7
  • Enthusiast

  • 40 posts
  • Corona SDK

Here you can vote the feature request to add the possibility of choosing the base directory of both files (.json and .png) of the particle system emitter.

 

http://feedback.coronalabs.com/forums/188732-corona-sdk-feature-requests-feedback/suggestions/6573916-choose-base-directory-for-texture-images-in-partic

 

 




[topic_controls]
[/topic_controls]