• meta tables, player.
    13 replies, posted
Basically I have a player_extension lua file in my gamemode folder that is the following [code] local meta = FindMetaTable( "Player" ) if (!meta) then return end function meta:GivePower( weaponstring ) print("Powers!") self:Give(weaponstring) end [/code] and then I try to Access the function in an entity's init file with the following [code] -- ENT:DoPowers - This is the function that do the actual powerup code, there is also code in sent_melon_base -- function ENT:DoPowers(Ent) --Ent is the ball... local ply -- We Want to do stuff to the Player/Ball here if (Ent:GetOwner():IsValid()) then ply = Ent:GetOwner() print("player valid") end -- Balloon(1) -- if (self.PowerupType == 1) then ply:PrintMessage(4, "Shoot At Other Players to attach balloons to them.") Ent:GetOwner():GivePower("weapon_crowbar") end end [/code] Problem is that the function GivePowers returns nil in the console, Am i doing something wrong here?
Are you including your player_extension.lua file?
1. Are you doing [code]include( "player_extension.lua" )[/code] in your init.lua file? 2. Does it actually print "player valid"? If you didn't manually set the entity's owner, it doesn't do it for you. It could error if Ent:GetOwner() isn't valid. 3. Also, did you define "Ent" anywhere? The correct syntax would be "self.Entity:GetOwner()". *edit* 1/3 ninja :ninja:
Remove the local infront of your meta!
[QUOTE=commander204;19218289]Remove the local infront of your meta![/QUOTE] I don't think that's the problem.
[QUOTE=commander204;19218289]Remove the local infront of your meta![/QUOTE] Why would you want him to do that?
To get me mad. :smile:
[QUOTE=Nerdeboy;19218174]Are you including your player_extension.lua file?[/QUOTE] Ah Silly me, I used AddCSLuaFile instead of include, thanks much. But another problem is that the actual player:Give("weapon_crowbar") only works a quarter of the time. to recap I have this: [code] function ENT:StartTouch(Ent) -- Only Balls are allowed to Touch! if (!Ent:GetClass() == "sent_ball_base") then return end -- Do the powerup stuff self:DoPowers(Ent) -- Remove the powerup self.Entity:Remove() -- Debug print("Powerup Touched") end function ENT:DoPowers(Ent) --Ent is the ball... local ply -- We Want to do stuff to the Player/Ball here if (Ent:GetOwner():IsValid()) then ply = Ent:GetOwner() print("player valid") end -- Balloon(1) -- if (self.PowerupType == 1) then ply:PrintMessage(4, "Shoot At Other Players to attach balloons to them.") ply:GivePower("weapon_crowbar") -- Haste -- elseif (self.PowerupType == "Haste") then Ent.HasteTimer = CurTime() + GAMEMODE.Powerups.Haste.Duration end end [/code] and then the meta function: [code] function meta:GivePower( weaponstring ) print("Powers!") self:Give(weaponstring) end [/code] Ive tried putting the [code] self:Give(weaponstring) [/code] in different places including directly in the Ent:starttouch() function. the results are only picking up the weapon sometimes.
[QUOTE=MeltingPlastic;19218365] the results are only picking up the weapon sometimes.[/QUOTE] Does the weapon spawn at all?
[QUOTE=ralle105;19218736]Does the weapon spawn at all?[/QUOTE] yeah, thats whats wierd, only sometimes. I basically have to run over at least 10 powerups for it to give me a weapon. And Im very sure it comes down to ply:Give().
Get an ESP, set it to display "weapon_crowbar" and see if it's under the map. Or just do: [lua] lua_run for k,v in pairs(ents.FindByClass"weapon_crowbar")do player.GetAll()[1]:SetPos(v:GetPos())end [/lua] If you teleport under the map (and/or get a crowbar), then the crowbar [i]is[/i] being made... just in the wrong place. (I swear Garry fixed the Give function... but who knows) Make sure you don't have a [url=http://wiki.garrysmod.com/?title=Gamemode.PlayerCanPickupWeapon]PlayerCanPickupWeapon[/url] hook that's stopping it from being given. And, are you using a non-default hull size for the players?
[QUOTE=Deco Da Man;19222261] (I swear Garry fixed the Give function... but who knows) [/QUOTE] He raised the height the weapon gets created at (rather than it being at your feet) If you're completely stuck, it won't work (Unless he improved it again without putting it in the changelog)
[QUOTE=Deco Da Man;19222261]Get an ESP, set it to display "weapon_crowbar" and see if it's under the map. Or just do: [lua] lua_run for k,v in pairs(ents.FindByClass"weapon_crowbar")do player.GetAll()[1]:SetPos(v:GetPos())end [/lua] If you teleport under the map (and/or get a crowbar), then the crowbar [i]is[/i] being made... just in the wrong place. (I swear Garry fixed the Give function... but who knows) Make sure you don't have a [url=http://wiki.garrysmod.com/?title=Gamemode.PlayerCanPickupWeapon]PlayerCanPickupWeapon[/url] hook that's stopping it from being given. And, are you using a non-default hull size for the players?[/QUOTE] Ahh, This Would Explain it. My player is controlling a rolling physics entity, the invisible player is constantly being repositioned to the entity. So the chances of the player actually picking up the weapon is rather low. How would you suggest getting around this? Other than that the hull is normal.
Does the player always need to be positioned inside the rolling physics entity? Maybe just use the CalcView hook clientside to position the camera at the physics entity and leave the player somewhere in the map, invisible frozen and nocollided.
Sorry, you need to Log In to post a reply to this thread.