Jump to content

Code For Playing An Emitter From A Prefab


gamecreator
 Share

Recommended Posts

I saved a prefab from an emitter then loaded it in my code.

 

Entity *expl1 = Prefab::Load("Prefabs/explosion.pfb");

 

The entity loads successfully. Question is simple: how do I play/stop/alter the emitter with code? Trying expl1->Play() returns 'Play': is not a member of 'Leadwerks::Entity'. What's the code to do this?

Link to comment
Share on other sites

Think the variable has to be of type Emitter so if you do this.

 

Entity *expl1 = Prefab::Load("Prefabs/explosion.pfb");

 

Emitter *emitter = (Emitter*)expl1;

 

emitter->Play();

emitter->Stop();

 

 

By casting it to the Emitter type I'm pretty sure you can Play/Stop it.

  • Upvote 1
trindieprod.png?dl=0spacer.png?dl=0steam-icon.png?dl=0twitter-icon.png?dl=0spacer.png?dl=0
Link to comment
Share on other sites

Thank you. That did it... kind of. The following code plays the emitter once if I uncomment the first line. It should keep playing the emitter since this is all in the App::Loop (play triggers every frame) but it doesn't. The keyhit code prints play! but doesn't play the emitter even once, even if the above play is commented out.

 

// emitter->Play();
if(window->KeyHit(Key::R))
{
  printf("play!\n");
  emitter->Play();
}

Link to comment
Share on other sites

No need to play() every frame, you only need to call it once (it actually should play by default when the entity is created).

 

Try:

 

emitter->SetLoopMode(true, true);

 

or

 

emitter->SetLoopMode(true, false);

if plan to release the emitter yourself.

 

Was thinking wonder if the Start function isn't being called in your emitter as prefabs don't call the start of child entities?

  • Upvote 1
trindieprod.png?dl=0spacer.png?dl=0steam-icon.png?dl=0twitter-icon.png?dl=0spacer.png?dl=0
Link to comment
Share on other sites

I can't get this to occur. Can you give more information about your emitter settings? Also, the Play() like many LE commands probably shouldn't be called every loop as it's meant to only resume playing a paused emitter.

 

I am not having any issues with this simple example:

window = Window:Create("Example", 0, 0, 800, 600,window.Center)

context = Context:Create(window)

world = World:Create()

camera = Camera:Create()

camera:SetPosition(0,1,-1.5)

light = DirectionalLight:Create()

light:SetRotation(35,35,0)

 

prefab = Prefab:Load("Prefabs/Effects/smoke.pfb")

prefab = tolua.cast(prefab,"Emitter")

 

while not window:KeyHit(Key.Escape) do

if window:Closed() then return false end

 

if window:KeyHit(Key.Space) then

if prefab:GetPaused() == true then --emitter is paused

prefab:Play()

else --emitter is not paused

prefab:Pause()

end

end

Time:Update()

world:Update()

world:Render()

context:SetBlendMode(Blend.Alpha)

context:DrawText("Press Space to Play/Pause",2,2)

context:SetBlendMode(Blend.Solid)

context:Sync(true)

end

 

Was thinking wonder if the Start function isn't being called in your emitter as prefabs don't call the start of child entities?

If the parent has a script, it will call the child script's Start() unless otherwise specified by NoStartCall. If the prefab is just the emitter as the prefab or the parent of the prefab then it will play with or without a script if the emitter settings are set that way.

 

And yes, the SetLoopMode needs to be set... bit i assumed he did that when he created the emitter prefab?

  • Upvote 1

Win7 64bit / Intel i7-2600 CPU @ 3.9 GHz / 16 GB DDR3 / NVIDIA GeForce GTX 590

LE / 3DWS / BMX / Hexagon

macklebee's channel

Link to comment
Share on other sites

I figured it out with your guys' help. Here's what went wrong: I assumed the prefab would load in at 0,0,0. It doesn't. It loads wherever it is in the editor. However, the first frame is a strange exception that confused me. If all you do is load the prefab and the emitter, the first frame of the loop will play it at the origin but the first Update will fix it. But, if you uncomment the SetLoopMode code in the Start function (which I didn't have there before you guys suggested it), even that's enough to position it to the editor position for the very first frame. In other words, most of the time the thing played but was off camera to me. But this weird possible bug (?) threw me off. I hope that makes sense.

 

Relevant code:

 


Emitter* emitter = NULL;

Entity *expl1 = NULL;

 

bool App::Start()

{

initializegame();

 

expl1 = Prefab::Load("Prefabs/explosion.pfb");

emitter = (Emitter*)expl1;

// emitter->SetLoopMode(true, true);

// emitter->SetPosition(0, 0, 0, true);

 

return true;

}

 

bool App::Loop()

{

if(window->Closed() || window->KeyDown(Key::Escape)) return false;

 

emitter->Play(); // This will play emitter at 0,0,0 - but only on the first frame and if SetLoopMode is commented above

 

if(window->KeyHit(Key::R))

{

printf("play!\n");

emitter->Play();

}

 

Time::Update();

world->Update();

world->Render();

 

return true;

}

  • Upvote 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...