+ Reply to Thread
Results 1 to 7 of 7

Thread: Custom runOn Hook Troubles.

  1. #1
    Wire Noob Steeveeo's Avatar
    Join Date
    Mar 2009
    Posts
    25

    Default Custom runOn Hook Troubles.

    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!

  2. #2
    No u Divran's Avatar
    Join Date
    Jul 2008
    Location
    Sweden
    Posts
    4,582

    Default Re: Custom runOn Hook Troubles.

    Quote Originally Posted by Steeveeo
    Code:
    function ENT:Use( activator, caller )
    I don't see you defining "ENT" anywhere..
    SVN Tutorial
    My SVN:
    Code:
    http://divranspack.googlecode.com/svn/trunk/%20divranspack/
    Get dropbox and get 250 MB extra space: Dropbox

  3. #3
    Wire Noob Steeveeo's Avatar
    Join Date
    Mar 2009
    Posts
    25

    Default Re: Custom runOn Hook Troubles.

    Quote Originally Posted by Divran View Post
    I don't see you defining "ENT" anywhere..
    I found that the E2 extloader calls include(""), so that is basically running inside the chip Ent as a normal ENT:function. As said, it worked before I made the active variables set under self.active (instead of local active, causing all the chips to run even without the hook set to 1).

  4. #4
    Wire Noob Steeveeo's Avatar
    Join Date
    Mar 2009
    Posts
    25

    Default Re: Custom runOn Hook Troubles.

    It's been a bit more than a day, and my week off is drawing to a close before I have to start up college classes again, does ANYONE have an idea of how to get this to work correctly?

  5. #5
    Wirererer bobthe2lol's Avatar
    Join Date
    Aug 2007
    Location
    US,NY
    Posts
    309

    Default Re: Custom runOn Hook Troubles.

    In concept:
    Have a table of chip entities (self.entity) that want to be executed by use, or what ever. Now, when ever this is triggered, loop through those, trigger the ones you want, and set a variable to 1. when your done triggering them, set it to 0. have a function <whateveryouwanthere>Clk() that returns said variable. Thats it in concept, and i am too busy to do it in reality.

  6. #6
    Wire Noob Steeveeo's Avatar
    Join Date
    Mar 2009
    Posts
    25

    Default Re: Custom runOn Hook Troubles.

    But wouldn't that run ALL the chips with runOnClk set on? I just want the used chip to run, which it does, but I can't seem to turn the use hook OFF.

  7. #7
    Wirererer bobthe2lol's Avatar
    Join Date
    Aug 2007
    Location
    US,NY
    Posts
    309

    Default Re: Custom runOn Hook Troubles.

    I'm pretty sure you cant use ENT:use() because that code is not like code in init.lua etc. You'd have to do something along the lines of self.entity:use(). But first. Like i said. Make a table of every chip what wants to be triggered when use it hit on it. Then, maby do it this way, or a much more efficent way, loop through all players, see if their aimentity is any one of the chips that want to be triggered, and if their use key is down. If so, use Table[aiment]:Exicute() or w/e the function is. When they dont want to be triggered anymore, just do Table[ent]=nil and or for k,v in pairs(Table) do if(v == enttoremove) then table.Remove(Table,k) end end

+ Reply to Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
proceed-collector
proceed-collector
proceed-collector
proceed-collector
linguistic-parrots
linguistic-parrots
linguistic-parrots
linguistic-parrots