Currently, I am developing small-time Lua for a small community (entities and Expression2 extensions). I decided that we could use a runOnUse and runOnCollide function set (along with things with them, like useClk, or collideEnt(), etc).
I am mainly doing this to get the experience and to learn, so do not redirect me to existing mods/extensions unless it is an example of a working piece (no "Username777 made this mod before, use that instead").
I am having some issues with these hooks, before I last edited them, they would run on Use or Collide even with the flag off, so I made the "active" variables local to each chip...or at least tried.
Now neither of the hooks run. No errors, so I can assume that the 'active' variables are just not getting put into the chips at all.
I am going to open myself up to criticism here and post my code, so here goes:
Code:
//-----------------Use Functions start Here----------------------------------------------------
--First the functionality
--local useactive = 0;
local user = nil;
local runByUse = 0;
--local nextUseExec = 0;
function ENT:Use( activator, caller )
if self.CanUseHook == nil then
self.CanUseHook = 1;
else
self.Entity:SetUseType(SIMPLE_USE)
if not self.useactive then return end
if not ValidEntity(activator) then return end
--Prevent spamming of executions
if CurTime() > self.nextUseExec || self.nextUseExec == nil then
runByUse = 1;
user = activator;
self.Entity:Execute()
runByUse = 0;
user = nil;
self.nextUseExec = CurTime()+0.05
end
end
end
--And now for the E2 registrations
e2function void runOnUse( activate )
self.useactive = activate;
end
e2function number useClk()
return runByUse
end
e2function number useClk(entity ply)
if not ValidEntity(ply) then return 0 end
if user == ply && runByUse == 1 then
return 1
else
return 0
end
end
e2function entity usedBy()
return user
end
//--------End Use Functions----]]
//-----------------Collide Functions start Here----------------------------------------------------
--First the Functionality
--local collideActive = 0;
--local collisionSensitivity = 50;
--local collisionRecheckTime = 0.1;
local collisionData = {};
local runByCollision = 0;
function ENT:PhysicsCollide( data, physobj )
if not self.collideActive then return end
if data.Speed >= self.collisionSensitivity && data.DeltaTime > self.collisionRecheckTime then
collisionData = data;
runByCollision = 1;
self.Entity:Execute()
collisionData = {};
runByCollision = 0;
end
end
--And now for the E2 registrations
--//Initial setup functions
e2function void runOnCollide( physactive )
self.collideActive = physactive;
end
e2function number collideClk()
return runByCollision
end
e2function void collisionSensitivity( sensitivity )
self.collisionSensitivity = math.Clamp(sensitivity, 15, math.huge);
end
e2function void collisionRecheckDelay( delay )
self.collisionRecheckTime = math.Clamp(delay, 0.1, math.huge);
end
--//Data returns
e2function vector collidePos()
return collisionData.HitPos
end
e2function entity collideEnt()
return collisionData.HitEntity
end
e2function vector collideSpeed()
return collisionData.OurOldVelocity
end
e2function vector collideEntSpeed()
return collisionData.TheirOldVelocity
end
e2function number collideLast()
return collisionData.DeltaTime
end
e2function number collideForce()
return collisionData.Speed
end
e2function vector collideNormal()
return collisionData.HitNormal
end
//--------End Collide Functions----]] First off, let me just say that if anyone would like to help me clean this up, feel free to do so. I don't really like calling those ENT functions like that, but it's the only way I could get them working in the first place.
The sooner I get help on this, the sooner I can get on with my other projects for that community. A community with toys is a happy community!
Bookmarks