gamecreator Posted May 9, 2017 Share Posted May 9, 2017 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? Quote Link to comment Share on other sites More sharing options...
tjheldna Posted May 9, 2017 Share Posted May 9, 2017 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. 1 Quote Link to comment Share on other sites More sharing options...
gamecreator Posted May 9, 2017 Author Share Posted May 9, 2017 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(); } Quote Link to comment Share on other sites More sharing options...
tjheldna Posted May 9, 2017 Share Posted May 9, 2017 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? 1 Quote Link to comment Share on other sites More sharing options...
macklebee Posted May 9, 2017 Share Posted May 9, 2017 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? 1 Quote 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 More sharing options...
tjheldna Posted May 9, 2017 Share Posted May 9, 2017 Also, If you have added this prefab to the world has it somehow lost it's instancing by the change of a value? 1 Quote Link to comment Share on other sites More sharing options...
gamecreator Posted May 9, 2017 Author Share Posted May 9, 2017 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; } 1 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.