• Multiple entity uses
    5 replies, posted
How would i make a entity be able to be used 3 times and every time i use it, it does something new, etc: press entity - gains 10 health presses again - takes health presses again - kills player. Any idea's?
[lua]function ENT:Initialize() -- Stuff self.Uses = 0 self.User = nil self.UseFunctions = { [1] = function( pl ) -- Stuff end, [2] = function( pl ) -- more stuff end, } end function ENT:Use( pl ) if pl:IsPlayer() then if pl == self.User then self.Uses = self.Uses + 1 else self.User = pl self.Uses = 1 end self.UseFunctions[ math.Clamp( self.Uses, 1, #self.UseFunctions ) ]( pl ) end end[/lua] That's how I'd do it. *note: It resets when a different player uses it.
Maybe this: [lua] function ENT:Use( pl ) local result = math.random(1,3) result(pl) end local function 1() -- stuff here end local function 2() -- stuff here end local function 3() -- stuff here end [/lua]
That won't work. For a start 1 is not a valid name for a function. You can use a number as an identifier for a function, but it has to be inside a table, like below. Secondly he didn't ask for it to do something at random, he asked for it to do something different. To me this implies it does diffrent actions in order. [lua]local actions = { [0] = function(self, activator, caller) -- the first action -- do something here end; [1] = function(self, activator, caller) -- the second action -- do something else here end; [2] = function(self, activator, caller) -- the third action -- do something else entirely here end } function ENT:Initialize() -- whatever else in here self.UseState = 0 self:SetUseType(USE_SIMPLE) end function ENT:Use(activator, caller) actions[self.UseState](self, activator, caller) self.UseState = (self.UseState + 1) % 3 end[/lua] After it does the third action it resets to the first action.
Perfect fishface! But how do i get it so on the third action it does self:Remove(); instead of resetting? [EDIT] Would i just add another function line like so?: [lua] [3] = function(self, activator, caller) self:Remove() end [/lua]
[QUOTE=101kl;19983314]Perfect fishface! But how do i get it so on the third action it does self:Remove(); instead of resetting? [EDIT] Would i just add another function line like so?: [lua] [3] = function(self, activator, caller) self:Remove() end[/lua] [/QUOTE] Pretty much, but for it to reach that yoou'd have to get rid of the %3 bit, otherwise it'd never reach stage 3. [editline]08:39AM[/editline] Also the bit with self:SetUseType(USE_SIMPLE) should be self:SetUseType(SIMPLE_USE). I was doing that bit from memory. The simple use part is so that it only runs the Use hook when you start pressing use on the entity, not every think while you're pressing use on it.
Sorry, you need to Log In to post a reply to this thread.