[Solved] Reloading a particle script?

Everything related to 3D programming
User avatar
Samuel
Enthusiast
Enthusiast
Posts: 755
Joined: Sun Jul 29, 2012 10:33 pm
Location: United States

[Solved] Reloading a particle script?

Post by Samuel »

In order to reload a particle script you must reload the material script (that the particle script uses) after the particle script is reloaded.
I'm not sure if this is the proper way, but it does work for me. If anyone knows a reason why I shouldn't do it this way let me know.

New working example.

Code: Select all


#CameraSpeed = 10

IncludeFile "Screen3DRequester.pb"

Define.f KeyX, KeyY, MouseX, MouseY
Declare CreateParticleScript()


Result.q=FileSize("Data/Particles/TestParticle.particle")
If Result>0
   DeleteFile("Data/Particles/TestParticle.particle")
EndIf


If InitEngine3D(#PB_Engine3D_DebugLog)
  
  InitSprite()
  InitKeyboard()
  InitMouse()
  
  If Screen3DRequester()

    Add3DArchive("Data/Textures"            , #PB_3DArchive_FileSystem)
    Add3DArchive("Data/Scripts"             , #PB_3DArchive_FileSystem)
    Add3DArchive("Data/Particles"           , #PB_3DArchive_FileSystem)
    Parse3DScripts()
    
    KeyboardMode(#PB_Keyboard_International)  
    
    ; Particles
    ;
    CreateParticleScript()

    ReloadMaterial("Particle/JetEngine1", "TestParticle.particle", #True)
    ReloadMaterial("Examples/Flare", "Examples.material", #True)
    Result=GetScriptParticleEmitter(0, "Particle/JetEngine1")
    If Result=0
      MessageRequester("Information","Could not find particle emitter!")
    EndIf

    ; Camera
    ;
    CreateCamera(0, 0, 0, 100, 100)
    MoveCamera(0, 0, 0, 300, #PB_Absolute)
    
    Repeat
      Screen3DEvents()
      
      If ExamineMouse()
        MouseX = -MouseDeltaX() * #CameraSpeed * 0.005
        MouseY = -MouseDeltaY() * #CameraSpeed * 0.005
      EndIf  
      
      If ExamineKeyboard()
        
        If KeyboardPushed(#PB_Key_Left)
          KeyX = -#CameraSpeed 
        ElseIf KeyboardPushed(#PB_Key_Right)
          KeyX = #CameraSpeed 
        Else
          KeyX = 0
        EndIf
        
        If KeyboardPushed(#PB_Key_Up)
          KeyY = -#CameraSpeed 
        ElseIf KeyboardPushed(#PB_Key_Down)
          KeyY = #CameraSpeed 
        Else
          KeyY = 0
        EndIf
          
      EndIf
      

      RotateCamera(0, MouseY, MouseX, 0, #PB_Relative)
      MoveCamera  (0, KeyX, 0, KeyY)
      
      RenderWorld()
      Screen3DStats()
      
      FlipBuffers()
    Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
  EndIf
  
Else
  MessageRequester("Error", "The 3D Engine can't be initialized", 0)
EndIf

End

Procedure CreateParticleScript()
  If CreateFile(0, "Data/Particles/TestParticle.particle")
    Restore Material
    Line$ = "//Particle Test"
    While Line$<> "END" 
      WriteStringN(0, Line$) 
      Read.s Line$
    Wend
    CloseFile(0)
  Else
    MessageRequester("Information","Can not create the file!")
  EndIf
EndProcedure


DataSection
Material: 
Data.s "// A jet engine (of sorts)"
Data.s "particle_system Particle/JetEngine1"
Data.s "{"
Data.s "	material 		Examples/Flare"
Data.s "	particle_width 	25"
Data.s "	particle_height	25"
Data.s "	cull_each		false"
Data.s "	quota			200"
Data.s "	billboard_type	point"
Data.s "	emitter Point"
Data.s "	{"
Data.s "		angle 5"
Data.s "		emission_rate 100"
Data.s "        time_to_live    1"
Data.s "        direction       0 -1 0"
Data.s "        velocity_min    250"
Data.s "        velocity_max    300"
Data.s "        colour_range_start  1 1 0.5"
Data.s "        colour_range_end    1 0.8 0.3"
Data.s "	}"
Data.s "	affector ColourFader"
Data.s "	{"
Data.s "		red -0.25"
Data.s "		green -1"
Data.s "		blue -1"
Data.s "	}"
Data.s "}"
Data.s "END"
EndDataSection
Last edited by Samuel on Sat Nov 09, 2013 9:10 pm, edited 1 time in total.
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Re: Reloading a particle script?

Post by applePi »

Hi Samuel
look example GetScriptParticle.pb he uses HideParticleEmitter with other tactics to change between particles
regarding your example ReloadMaterial does not work with particles , so i think to use
FreeParticleEmitter(0) before reloading the particle again
Result=GetScriptParticleEmitter(0, "Examples/Rain")

also your example you should use CreateParticleScript() before Parse3DScripts()
since Parse3DScripts() needs to parse the scripts in TestParticle.particle
before manipulating it later in the program
your example, press space to change particles to rain

Code: Select all

#CameraSpeed = 10

IncludeFile "Screen3DRequester.pb"

Define.f KeyX, KeyY, MouseX, MouseY
Declare CreateParticleScript()


Result.q=FileSize("Data/Particles/TestParticle.particle")
If Result>0
   DeleteFile("Data/Particles/TestParticle.particle")
EndIf


If InitEngine3D()
  
  InitSprite()
  InitKeyboard()
  InitMouse()
  
  If Screen3DRequester()

    Add3DArchive("Data/Textures"            , #PB_3DArchive_FileSystem)
    Add3DArchive("Data/Scripts"             , #PB_3DArchive_FileSystem)
    Add3DArchive("Data/Particles"           , #PB_3DArchive_FileSystem)
    
    CreateParticleScript()
    Parse3DScripts()
    
    KeyboardMode(#PB_Keyboard_International)  
    
    ; Particles
    ;
    ;CreateParticleScript()
    ;ReloadMaterial("Particle/JetEngine1", "TestParticle.particle", #True)
     Result=GetScriptParticleEmitter(0, "Particle/JetEngine1")
    If Result=0
      MessageRequester("Information","Could not find particle emitter!")
    EndIf

    ; Camera
    ;
    CreateCamera(0, 0, 0, 100, 100)
    MoveCamera(0, 0, 0, 300, #PB_Absolute)
    
    Repeat
      Screen3DEvents()
      
      If ExamineMouse()
        MouseX = -MouseDeltaX() * #CameraSpeed * 0.005
        MouseY = -MouseDeltaY() * #CameraSpeed * 0.005
      EndIf  
      
      If ExamineKeyboard()
        
        If KeyboardPushed(#PB_Key_Space)
          
          FreeParticleEmitter(0)
          Result=GetScriptParticleEmitter(0, "Examples/Rain")
          
        EndIf 
        
        If KeyboardPushed(#PB_Key_Left)
          KeyX = -#CameraSpeed 
        ElseIf KeyboardPushed(#PB_Key_Right)
          KeyX = #CameraSpeed 
        Else
          KeyX = 0
        EndIf
        
        If KeyboardPushed(#PB_Key_Up)
          KeyY = -#CameraSpeed 
        ElseIf KeyboardPushed(#PB_Key_Down)
          KeyY = #CameraSpeed 
        Else
          KeyY = 0
        EndIf
          
      EndIf
      

      RotateCamera(0, MouseY, MouseX, 0, #PB_Relative)
      MoveCamera  (0, KeyX, 0, KeyY)
      
      RenderWorld()
      Screen3DStats()
      
      FlipBuffers()
    Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
  EndIf
  
Else
  MessageRequester("Error", "The 3D Engine can't be initialized", 0)
EndIf

End

Procedure CreateParticleScript()
  If CreateFile(0, "Data/Particles/TestParticle.particle")
    Restore Material
    Line$ = "//Particle Test"
    While Line$<> "END" 
      WriteStringN(0, Line$) 
      Read.s Line$
    Wend
    CloseFile(0)
  Else
    MessageRequester("Information","Can not create the file!")
  EndIf
 
EndProcedure


DataSection
Material: 
Data.s "// A jet engine (of sorts)"
Data.s "particle_system Particle/JetEngine1"
Data.s "{"
Data.s "   material       Examples/Flare"
Data.s "   particle_width    25"
Data.s "   particle_height   25"
Data.s "   cull_each      false"
Data.s "   quota         200"
Data.s "   billboard_type   point"
Data.s "   emitter Point"
Data.s "   {"
Data.s "      angle 5"
Data.s "      emission_rate 100"
Data.s "        time_to_live    1"
Data.s "        direction       0 -1 0"
Data.s "        velocity_min    250"
Data.s "        velocity_max    300"
Data.s "        colour_range_start  1 1 0.5"
Data.s "        colour_range_end    1 0.8 0.3"
Data.s "   }"
Data.s "   affector ColourFader"
Data.s "   {"
Data.s "      red -1"
Data.s "      green -0.25"
Data.s "      blue -1"
Data.s "   }"
Data.s "}"
Data.s "END"
EndDataSection
User avatar
Samuel
Enthusiast
Enthusiast
Posts: 755
Joined: Sun Jul 29, 2012 10:33 pm
Location: United States

Re: Reloading a particle script?

Post by Samuel »

Hello ApplePi,
applePi wrote: look example GetScriptParticle.pb he uses HideParticleEmitter with other tactics to change between particles
regarding your example ReloadMaterial does not work with particles , so i think to use
FreeParticleEmitter(0) before reloading the particle again
Result=GetScriptParticleEmitter(0, "Examples/Rain")
GetScriptParticle.pb uses scripts that were all pre built. I was hoping for a solution for particle scripts that will be created during runtime.
Also, I don't need to swap particles just create one during runtime.
applePi wrote: also your example you should use CreateParticleScript() before Parse3DScripts()
since Parse3DScripts() needs to parse the scripts in TestParticle.particle
before manipulating it later in the program
This is not true for material scripts. One can use ReloadMaterial() to load a script created at runtime after
Parse3DScripts(). I was looking for a similar way of doing that, but with a particle script instead of a material script.
I know OGRE has this capability, but I'm not sure if Purebasic has made use of it yet.


Thanks for the help so far. If I'm not making any since let me know and I'll try to explain myself better.
User avatar
Samuel
Enthusiast
Enthusiast
Posts: 755
Joined: Sun Jul 29, 2012 10:33 pm
Location: United States

Re: [Solved] Reloading a particle script?

Post by Samuel »

I solved the problem. See my original post for the working example.
Post Reply