Is particle candy can only be required in the main.lua as global?

Hello,

I am trying to figure out the best to include the particle candy lua code. The only plae where it seems to wok is main.lua? I am using director and i would thought that using something like this on the screen i need the engine:

1- local Particles = require(“lib_particle_candy”)

Instead the thing that work seems to be something like (only in the main.lua)

2- Particles = require(“lib_particle_candy”)

Any reason why [1] does not work for me?

Thanks

Mo [import]uid: 49236 topic_id: 17429 reply_id: 317429[/import]

I’m no expert, but I think “local” signifies that Particles is only related to main.lua, whereas without the “local” it can be called by other functions in other files. [import]uid: 66117 topic_id: 17429 reply_id: 66023[/import]

Hi Joe.

Thanks so much for taking the time. That will be my interpretation of local too. The thing I do not understand with particle candy is that it does not seems to like to be required in the game screen where i actually needed. I tried both using local and as global in the game screen but no luck. It seems to be only happy in the main.lua section? Anybody noticed that? I thought you could put “require” a module anywhere in your code and use local if you need that module only one specific screen?

Thanks again for taking the time!

Mo [import]uid: 49236 topic_id: 17429 reply_id: 66037[/import]

I use it on my game screen, I also use Director Class. on my game screen LUA here is how I call it in and it works as expected Make sure you call it in at the TOP of your LUA file. Its local but i think only items Below it in your code will see it. so if you do the require at the bottom of your code, I don’t think anything will see it:

–*****************************************************
–***** Game Settings
–*****************************************************
local director = require(“director”)
local ui = require(“ui”)
local physics = require(“physics”)

–local ads = require “ads”

local Particles = require(“lib_particle_candy”) [import]uid: 67604 topic_id: 17429 reply_id: 66109[/import]

Thank so much rgleason for your confirmation. I feel like %^^$% It seems that it is not the Particle engine but the FXlibrary (B) that is giving me trouble…

[lua]module(…, package.seeall)
local Particles = require(“lib_particle_candy”)
local FXLibrary = require(“lib_particleEffects_01”)[/lua]

I have put those two statements all the way to the top (as seen at the above) but still FXlibrary is giving me a problem:

Runtime error
…ME~1\CORONA~2\Sandbox\129\lib_particleEffects_01.lua:80: attempt to index global ‘Particles’ (a nil value)

I did not noticed that the error was related to the FX effect rather the actual PC engine. I have also to admit that i did modified the FX library to have only the effects i need (explosions rather the sparks, water effects…) Maybe(probably) I screwed up!?

I need to figure out what i did to the FXlibrary that makes it only available when in the main.lua (and no local before it) Unfortunately I cannot simply commented out the FX library require since I am using it (FX) on my code.

Any event, thank you so much for taking the time!

Mo [import]uid: 49236 topic_id: 17429 reply_id: 66147[/import]

I don’t call in the FX library at all in my code. I create my own effects and particles inside my playgame LUA file. Here is a sample of what i did that works.

Step 1: Create your Particles (Copy one from one of the samples you like)

--\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* Step 1 Define your Particles \*\*\*\*\*  
Particles.CreateParticleType ("Sparkle", {  
 imagePath = "images/effects/colored\_stars.png",  
 imageWidth = 64,   
 imageHeight = 64,   
 velocityStart = 65,   
 velocityVariation = 65,  
 velocityChange = -1.0,  
 directionVariation = 330,  
 alphaStart = 0,   
 alphaVariation = .25,   
 fadeInSpeed = 2.0,   
 fadeOutSpeed = -0.85,   
 fadeOutDelay = 500,   
 scaleStart = 0.01,   
 scaleVariation = 0.2,  
 scaleInSpeed = 0.5,  
 weight = 0,   
 autoOrientation = true,   
 rotationVariation = 360,  
 killOutsideScreen = true,   
 lifeTime = 4000,   
 useEmitterRotation = false,  
 emissionShape = 0,  
 emissionRadius = 30,  
 blendMode = "add"  
} )  

Step 2 - Create the emitter you will eventually use.

--\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* Step 2 - Create your Emitters \*\*\*\*\*  
  
-- CREATE EMITTERS (NAME, SCREENW, SCREENH, ROTATION, ISVISIBLE, LOOP)  
  
Particles.CreateEmitter("Explosion3", display.contentWidth, display.contentHeight, 300, false, false) -- Sparkle Explosion   

Step 3 - Assign Particles to your emitter

--\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* Step 3 - Assign Particles to your Emitter  
  
Particles.AttachParticleType("Explosion3", "Sparkle", 25, 0,0)   
  

This sets up the emitter. you then later call it and display it as needed.

[code]
– Identify the emitter i want to use
if emitterID == 3 then emitterToDisplay = “Explosion3” end

–Put the emitter on the screen where i want it
Particles.StartEmitter(emitterToDisplay, true)
Particles.GetEmitter (emitterToDisplay).x = emitterX – emitterX could be a event.other.x reference if thats how you want to place it
Particles.GetEmitter (emitterToDisplay).y = emitterY – emitterY could be a event.other.y reference if thats how you want to place it
Particles.SetEmitterScale(emitterToDisplay,2.0)
[/code] [import]uid: 67604 topic_id: 17429 reply_id: 66235[/import]

Thanks rgleason!

Actually that is the way I started but I had alot of effects and so I wanted a cleaner look on my gameScreen.lua. For that reason, I decided to use the FX library which makes the code cleaner since all the effects settings are in the library. Your confirmation that you can put the PC engine in any module means probably that the FX library is different for some reason. And needs to be in main.lua

I may actually switch back to your way to see if I can speed up my app that way. I am using a lot of effects and I can see that in my older device like the ipod touch third gen, it is not so great right now (even on iphone 4 but it is smoking on ipad2!) I was able to improve quite a bit by making my explosion smaller in size…

Thanks again for the taking the time to answer me. I appreciated very much.

Mo. [import]uid: 49236 topic_id: 17429 reply_id: 66289[/import]

Hi Mo,
LUA files that need to be required by your code should always rest in your project’s root folder. Otherwise it may give you some headaches. While requiring LUA files from a sub folder works with the windows SDK, it won’t work when testing it on an iOS device, for example. Therefore, the best practice is always to place your LUA files or modules in your project’s root folder.

There are several ways to organize your particle effects in a clean way, just create a second LUA file (for example “particle_types.lua”) in your project folder and place your particle type definitions in there:

“particle_types.lua”
[lua]local ParticleTypes = {}

ParticleTypes.Type1 =
{
– PARTICLE TYPE PROPERTIES HERE
– PARTICLE TYPE PROPERTIES HERE
– …
}

ParticleTypes.Type2 =
{
– PARTICLE TYPE PROPERTIES HERE
– PARTICLE TYPE PROPERTIES HERE
– …
}

return ParticleTypes[/lua]
Once you load this LUA file into your code using require(), you should be able to access the particle type definition and attach them to your emitters at any time:

[lua]Particles = require(“lib_particle_candy.lua”)
ParticleTypes = require(“particle_types.lua”)

– CREATE AN EMITTER:
Particles.CreateEmitter(“E1”, 50, 50, 0, true, true)

– CREATE A NEW PARTICLE TYPE:
Particles.CreateParticleType (“Test1”, ParticleTypes.Type1)

– ATTACH IT TO YOUR EMITTER:
Particles.AttachParticleType(“E1”, “Test1”, 12, 99999,0)

– TRIGGER YOUR EMITTER:
Particles.StartEmitter(“E1”)[/lua]
This is a slightly different approach as used with the included effects library -you simply move all the particle type defintions code into a seperate file to keep your code clean and slim.

When you experience problems accessing the Particle Candy library functions from anywhere in your code, you should also consider to keep the library reference global (using no local here):

[lua]-- LOAD PARTICLE LIBRARY (NO LOCAL USED)
Particles = require(“lib_particle_candy”)[/lua]
Hope this clarifies things a little. [import]uid: 10504 topic_id: 17429 reply_id: 66937[/import]

Thanks x-pressive.com!

I like the moving the particles def into a new module. I will assume I will need to use “module(…, package.seeall)” in the new lua module “particle_types.lua” since I am still using the old way of doing things (I am affraid to change since I am almost done with my app…

Thanks again

Mo.

ps: and yes, i have currently both the PC and the library in the root folder where main.lua is. [import]uid: 49236 topic_id: 17429 reply_id: 67085[/import]

No, you don’t need to use the module() function with the code posted above. [import]uid: 10504 topic_id: 17429 reply_id: 67167[/import]

Cool. Good to know.

Thanks.

Mo [import]uid: 49236 topic_id: 17429 reply_id: 67180[/import]