Jump to content

Prefabbed emitters


wildcherrii
 Share

Recommended Posts

I have some emitters I've made in the editor, I saved them as prefab.  They dont attach to a map object or anything so I dont want to have them loaded up when the map loads but still i need to use them throughout my simulation.  My question is that it is possible to load a emitter up via prefab in lua code?  I sure dont want to have to code these emitters, if that were even possible.

 

I couldnt find anything on loading  premade emitters in code, just create(0 emitters which im trying to avoid.

 

 

Link to comment
Share on other sites

Edit: I wrote this loading code :

        if emit == null then 
             emit=Prefab:Load("Prefabs/Effects/myeffect.pfb")
             emit:GetLoopMode()
        end

 

But it returns 

"C:/myProject/scripts/util/snd.lua" : 17 : attempt to index global 'emit' (a nil value)

 

Link to comment
Share on other sites

Have you defined "emit" earlier in your code?

To get an idea how to use emitters in code have a look at the FPSGun script (Assets - Scripts - Objects - Weapons - FPSGun.lua ).

On line 105 we set up a table to hold the data for our emitters. The omission of this initial declaration is what is causing your error above. 

Then in the following lines we set up 3 emitters. One for debris, smoke and blood.  Each can have its own settings and materials.

Then to use them ( on line 350 ) we create a local variable ( in this case "e" ). We load our table data into "e" using "instance". This is quicker to do rather than loading data in. Then we show the emitter and set its position etc.

Hope this helps.

 

Link to comment
Share on other sites

Hi thanks for your reply.   I've tried the fps code prior to posting but I think something is wrong internally.  To test this I made a blank project,  in the MAIN.LUA script ( top level ),  before the main While/Do loop i added this code - 

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

then ran it,  it does the same thing as my normal project, it says :

"C:/Project/Scripts/Main.lua" : 17 : attempt to call method 'Play' (a nil value)

The variable lo is not in a function but above the main while/do loop in main.lua so it's globally declared I would presume.   Do i need to declare the variable as an Emitter:type or something ?

Running a debug on the variable the text returned was showing a loaded entity - userdata: 0x0432dba8

Link to comment
Share on other sites

On 3/31/2022 at 2:10 PM, wildcherrii said:

Hi thanks for your reply.   I've tried the fps code prior to posting but I think something is wrong internally.  To test this I made a blank project,  in the MAIN.LUA script ( top level ),  before the main While/Do loop i added this code - 

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

then ran it,  it does the same thing as my normal project, it says :

"C:/Project/Scripts/Main.lua" : 17 : attempt to call method 'Play' (a nil value)

The variable lo is not in a function but above the main while/do loop in main.lua so it's globally declared I would presume.   Do i need to declare the variable as an Emitter:type or something ?

Running a debug on the variable the text returned was showing a loaded entity - userdata: 0x0432dba8

What i learned from my little experience is that some commands only work inside functions but not outside of them. im getting a lot of these and then i switch some commands around like put them in a function or take something out of it so it isnt in a function and eventually, atleast for now, i ended up getting stuff to work. Maybe it helps you a bit, im a coding noob, so maybe im just talkin BS here, but atleast for me it worked out most of the time, even tho it can be tedious.

Link to comment
Share on other sites

I might be wrong here, but this is how I understand it. Prefab:Load returns object of a class Entity. But that class doesn't have a method Play. Only it's subclass Emitter has a method Play.

https://www.leadwerks.com/learn?page=API-Reference_Object_Entity

Maybe if you define the variable as Emitter first, and then load prefab into it, it remains as Entity class, I don't know. And you can also cast your Entity object to Emitter object. I think like this:

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

 

Link to comment
Share on other sites

Well its crazy, I honestly can't believe no on else has posted anything on this. 

emt_eff1 = tolua.cast(emt_eff1,"Emitter")
emt_eff1=Prefab:Load("Prefabs/smoke.pfb")
ctext (emt_eff1:GetClassName())

This returns Emitter as the class type.

But if I call any commands on the emitter Pause()  or Resume() then it errors out saying "Attempt to call method 'Pause' a nil value."

If I set the emitter to play and loop in the editor then load it with the above code it works fine, I just cant reference it be any command without an error.

How have people been loading prefab emitters this long without any reports on the forums? 

 

 

Link to comment
Share on other sites

Oh I might as well throw this in, in case some other person has this happen to them -  I found a work around, not sure it's conditional but for now it seems to work -

emt_eff1 = Emitter:Create()
emt_eff1:Release()
emt_eff1=Prefab:Load("Prefabs/smoke.pfb")
emt_eff1:Pause()

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...