Garry's Mod Wiki

Revision Difference

Editable_Entities#551427

<cat>Dev.Lua</cat> Making an entity editable is a lot easier than you would think. To edit an entity in realtime in sandbox mode (or your own gamemode if you have it enabled), you simply hold C (context menu) and right click on the entity. A submenu appears, and choosing properties will show a window like the one below. <image src="entity_editor_example.jpg" alt="center"/> # Mark it as editable The first thing to do is to mark it as editable. ``` ENT.Editable = true ``` Simple enough? # Data Tables The rest of the magic happens in <page text="ENT:SetupDataTables">ENTITY:SetupDataTables</page> - where you're hopefully already defining your network variables for you entity. It probably looks something like this right now. ``` function ENT:SetupDataTables() self:NetworkVar( "Float", 0, "FogStart" ) self:NetworkVar( "Float", 1, "FogEnd" ) self:NetworkVar( "Float", 2, "Density" ) self:NetworkVar( "Vector", 0, "FogColor" ) end ``` So all you need to do is add options to the end of each of these. ``` function ENT:SetupDataTables() self:NetworkVar( "Float", 0, "FogStart", { KeyName = "fogstart", Edit = { type = "Float", order = 1, min = 0, max = 1000000 } } ) self:NetworkVar( "Float", 1, "FogEnd", { KeyName = "fogend", Edit = { type = "Float", order = 2, min = 0, max = 1000000 } } ) self:NetworkVar( "Float", 2, "Density", { KeyName = "density", Edit = { type = "Float", order = 3, min = 0, max = 1 } } ) self:NetworkVar( "Vector", 0, "FogColor", { KeyName = "fogcolor", Edit = { type = "VectorColor", order = 4 } } ) end ``` This might seem a bit complicated, but it's simple really. We have a table with: ## KeyName This defines the key-value name of this variable. This is the standard way in Source to handle networked entity variables, and is required for editing to work. As a bonus, it will also allow map-makers to create maps containing your entity and set these variables directly from Hammer I/O. ## Edit This is a subtable describing how to edit this variable. The actual contents of this table differs with each type of variable, and are described below. # Types These variables are shared between all "edit types": * `title` - a string - the title to show to the left of the control. If unset it will default to the control name. This must be unique to each item. * `order` - a number - will display the lowest orders first. * `category` - a string - controls are grouped by category (defaults to `General`) * `readonly` - a boolean initializing the contents as read only value, which can be copied, but not modified. ## Generic A simple text box. * `waitforenter` - boolean, whether the value should be updated in real-time as the user types, or only when they press enter. (default: `false`) ## Boolean This type is a simple checkbox. There's no other settings in here apart from the shared values. ## Float This provides the client with a number slider. * `min` - The minimum value (defaults to `0`) * `max` - The maximum value (defaults to `1`) ## Int Identical to **Float** but only lets you choose integers. ## VectorColor This provides the client with a color selector - which is represented by a Vector. Each color component's range is between 0-1. ## Combo This is a ComboBox. * `text` - the default selection. (default: `Select...`) * `values` - a key/value table of available options in the ComboBox. Keys are the identifier string, values are the associated actual values. Required. ⤶ # Hooks⤶ ⤶ ## CanEditVariable⤶ ⤶ <page text="CanEditVariable">GM:CanEditVariable</page> is called serverside before a variable has been edited. Here you can add checks for its validity and stop the edit if needed.⤶ ⤶ ## VariableEdited⤶ ⤶ <page text="VariableEdited">GM:VariableEdited</page> is called serverside after a variable of an Entity has been edited.⤶ # Advanced The property editors are modular, and are defined in `lua/vgui/` - each starting with `prop_*`. Creating your own should be quite simple by looking at these files. ⤶ ⤶