How to use IMaterials?

I’m trying to make a beam draw from an attacker to a victim using the beam rendering functions, but i can’t get render.SetMaterial( IMaterial ) to work. The problem is i have no idea how to make an IMaterial. Does anyone know how to do this?

Thanks in advance!

Probably render.DrawBeam would be better.

That’s exactly what he said he was doing. :wink: That function requires you use render.SetMaterial.

Anyway as can be seen on the wiki page provided by yakahughes use Material(“path/to/material”) to create an iMaterial object.

Thanks, I’ll try that out.

[editline]01:59PM[/editline]

I have another problem now. The code probably works, but when i try to run it on the PlayerTraceAttack hook, it won’t draw. I can always make a table for traces to make, so the trace wont need to happen on that hook. What hook should i use?

[editline]02:28PM[/editline]

Oh, heres the code BTW



	function DrawHit( ply, dmginfo, vec, tr )
		atk = dmginfo:GetAttacker()
		if atk:IsPlayer() then
			render.SetMaterial( Material("cable/redlaser") )
			render.DrawBeam( atk:GetShootPos(), tr.HitPos, 5, 0, 0, Color( 255, 255, 255, 255 ) ) 
			print("true")
			print(atk:Nick())
			print(ply:EntIndex())
		end
	end
	
	hook.Add("PlayerTraceAttack","hit",DrawHit)


That’s because all drawing has to be done in a painting hook such as HUDPaint or ENT.Draw. It has to be done for a period of time to be visible.

Ah, okay… The render.DrawBeam wiki page said it would only work with draw hooks. I thought that meant only the ENT:Draw hook, so I didn’t want to try that, only to find out it won’t work.

[editline]06:32PM[/editline]

I’m still having issues with this, the system works (prints in console), but no beams. Here is the code now:



	CritTrace = {}
	Laser = Material("cable/physbeam")
	Crits = 0
	
	function DrawCrit()
		for k,v in pairs(CritTrace) do
			render.SetMaterial( Laser )
			render.DrawBeam( v[1], v[2], 20, 0, 0, 255,255,255,255 ) 
			if !timer.IsTimer("critting") then
				timer.Create("critting",5,1,function()
					print("critting...")
					timer.Remove("critting")
				end)
			end
		end
	end
	
	function AddCrit( ply, dmginfo, dir, tr )
		atk = dmginfo:GetAttacker()
		print("Attacker: "..atk:Nick().." at "..atk:GetShootPos())
		print("Victim: "..ply:Nick().." at "..tr.HitPos)
		if atk:IsPlayer() and ply:IsPlayer() then
			table.insert(CritTrace,{atk:GetShootPos(),tr.HitPos})
			print("Crit added")
			Crits = Crits + 1
			timer.Create("undocrit"..Crits,5,1,function()
				table.remove(CritTrace,1)
				print("Crit removed")
			end)
		end
	end
	
	hook.Add("PlayerTraceAttack","AddCrit",AddCrit)
	hook.Add("HUDPaint","drawcrit",DrawCrit)


Note that the last 4 entries in the render.DrawBeam function are intended to be like that (i’ve had problems with the color thing before, so i thaught i’d try it with numbers instead)

Doesn’t anyone know why it’s not working?

You are trying to use Render serverside. It needs to be clientside.

Nope, the whole script is clientside. Anyway, I’ve made some progress on this.

The method i currently use involves spawning an entity and keeping it in front of the player at all times, so i can use the ENT:Draw() hook. I’m not very happy with this method, and if there is a better way, please tell me.

You can try a PostPlayerDraw hook.

Can someone actually verify that this works before I tear down my whole system and redo it?

Apparently, PostPlayerDraw isn’t a gamemode function, but i can confirm that it works with the PostDrawOpaqueRenderables hook.

[editline]01:13PM[/editline]

[lua]local mat = Material(“cable/redlaser”)
local col = Color(255, 255, 255, 255)
hook.Add(“PostDrawOpaqueRenderables”, “test”, function( )
local v1 = Vector(0, 0, 200)
local v2 = Vector(100, 100, 400)
render.SetMaterial(mat)
render.DrawBeam(v1, v2, 5, 0, 0, col)
end)[/lua]

This code produces this:

http://img267.imageshack.us/img267/1158/beam.png

[editline]01:14PM[/editline]

Sorry about the quality of that picture, I’m on my laptop.

THANK YOU!!! Have a cookie.