How to make a custom entity support transparency like "prop_physics"?
4 replies, posted
Currently I can only get "prop_physics" entities with my model attached to it support propper transparency...
Like this works:
local ent = ents.Create("prop_physics")
ent:SetModel("models/mymdl/my_mdl.mdl")
-- .....
...This dosen't (I don't know what to define in the scripted entity...):
local ent = ents.Create("my_scripted_ent")
ent:SetModel("models/mymdl/my_mdl.mdl")
-- ....
The model applied to my own scripted entity:
https://files.facepunch.com/forum/upload/350606/53226e67-5c97-46f1-aeda-9644162126c9/000.jpg
The model applied to the "prop_physics" entity:
https://files.facepunch.com/forum/upload/350606/2ace89d6-0742-4285-9440-08d56fa329ed/001.jpg
RENDERMODE TRANSCOLOR
Color(255, 255, 255, 100)
Draw the model clientside also.
Also set rendermode to RENDERGROUP_BOTH (I think this is needed)
Thanks! You got me on the right track. Got it working.
Here is what I did. I need animation also some time in the future maybe... So it is almost working on the clientside... Do you know why the animation on the clientside runs like maybe 2-5 frames and the jumps back to the start? It is working fine on the serverside entity!
shared.lua
ENT.Type = "anim"
ENT.Base = "base_entity"
ENT.AutomaticFrameAdvance = true
ENT.PrintName = "Custom Entity!"
ENT.Author = "ravo Norway"
ENT.Spawnable = true
ENT.AdminSpawnable = true
ENT.Category = "RAVO-NORWAY"
-- Pre-cache
util.PrecacheModel("models/my_custom_entity/my_custom_entity.mdl")
init.lua
AddCSLuaFile("shared.lua")
AddCSLuaFile("cl_init.lua")
include("shared.lua")
util.AddNetworkString("startAnimation")
function ENT:Initialize()
-- Sets what model to use
self:SetUseType(SIMPLE_USE)
self:SetModel("models/my_custom_entity/my_custom_entity.mdl")
self:SetRenderMode(RENDERMODE_TRANSCOLOR)
self:SetColor(
Color(
255,
255,
255,
100
)
)
-- Physics stuff
self:PhysicsInit(SOLID_VPHYSICS)
self:SetMoveType(MOVETYPE_VPHYSICS)
self:SetSolid(SOLID_VPHYSICS)
-- Init physics only on server, so it doesn't mess up physgun beam
if (SERVER) then self:PhysicsInit(SOLID_VPHYSICS) end
-- Make prop to fall on spawn
local phys = self:GetPhysicsObject()
if (IsValid(phys)) then phys:Wake() end
end
function ENT:Think()
-- Fast animation
if SERVER then
self:NextThink(CurTime())
return true
end
end
function ENT:Use(activator, caller)
self:Open(activator)
end
-- --- -
-- Animation
local function fireAnimation(self, activator, animationName)
self:ResetSequence(animationName)
net.Start("startAnimation")
net.WriteTable({
Name = animationName,
Parent = self
})
net.Send(activator)
end
function ENT:Open(activator)
fireAnimation(self, activator, "open")
end
cl_init.lua
include("shared.lua")
function ENT:Draw()
self:DrawModel()
end
hook.Add("OnEntityCreated", "OnEntityCreated:MyCustomEntity001", function(ent)
if (
ent:IsValid() and
ent:GetClass() == "my_custom_entity"
) then
local ghostEnt = ClientsideModel(ent:GetModel(), RENDERGROUP_BOTH)
ghostEnt:SetPos(ent:GetPos())
ghostEnt:SetParent(ent)
ghostEnt:Spawn()
ghostEnt:Activate()
-- Alpha settings
ghostEnt:SetRenderMode(RENDERMODE_TRANSCOLOR)
ghostEnt:SetColor(
Color(
255,
255,
255,
200
)
)
-- -
ent:CallOnRemove("CallOnRemove"..ent:EntIndex(), function()
if ghostEnt:IsValid() then
ghostEnt:Remove()
end
end)
end
end)
-- Animation
net.Receive("startAnimation", function()
local animationData = net.ReadTable()
local parent = animationData.Parent
local animationName = animationData.Name
for _,MaybeGhostChild in pairs(parent:GetChildren()) do
if string.match(string.lower(MaybeGhostChild:GetClass()), string.lower("C_BaseFlex")) then
if not parent:IsValid() or not MaybeGhostChild:IsValid() then return end
-- OK; Set
MaybeGhostChild:ResetSequence(animationName)
break
end
end
end)
Set the base to "base_anim".
Shared =
ENT.AutomaticFrameAdvance = true
Serverside =
function ENT:Think()
self:NextThink(CurTime())
return true
end
These two should make animations working for your entity.
Okay thanks I will try it out one day I got time
Sorry, you need to Log In to post a reply to this thread.