Stack script

From GMod Wiki

Jump to: navigation, search


General: Stack Script

Yarin Kaul Icon Lua48.png
Description:This script is an example how you can play with entities. By typing stack in your console, the prop you are looking at will copy itself. For example, stack 10 0 0 128 will stack the object you are looking at 10 times, 128 points from the last in a upwards direction.
Original Author(s):Haza
 
/*---------------------------------------------------------
	// Stack a prop_physics. Possibly other things but prop_physics just to be safe.
---------------------------------------------------------*/
function CCStack( player, command, arguments )
 
        // Check that all the arguments are correct.
	if ( arguments[1] == nil || arguments[2] == nil || arguments[3] == nil || arguments[4] == nil) then 
	player:PrintMessage( HUD_PRINTCONSOLE , "stack [times] [x] [y] [z]" )  
	return 
	end
 
	// Get the traceline of what the player is looking at.
	local tr = utilx.GetPlayerTrace( player, player:GetCursorAimVector() )
	local trace = util.TraceLine( tr )
 
	// Put our arguments into variables.
	local number = arguments[1]
	local x = arguments[2]
	local y = arguments[3]
	local z = arguments[4]
 
	// Make sure the trace actually hit somthing. And make sure its not a player and its a prop.
        if (!trace.Hit) then return end
	if (!trace.HitNonWorld) then return end
	if (trace.Entity:IsPlayer()) then return end
	if (!trace.Entity:IsValid()) then return end
	if (trace.Entity:GetClass() != "prop_physics") then return end
 
	// Put the entity in a varible to make programming faster.
	local ent = trace.Entity
 
	// Put the position of the Entity we are looking at in a var.
	local currvector = ent:GetPos()
 
	// Freeze it where it is, if not already done so.
	ent:GetPhysicsObject():EnableMotion( false )
 
	// Loop, up to the number of times specified.
	for i=1, number, 1 do
		// Add the difference vector to the curr vector. Note: currvector + Vector( x, y, z ) did not work. 
		currvector.x = currvector.x + x
		currvector.y = currvector.y + y
		currvector.z = currvector.z + z
 
		// Create our new entity.
		newent = ents.Create("prop_physics")				// Create a prop_physics.
			newent:SetModel(ent:GetModel())				// Set the model.
			newent:SetColor(ent:GetColor())				// Set the same color.
			newent:SetPos(currvector)				// Set the incrementing position.
			newent:SetAngles(ent:GetAngles()) 			// Set them to look the same way.
			newent:Spawn() 						// Spawn it.
			newent:GetPhysicsObject():EnableMotion( false ) 	// Freeze it.
 
		player:AddCount( "props", newent )				// Add it to the players prop count.
	end
end
 
concommand.Add( "stack", CCStack ) 		// Create the concommand to match the function CCStack.
 
 

With angle rotation

Also if you want the user to be able to change the angle as well, do the following to the script.

Under

local ent = trace.Entity

Place

local anglevector = trace.Entity:GetAngles()
if(arguments[5] == nil || arguments[6] == nil || arguments[7] == nil) then
     local ax = arguments[5]
     local ay = arguments[6]
     local az = arguments[7]
else
     local ax = 0
     local ay = 0
     local az = 0
end


Under

currvector.z = currvector.z + z

Place

 
anglevector.x = anglevector.x + ax
anglevector.y = anglevector.y + ay
anglevector.z = anglevector.z + az
 


Change

newent:SetAngles(ent:GetAngles())

To

newent:SetAngles(anglevector)


Change

player:PrintMessage( HUD_PRINTCONSOLE , "stack [times] [x] [y] [z]" ) 

To

player:PrintMessage( HUD_PRINTCONSOLE , "stack times x y z [ax ay az]" )


For example the user would now type, stack 10 0 0 128 and the same would happen but if you did stack 10 0 0 128 0 0 10 it will tilt 10 points upwards.

Personal tools
Namespaces
Variants
Actions
Navigation
Lua Scripting
Functions
Hooks
Toolbox