I've made this entity that is supposed to slide towards it's kin and link up into a mesh.
This works fine, apart from the game occasionally crashes when they link.
Is this gmod being shit or have I done something wrong?
[lua]ENT.Type = "anim"
ENT.Base = "base_gmodentity"
ENT.PrintName = "Dynamic Mesh Point"
ENT.Author = "Lexi"
ENT.Contact = "mwaness@gmail.com"
ENT.Purpose = ""
ENT.Instructions = ""
ENT.Information = ""
ENT.Category = "Util"
ENT.Spawnable = true
ENT.AdminSpawnable = true
ENT.RenderGroup = RENDERGROUP_TRANSLUCENT;
function math.DecimalPlaces(numb,places)
return math.Round(numb*10^places)/10^places
end
if SERVER then
AddCSLuaFile"shared.lua"
AddCSLuaFile"cl_init.lua"
local meshes = {}
local targetpoint = nil
function ENT:SpawnFunction( ply, tr )
if not tr.Hit then return end
local ent = ents.Create("sent_mesh")
ent:SetPos( tr.HitPos + tr.HitNormal * 50--
)
ent:Spawn()
ent:Activate()
table.insert(meshes,ent)
ent._corners = {}
if not ValidEntity(targetpoint) then
targetpoint = ent
ent.targetpoint = ent:GetPos()
elseif not ValidEntity(targetpoint._corners.n) then
targetpoint._corners.n = ent
ent._corners.s = targetpoint
ent.target = targetpoint
ent.targetpoint = Vector(100,0,0)
elseif not ValidEntity(targetpoint._corners.e) then
targetpoint._corners.e = ent
ent._corners.w = targetpoint
ent.target = targetpoint
ent.targetpoint = Vector(0,100,0)
elseif not ValidEntity(targetpoint._corners.s) then
targetpoint._corners.s = ent
ent._corners.n = targetpoint
ent.target = targetpoint
ent.targetpoint = Vector(-100,0,0)
elseif not ValidEntity(targetpoint._corners.w) then
targetpoint._corners.w = ent
ent._corners.e = targetpoint
ent.target = targetpoint
ent.targetpoint = Vector(0,-100,0)
else
targetpoint = targetpoint._corners.w
ent.target = targetpoint
ent._corners.e = targetpoint
ent.targetpoint = Vector(0,-100,0)
end
local phys = ent:GetPhysicsObject()
if (phys:IsValid()) then
phys:EnableMotion(true)
phys:Wake()
ent:ToggleMotion()
else
ent:Remove()
error"Mesh point spawned with no physics!"
end
return ent
end
function ENT:Initialize()
self.Entity:SetModel("models//props_junk/Rock001a.mdl")
self.Entity:PhysicsInit( SOLID_VPHYSICS)
self.Entity:SetMoveType( MOVETYPE_VPHYSICS)
self.Entity:SetSolid( SOLID_VPHYSICS)
self.Entity:SetUseType(SIMPLE_USE)
self.Entity:DrawShadow(false)
self.MovinTooFast = false
self.ShadowParams = {}
end
function ENT:Use(activator,caller)
if activator:IsPlayer() then
self:ToggleMotion()
end
end
function ENT:ToggleMotion()
local phys = self:GetPhysicsObject()
if not IsValid(phys) then return end
if self.MovinTooFast then
self:StopMotionController()
phys:EnableGravity(false)
phys:Wake()
phys:SetDamping(500,0)
phys:EnableDrag(true)
self.MovinTooFast = nil
else
self:StartMotionController()
phys:EnableGravity(false)
phys:SetDamping(0,0)
phys:EnableDrag(false)
phys:Wake()
self.MovinTooFast = true
end
end
function ENT:GainTarget()
end
local vector0 = Vector(0,0,0)
function ENT:PhysicsSimulate(phys,deltatime)
phys:Wake()
local pos = self:GetPos()
if not (self.targetpoint and ValidEntity(self.target))then
self:ToggleMotion()
return
end
local target = self.target:GetPos() + self.targetpoint
if math.floor(pos.x) == math.floor(target.x)
and math.floor(pos.y) == math.floor(target.y)
and math.floor(pos.z) == math.floor(target.z) then
self:SetPos(target)
self:ToggleMotion()
if ValidEntity(self.target) then
constraint.Rope(self.Entity,self.target,0,0,vector0,vector0,self:GetPos():Distance(self.target:GetPos()),0,0,10,"cable/redlaser",true)
end
return
elseif self.PrevPos == pos then
self:ToggleMotion()
print"ERROR ENTITY BLOCKED"
return
else
self.PrevPos = pos
end
print(pos,target)
self.ShadowParams.secondstoarrive = 1 // How long it takes to move to pos and rotate accordingly - only if it _could_ move as fast as it want - damping and maxspeed/angular will make this invalid
self.ShadowParams.pos = target // Where you want to move to
self.ShadowParams.angle = Angle( 0, 0, 0 ) // Angle you want to move to
self.ShadowParams.maxangular = 5000 //What should be the maximal angular force applied
self.ShadowParams.maxangulardamp = 10000 // At which force/speed should it start damping the rotation
self.ShadowParams.maxspeed = 1000000 // Maximal linear force applied
self.ShadowParams.maxspeeddamp = 10000// Maximal linear force/speed before damping
self.ShadowParams.dampfactor = 0.2 // The percentage it should damp the linear/angular force if it reachs it's max ammount
self.ShadowParams.teleportdistance = 0 // If it's further away than this it'll teleport (Set to 0 to not teleport)
self.ShadowParams.deltatime = deltatime // The deltatime it should use - just use the PhysicsSimulate one
phys:ComputeShadowControl(self.ShadowParams)
end
return
end
function ENT:Draw()
self.Entity:DrawModel()
end[/lua]
I have a similar problem with constraint.Weld. I will use it many times in the same way, and it generally works. However, once in a while, a weld will fail - it just doesn't do anything. Any further use of constraint.Weld will crash the game from there on out unless the map changes. I can't intentionally replicate it, though - it just happens once in a while.
That code now crashes me on the first rope, even on a fresh gmod install.
The instruction at "0x11177b4f" referenced memory at "0x0e8a0454". The memory could not be "read".
[QUOTE=Lexic;17192690]That code now crashes me on the first rope, even on a fresh gmod install.
The instruction at "0x11177b4f" referenced memory at "0x0e8a0454". The memory could not be "read".[/QUOTE]
Try adding a timer which creates the rope a short moment later. If I remember correctly, you should never remove an entity, constrain it, or perform any operation on it inside a physics hook (PhysicsCollide, etc...), else the engine considers it as a violation and crashes.
[QUOTE=_Kilburn;17192778]Try adding a timer which creates the rope a short moment later. If I remember correctly, you should never remove an entity, constrain it, or perform any operation on it inside a physics hook (PhysicsCollide, etc...), else the engine considers it as a violation and crashes.[/QUOTE]
Ah, that fixed it. Thanks.
Sorry, you need to Log In to post a reply to this thread.