Hello recently ive been trying to make an entity that when you press E on it it opens up a vgui menu with 4 options. The options are red, blue, green and close. Ive had some success however when there are more than 1 it updates them all when you choose a new color. I want it so that if you have 3, you can walk up and change the colors of the 3 cubes individually instead of all 3 updating to whatever you chose last.
here is my Frankenstein code, im so sorry, im so ashamed. ='(
[CODE]AddCSLuaFile("shared.lua")
AddCSLuaFile("cl_init.lua")
include("shared.lua")
function ENT:SpawnFunction( ply, tr )
if ( !tr.Hit ) then return end
local ent = ents.Create( "StorageUnit" )
ent:SetPos( tr.HitPos + Vector(0, 0, 37) )
ent:Spawn()
ent:Activate()
ent.owner = ply
return ent
end
function ENT:Initialize()
self:SetModel( "models/hunter/blocks/cube05x05x05.mdl" )
self.Entity:SetSkin(0)
self:PhysicsInit( SOLID_VPHYSICS )
self:SetMoveType( MOVETYPE_VPHYSICS )
self:SetSolid( SOLID_VPHYSICS )
self:SetUseType(ONOFF_USE)
self.numberContents = 0
self.Contents = {}
local phys = self.Entity:GetPhysicsObject()
if (phys:IsValid()) then
phys:Wake()
end
end
function ENT:StartTouch( ent )
if( !IsValid( ent ) ) then return end
if ent.held or ent.held1 then
if self.numberContents < 8 then
local entTable = { class = ent:GetClass(), owner = self.owner }
self.numberContents = (self.numberContents + 1)
table.insert( self.Contents, entTable )
umsg.Start( "addItem" )
umsg.Short( self:EntIndex() )
umsg.String( ent:GetClass() )
umsg.End()
ent:Remove()
end
end
end
function ENT:WithdrawItem( class )
for k, v in pairs( self.Contents ) do
if v.class == class then
local item = ents.Create( class )
item:SetPos( self:GetPos() + self:GetForward() * 10 + self:GetUp() * 40 )
item:Spawn()
item:Activate()
if v.rot then
item.rotten = true
end
item.owner = v.owner
umsg.Start( "removeItem" )
umsg.Short( self:EntIndex() )
umsg.String( class )
umsg.End()
table.remove( self.Contents, k )
self.numberContents = (self.numberContents - 1)
return
end
end
end
function ENT:Use( ply )
ply.lastUse = ply .lastUse or CurTime()
if ply.lastUse > CurTime() then return end
ply.lastUse = CurTime() + 1
self:SendContents( ply )
end
function DoItemSpawn( ply, cmd, args )
local class = tostring( args[2] )
if !class then return end
local fridge = Entity( args[1] )
if !IsValid( fridge ) then return end
if !fridge:Visible( ply ) then return end
if fridge:GetPos():Distance( ply:GetPos() ) > 120 then return end
fridge:WithdrawItem( class )
end
concommand.Add( "StorageUnit_withdrawItem", DoItemSpawn )
function ENT:SendContents( ply )
if !(IsValid( ply ) and ply:IsPlayer()) then return end
umsg.Start( "openStorage", ply )
umsg.Short( self:EntIndex() ) --send the entity ID of the fridge we want.
umsg.End()
end
hook.Add("PhysgunPickup", "HoldingFood", function(pl, ent)
ent.held1 = true
end)
hook.Add("PhysgunDrop", "DroppedFood", function(pl, ent)
ent.held1 = false
end)
hook.Add( "GravGunOnPickedUp", "HoldingSomething", function( pl, ent )
ent.held = true
end )
hook.Add( "GravGunOnDropped", "DroppedSomething", function( pl, ent )
ent.held = false
end )[/CODE]
[LUA]
include("shared.lua")
function ENT:Initialize()
if !(CubeMenu and CubeMenu:IsValid()) then
local storage = vgui.Create( "CubeMenu" )
storage:SetPos( ScrW()*0.5 - storage:GetWide()*0.5, ScrH()*0.5 - storage:GetTall()*0.5 )
storage:SetVisible( false )
CubeMenu = storage --Define the global CubeMenu which will be a vgui for all fridges.
end
end
function ENT:Draw()
self.Entity:DrawModel()
end
function ENT:DrawColor()
self.Entity:SetColor(255, 0, 0, 255)
end
local PANEL = {}
function PANEL:Init()
self:SetSize( 300, 140 )
local label = vgui.Create( "DLabel", self )
label:SetPos( 120, 5 )
label:SetText( "Cube Menu" )
label:SizeToContents()
self.title = label
local button = vgui.Create( "DButton", self )
button:SetSize( self:GetWide() - 20, 24 )
button:SetPos( 10, self:GetTall() - 32 )
button:SetText( "Close" )
button.DoClick = function()
self:Close()
end
local button1 = vgui.Create( "DButton", self )
button1:SetSize( self:GetWide() - 20, 24 )
button1:SetPos( 10, self:GetTall() - 61 )
button1:SetText( "blue" )
button1.DoClick = function()
self:Close()
end
local button2 = vgui.Create( "DButton", self )
button2:SetSize( self:GetWide() - 20, 24 )
button2:SetPos( 10, self:GetTall() - 90 )
button2:SetText( "red" )
button2.DoClick = function()
self:Close()
end
local button3 = vgui.Create( "DButton", self )
button3:SetSize( self:GetWide() - 20, 24 )
button3:SetPos( 10, self:GetTall() - 119 )
button3:SetText( "green" )
button3.DoClick = function()
for id, list in pairs( self.lists ) do
if list and list:IsValid() then
end
end
self:Close()
end
self.lists = {}
end
function PANEL:OpenList( fridgeID )
self:Open()
for id, list in pairs( self.lists ) do
if list and list:IsValid() then
local ent = Entity( id )
if !IsValid( ent ) then
--This fridge was removed, lets get rid of the list
self.lists[ id ] = nil
else
--Hide the list we only want to show the current fridge we need
list:SetVisible( false )
end
end
end
local list = self.lists[ fridgeID ]
if list and list:IsValid() then
list:SetVisible( true )
else
--This is a new Refridgerator, we must make a list of items for it.
list = self:NewList( fridgeID )
list:SetVisible( true )
end
end
function PANEL:NewList( fridgeID )
local list = vgui.Create( "DPanelList", self )
list:SetSize( self:GetWide() - 20, self:GetTall() - 164 )
list:SetPos( 10, self.title:GetTall() + 10 )
list:EnableVerticalScrollbar( true )
list:EnableHorizontal( true )
list:SetPadding( 3 )
list:SetSpacing( 2 )
list:SetVisible( false )
list.items = {}
list.id = fridgeID
self.lists[ fridgeID ] = list
return list
end
function PANEL:Open()
gui.EnableScreenClicker( true )
self:SetVisible( true )
end
function PANEL:Close()
gui.EnableScreenClicker( false )
self:SetVisible( false )
end
function PANEL:Paint()
draw.RoundedBox( 10, 0, 0, self:GetWide(), self:GetTall(), Color( 40, 40, 40, 255 ) )
end
vgui.Register( "CubeMenu", PANEL, "Panel" )
local PANEL = {}
function PANEL:Init()
self:SetSize( 6, 200 )
self:SetMouseInputEnabled( true )
self.animHoverOn = Derma_Anim( "HoverOn", self, self.HoverOnAnim )
self.animHoverOff = Derma_Anim( "HoverOff", self, self.HoverOffAnim )
self.Alpha = 100
local label = vgui.Create( "DLabel", self )
label:SetPos( 5, 5 )
label:SetWide( self:GetWide() - 10 )
label:SetText( "" )
self.title = label
end
function PANEL:SetTitle( title )
self.title:SetText( title )
end
function PANEL:OnCursorEntered()
self.Armed = true
surface.PlaySound("UI/buttonrollover.wav")
self.animHoverOff:Stop()
self.animHoverOn:Start(.2)
end
function PANEL:OnCursorExited()
self.Armed = false
self.Selected = false
self.animHoverOn:Stop()
self.animHoverOff:Start(.2)
end
function PANEL:OnMousePressed()
self.Armed = true
self.Selected = true
end
function PANEL:OnMouseReleased()
self.Selected = false
surface.PlaySound("UI/buttonclickrelease.wav")
return self:DoClick()
end
function PANEL:Paint()
local bgColor = Color(160, 160, 160, 255)
local fgColor = color_black
local width = 64
local alpha = self.Alpha
local w, h = self:GetSize()
if self.Selected then
width = 82
bgColor = Color(170, 170, 170, 255)
fgColor = color_black
elseif self.Armed then
width = 88
bgColor = Color(160, 160, 160, 255)
fgColor = color_white
end
surface.SetDrawColor( 80, 80, 80, 255 )
surface.DrawRect( 0, 0, w, h )
surface.SetDrawColor( bgColor.r, bgColor.g, bgColor.
Im having terrible luck here, i just cant make the vgui result effect one entity separate from the others. I did however think of a way to make it work... once you choose an option trace the players view and the entity they are looking at take the effects
Check out the c4 entity in ttt.
This worked perfectly for me, ive been able to do everything i wanted with that code without settling. Ty so much!
Sorry, you need to Log In to post a reply to this thread.