-
Notifications
You must be signed in to change notification settings - Fork 4
Example
Linus045 edited this page Dec 1, 2023
·
15 revisions
A simple panel you can open and close with a chat command.
A simple menu that shows how to interact with buttons.
A simple example using the linkToVehicle() function
@persist Frame:dframe
if(first()) {
#add all players to the default list
vguiDefaultPlayers(players())
Frame = dframe(1)
Frame:setSize(150,150)
Frame:center() #puts the frame in the center of the screen
Frame:setVisible(0) #makes the panel invisible
Frame:setDeleteOnClose(0) # only hides the panel when the close button is pressed, not delete it
Frame:makePopup() #makes the panel move in the foreground
Frame:create() #creates the panel on the clients
}
event chat(Player:entity, Message:string, Team:number) {
if(Message=="menu") {
local Visible = Frame:isVisible(Player) #get the current visible status of the panel of a specific player
Frame:setVisible(!Visible) #inverse the status (makes it visible if it's invisible and visible if it's invisible)
Frame:modify(array(Player)) #apply changes only to the player who executed the command
}
}(Obsolete) Old way
@persist Frame:dframe
if(first()) {
runOnChat(1)
#add all players to the default list
vguiDefaultPlayers(players())
Frame = dframe(1)
Frame:setSize(150,150)
Frame:center() #puts the frame in the center of the screen
Frame:setVisible(0) #makes the panel invisible
Frame:setDeleteOnClose(0) # only hides the panel when the close button is pressed, not delete it
Frame:makePopup() #makes the panel move in the foreground
Frame:create() #creates the panel on the clients
}
if(chatClk()) {
if(lastSaid()=="menu") {
local Visible = Frame:isVisible(lastSpoke()) #get the current visible status of the panel of a specific player
Frame:setVisible(!Visible) #inverse the status (makes it visible if it's invisible and visible if it's invisible)
Frame:modify(array(lastSpoke())) #apply changes only to the player who executed the command
}
}A simple menu that shows how to interact with buttons.
@persist Main:dframe [Button Button2]:dbutton
if(first()) {
vguiDefaultPlayers(players()) #add all players to the default list
Main = dframe(1)
Main:setTitle("Menu")
Main:setPos(60,300)
Main:setColor(255,120,120)
Main:setDeleteOnClose(0) #only hides the panel when closing it instead of deleting it
Main:setVisible(0)
Main:makePopup()
Main:enableKeyboardInput(0) #disables the keyboard input, you can still move but your mouse is locked on the panel
#needs to be called after :makePopup()
Main:create()
Button = dbutton(2,1)
Button:setPos(15,25)
Button:setText("Change Color")
Button:setIcon("icon16/arrow_refresh.png")
Button:setSize(110,40)
Button:create()
Button2 = dbutton(3,1)
Button2:setPos(15,70)
Button2:setText("Spawn Prop")
Button2:setIcon("icon16/package_add.png")
Button2:setSize(110,40)
Button2:create()
}
event chat(Player:entity, Message:string, Team:number) {
if(Message == "menu") {
Main:setVisible(!Main:isVisible(Player))
Main:modify(array(Player))
}
}
#this get's triggered when any player interacts with an vgui element (presses a button/clicking on a list/etc.)
event vguiClk(Player:entity, ID:number, Values:table) {
switch(ID) { #switch-case statement to differenciate from different elements
case 2,
Main:setColor(randvec(0,255))
Main:modify(array(Player))
break
case 3,
local AimPos = Player:aimPos()
propSpawn("models/props_junk/cardboard_box001a.mdl",AimPos + vec(0,0,50),0)
break
}
}(Obsolete) Old way
@persist Main:dframe [Button Button2]:dbutton
if(first()) {
runOnChat(1)
runOnVgui(1) #use runOnVgui to execute the E2 when an interaction with the a vgui element happens
vguiDefaultPlayers(players()) #add all players to the default list
Main = dframe(1)
Main:setTitle("Menu")
Main:setPos(60,300)
Main:setColor(255,120,120)
Main:setDeleteOnClose(0) #only hides the panel when closing it instead of deleting it
Main:setVisible(0)
Main:makePopup()
Main:enableKeyboardInput(0) #disables the keyboard input, you can still move but your mouse is locked on the panel
#needs to be called after :makePopup()
Main:create()
Button = dbutton(2,1)
Button:setPos(15,25)
Button:setText("Change Color")
Button:setIcon("icon16/arrow_refresh.png")
Button:setSize(110,40)
Button:create()
Button2 = dbutton(3,1)
Button2:setPos(15,70)
Button2:setText("Spawn Prop")
Button2:setIcon("icon16/package_add.png")
Button2:setSize(110,40)
Button2:create()
}
if(chatClk()) {
if(lastSaid() == "menu") {
Main:setVisible(!Main:isVisible(lastSpoke()))
Main:modify(array(lastSpoke()))
}
}
#this get's triggered when any player interacts with an vgui element (presses a button/clicking on a list/etc.)
if(vguiClk()) {
local ID = vguiClkPanelID() #this gets the ID of the interacted element
switch(ID) { #switch-case statement to differenciate from different elements
case 2,
Main:setColor(randvec(0,255))
Main:modify(array(vguiClkPlayer()))
break
case 3,
local AimPos = vguiClkPlayer():aimPos()
propSpawn("models/props_junk/cardboard_box001a.mdl",AimPos + vec(0,0,50),0)
break
}
}@name VGUI Vehicle Link
@persist Frame:dframe EdName:dtextentry BtnPrint:dbutton
@strict
if(first()) {
# HowTo: Spawn this e2 onto a vehicle entity and sit down
Frame = dframe(1)
Frame:setVisible(0) # hides initial panel
# link the frame to the vehicle, so it will automatically show/hide when someone sits down
Frame:linkToVehicle(entity():isWeldedTo())
Frame:center()
Frame:makePopup()
# disable keyboard focus so you can still leave the seat
Frame:enableKeyboardInput(0)
Frame:showCloseButton(0)
Frame:create()
EdName = dtextentry(2, Frame)
EdName:setPos(20,50)
EdName:create()
BtnPrint = dbutton(3, Frame)
BtnPrint:setPos(20,80)
BtnPrint:setText("Print")
BtnPrint:create()
}
event vguiClk(Player:entity, ID:number, Values:table) {
if(ID == BtnPrint:id()) {
print(EdName:getText(Player))
}
}