1. Post #1
    quincy18's Avatar
    September 2007
    1,018 Posts
    Is there anyway a swep can call a function from a gamemode like I write a function with some checking inside and the swep calls it.

  2. Post #2
    cloudwolf's Avatar
    February 2007
    50 Posts
    More information is necessary. Client side can call client side or shared Lua. Server side can call server side and shared Lua. What are you exactly trying to call because depending on the situation you would have to use console commands/datastream which datastream is just a fancier way of using console commands.

  3. Post #3
    quincy18's Avatar
    September 2007
    1,018 Posts
    Its serverside, I'm trying to make some sort of harvesting swep.

  4. Post #4
    diaoyudao shi ZHONGGUO de
    Disseminate's Avatar
    December 2007
    4,094 Posts
    Yep, entirely possible. If you define a function in the serverside gamemode, you can call it like any other function in the SWEP, as long as the call is in the SWEP's init or shared.

  5. Post #5
    quincy18's Avatar
    September 2007
    1,018 Posts
    Thanks

  6. Post #6
    quincy18's Avatar
    September 2007
    1,018 Posts
    For some reason this does not work, anyone care to explain ?

    Swep : shared

    SWEP.Author				= "Xyro"
    SWEP.Contact			= ""
    SWEP.Purpose			= "Harvest"
    SWEP.Instructions		= "Left click to Harvest"
     
    SWEP.Spawnable			= false
    SWEP.AdminSpawnable		= true
     
    SWEP.ViewModel			= "models/weapons/v_crowbar.mdl"
    SWEP.WorldModel			= "models/weapons/w_crowbar.mdl"
    
    SWEP.Primary.ClipSize		= -1
    SWEP.Primary.DefaultClip	= -1
    SWEP.Primary.Automatic		= false
    SWEP.Primary.Ammo		= "none"
     
    SWEP.Secondary.ClipSize	= -1
    SWEP.Secondary.DefaultClip	= -1
    SWEP.Secondary.Automatic	= false
    SWEP.Secondary.Ammo		= "none"
     
    local ShootSound = Sound( "Weapon_Crowbar.Single" )
     
    /*---------------------------------------------------------
    	Reload does nothing
    ---------------------------------------------------------*/
    function SWEP:Reload()
    end
     
    /*---------------------------------------------------------
      Think does nothing
    ---------------------------------------------------------*/
    function SWEP:Think()	
    end
     
    /*---------------------------------------------------------
    	PrimaryAttack
    ---------------------------------------------------------*/
    function SWEP:PrimaryAttack()
    	
    	self.Weapon:EmitSound( ShootSound )
    	self.Owner:SetAnimation( PLAYER_ATTACK1 );
    	self.Weapon:SendWeaponAnim( ACT_VM_HITCENTER );
    	
    	local pos = self.Owner:GetShootPos()
    	local ang = self.Owner:GetAimVector()
    	local tracedata = {}
    	tracedata.start = pos
    	tracedata.endpos = pos+(ang*200)
    	tracedata.filter = self.Owner
    	local trace = util.TraceLine(tracedata)
    	if not trace.Hit then return end
    	
    	entity = trace.Entity
    	if not entity:IsValid() then return end 
    	
    	physicsObj = entity:GetPhysicsObject()
    	if not physicsObj:IsValid() then return end
    	
    	self:DoProcces(self.Owner, "harvest", entity)
    	
    end
     
    /*---------------------------------------------------------
    	SecondaryAttack
    ---------------------------------------------------------*/
    function SWEP:SecondaryAttack()
     
    end
    
    function SWEP:Initialize()
    
    end
    

    Gamemode init :

     function DoProcces(ply, procces, entity)
    
    	ply:Freeze(true)
    	Msg(procces)
    	
     end 
    

  7. Post #7
    Deroxidan's Avatar
    July 2009
    23 Posts
    In what directories did you place the files?

  8. Post #8
    quincy18's Avatar
    September 2007
    1,018 Posts
    garrysmod/gamemode/waterworld/entities/weapons weapon
    garrysmod/gamemode/waterworld for the gamemode

    oh lol I forget to include the error :

    weapons\harvesting_tools\shared.lua:62: '=' expected near 'end'

  9. Post #9
    Deroxidan's Avatar
    July 2009
    23 Posts
    Ah that will help.
    try adding a = sign before the end as the error does say that if you read it.
    or try remove the end.

  10. Post #10
    quincy18's Avatar
    September 2007
    1,018 Posts
    sorry for this post it seems that while testing I put a seccond file inside the garrymod/lua folder so it was trying to use a old file xD. But now a seccond question It gives this error


    weapons\harvesting_tools\shared.lua:60: attempt to call method 'DoProcces' (a nil value)

  11. Post #11
    Deroxidan's Avatar
    July 2009
    23 Posts
    I think its because you hav'nt coded it enough for it to understand what you want to do, the code you have on line 60 is meaningless

    The swep needs to know what harvesting is. So you must code it.

  12. Post #12
    quincy18's Avatar
    September 2007
    1,018 Posts
    Huh Im only trying to call a function from the sweps primary attack the code in the gamemode file is only a test :O.

    and the code on line 60 is supposed to call it, if not how do I call it.

  13. Post #13
    Devenger's Avatar
    February 2006
    235 Posts
    If it's a gamemode function, you're calling it from the wrong place. In the SWEP,

    self:DoProcces(self.Owner, "harvest", entity) --This will call the function DoProcces in the weapon's table. Examples of functions in the weapon's table: PrimaryAttack, Reload

    If you want to call a gamemode function that you made in the gamemode table (e.g. function GM: ...),

    GAMEMODE:DoProcces(self.Owner, "harvest", entity) --This will call the function DoProcces in the gamemode table.

  14. Post #14
    quincy18's Avatar
    September 2007
    1,018 Posts
    Thanks, I thought something like that but I couldn't find something like that on Gmod Wiki

  15. Post #15
    Deroxidan's Avatar
    July 2009
    23 Posts
    -snip-

  16. Post #16
    quincy18's Avatar
    September 2007
    1,018 Posts
    For some reason It still gives the same error

    weapons\harvesting_tools\shared.lua:60: attempt to call method 'DoProcces' (a nil value)

    Im using this :

    GAMEMODE:DoProcces(self.Owner, "harvest", entity)

    Do I need to declare what my gamemode is first ?
    Its derived from base

  17. Post #17
    Deroxidan's Avatar
    July 2009
    23 Posts
    Wait a minute :sigh:

    I believe you spelt DoProcess as DoProcces .

  18. Post #18
    Pass along
    commander204's Avatar
    May 2008
    4,065 Posts
    No instead just doing

    function DoProcces() 

    do

    function GM:DoProccess()

  19. Post #19
    quincy18's Avatar
    September 2007
    1,018 Posts
    It worked thanks !