Revision Difference
undo.Create#546241
<function name="Create" parent="undo" type="libraryfunc">
<description>Begins a new undo entry</description>
<realm>Server</realm>
<args>
<arg name="name" type="string">Name of the undo message to show to players</arg>
</args>
</function>
<example>
<description>This example creates a prop_physics, and adds it to Player's undo list.</description>
<code>
prop = ents.Create("prop_physics")
prop:SetModel("models/props_junk/wood_crate001a.mdl")
prop:Spawn()
undo.Create("prop")
undo.AddEntity(prop)
undo.SetPlayer(Player)
undo.Finish()
</code>
</example>
<example>
<description>When you need to override the undo messages for a spawnable entity inside [`ENT:SpawnFunction(user, trace)`](https://wiki.facepunch.com/gmod/ENTITY:SpawnFunction) and apply a custom text, calling `undo.Create()` will not work and will in fact double the undo entries in the list pointing to the same entity created. Otherwise you will see `Scripted Entity (entity_class)` when you create it. This is defined in [commands.lua](https://github.com/Facepunch/garrysmod/blob/master/garrysmod/gamemodes/sandbox/gamemode/commands.lua) and you must return `nil`, then add the remaining hooks and function calls to account for the missing stuff. This is from the `LaserStool`:</description>⤶
<description>When you need to override the undo messages for a spawnable entity inside [`ENT:SpawnFunction(user, trace)`](https://wiki.facepunch.com/gmod/ENTITY:SpawnFunction) and apply a custom text, calling `undo.Create()` will not work and will in fact double the undo entries in the list pointing to the same entity created. Otherwise you will see `Scripted Entity (entity_class)` when you create it. This is defined in [commands.lua](https://github.com/Facepunch/garrysmod/blob/master/garrysmod/gamemodes/sandbox/gamemode/commands.lua) and you must return `nil`, then add the remaining hooks and function calls to account for the missing stuff:⤶
⤶
</description>⤶
<code>
local laser = ents.Create("gmod_laser")
if(laser and laser:IsValid()) then -- Validates laser entity ( class : gmod_laser )
⤶
-- This part contains custom library calls and it is not importnat⤶
⤶
-- This will update your custom undo message by returning `nil`⤶
-- Otherwise the undo message will be `Scripted Entity (gmod_laser)`⤶
undo.Create("Laser emitter ["..laser:EntIndex().."]")⤶
undo.AddEntity(laser) -- Add out custom laser⤶
if(weld) then undo.AddEntity(weld) end -- Add the constraint ⤶
local prop = ents.Create("prop_physics")
if(prop and prop:IsValid()) then -- Validates prop entity ( class : prop_physics )
-- This will update your custom undo message by returning `nil`⤶
-- Otherwise the undo message will be `Scripted Entity (prop_physics)`⤶
undo.Create("Test ["..prop:EntIndex().."]")⤶
undo.AddEntity(prop) -- Add out custom prop⤶
undo.SetPlayer(user) -- Apply undo for the player
undo.Finish() -- Make your custom undo
gamemode.Call("PlayerSpawnedSENT", user, laser) -- Account for missing hook
user:AddCount("sents", laser) -- Add to the SENTs count⤶
user:AddCount("laseremitters", laser) -- Add count ( ownership )⤶
user:AddCleanup("sents", laser) -- Add item to the sents cleanup
user:AddCleanup("laseremitters", laser) -- Add item to the cleanup
gamemode.Call("PlayerSpawnedSENT", user, prop) -- Account for missing hook
user:AddCount("sents", prop) -- Add to the SENTs count ( ownership )⤶
user:AddCount("my_props", prop) -- Add count to our personal count⤶
user:AddCleanup("sents", prop) -- Add item to the sents cleanup
user:AddCleanup("my_props", prop) -- Add item to the cleanup
end -- Return nil, so the game will not create undo entry
</code>
</example>