Lua Effects - No Idea where to Start - Need Small Tutorial

Title isn’t very descriptive, so I’ll ask here.

My problem is that I need a small Lua file that shows me how I can make a simple particle effect, since I want to learn this for my new SWEP. Only wall I have is I’ve never even tried this stuff before, and I’m not very confident I could get all the functions down within a month.

Let’s say I wanted to make a shiny, red ball effect on a projectile. How would I draw this?

Don’t know if this is what you mean but.

That’s not what he meant( the shiny ball example ). Atleast I don’t think so, however that’s good for particle effects. I don’t know much about either, or where to start, so whoever answers killer_steels question would be helping more than one person, probably quite a few!

That?

That’s barely helpful. What I’m asking for is an actual code block that I can learn from, not a bunch of functions that I really can’t rope together into something reasonable or ledgible.

Thanks for the link, but it’s not what I’m looking for.

I’ll put a tutorial on the wiki sometime. Seems like there’s no info about it currently.

Please do!

Obliged, Bee. This is gonna help quite a few people.

While this isn’t something I’m particularly good with, I’ll try to summarize how I’ve been able to use the functions and what you could possibly do with them.

Starting off, you’ll probably want to know how to pass data along to the effect telling it what to do specifically to that time that you are spawning it. You can do this when spawning an effect using some of the functions found here. For instance, if you wanted to spawn your effect at the origin of the map you would want to do something along these lines.


local origin = Vector(0,0,0)
local ed = EffectData()
ed:SetStart( origin )
util.Effect( "myeffect", ed )

Now, on to actually creating the effect. You’ll need to have a folder directory similar to “…/gamemode_or_addon/entities/effects/myeffect” in order to actually spawn your effect, also note that the folder name of the effect is what you call when spawning it using util.Effect(). Inside the folder is only a single init.lua file including the 3 hooks found here. Generally, if you’re doing particle effects (Which is most likely), you’ll only really be using the init hook. Here’s a snippet of code with comments from a quick effect to show you how to use the functions that Chessnut had linked as well as this.


function EFFECT:Init( data )

	self.Target = data:GetStart() // Grabs the vector that was set before the effect was created.

	local Tar = self.Target

	local emitter = ParticleEmitter( Tar ) // Creates the actual emitter
	

		for i=1, 100 do // For loop to create 100 different particles		
				
			particle = emitter:Add( "sprites/light_glow02_add", Tar ) // Sets the particle's texture and starting position
				local spawnpos = Vector(math.random(-512,512),math.random(-512,512),math.random(-206,206)) // Sets the ending position for the particle; done differently on each particle, pushing them all in different directions, effectively making a sphere.
				local velvec = spawnpos:GetNormalized() // Normalizes the ending position
				local velmult = math.random(300,350) // Speed in which the particle travels toward it's ending position
				particle:SetVelocity(velvec*velmult) // Pushes the particle toward it's ending position at the randomized speed.
				particle:SetLifeTime(0)
				particle:SetDieTime(1) // Destroys the particle after 1 second
				particle:SetStartAlpha(255) // Starts at fully visible
				particle:SetEndAlpha(0) // Moves to not visible over the span of it's lifetime
				particle:SetStartSize(math.random(5, 20)) // Randomly sets a starting size
				particle:SetEndSize( 0 ) // Shrinks down to nothing at the end of it's lifetime
				particle:SetColor( 255, 255, 255 ) // Leaves the color as white
				particle:VelocityDecay( true )
				
		end
			

	emitter:Finish() // End's the emitter
end


function EFFECT:Think( )
	return false	
end


function EFFECT:Render()
end

With everything that I just showed you, the effect should look something like this in game if you were to change the starting point from the origin of the map to a players position. Of course you can’t see the particles below the player in that picture because of the platform below blocking it.

http://cloud.steampowered.com/ugc/596954390989997629/73480DCB10219D90FBEF5E1A3A73A412E1C9E744/

Of course there are a lot of other ways you can manipulate the particles to give any real effect you’d like. I hope that typing all of this out has been able to help you a decent amount on learning how to make effects, it really was a bitch to try to learn on my own and having a tutorial would have been useful.

Now this is how a tutorial SHOULD BE WRITTEN. This is very, very appreciated!

If only the wiki had more tutorials like this, awesome Lua effects and gamemodes would be flying out the door like high quality chocolate. I’ll give this all a shot when I get home.

Thanks again!

Usefull.

Working with particles is a pain in the ass, I suggest putting your lua file in autorun and doing openscript for fast reloading and testing.

I thought everyone did that already?

Not me,
I am still a user of ‘Luapad’ and gm_rawio, it is even faster then using openscript.
It is a wonder, that all the updates didnt break the functionality of it.
Greetings, Leakerify