I have a few general questions on learning LUA in Gmod. I've been able to find a lot of resources and have programming experience (mainly in a language that seems to evoke nausea around here but also a bit of C++ and Ruby), so I'm mainly looking to ensure that my findings are good ones and not outdated rather than a handout, so to speak.
First one is rather specific - is the page "Why your scripts are now broken" a reasonably complete account of things I should look out for if I use resources or material from version 12? ([url]https://docs.google.com/document/d/1khSuIYrAMkqXu7wlH5YRJNwz6hOH6Xqi5lqBhE3x6gA/edit[/url]) It doesn't have to be 100% exhaustive but I mainly want to avoid big pitfalls.
Secondly, would it be better to base a new gamemode off of an existing one or create small addons to get started? So far, my learning has been experimental editing of existing things, minor fixes of broken legacy addons I've found or used in the past, and lazily dumping random tidbits in lua/autorun (this feels extremely lazy and I hate doing this). Addons, being smaller in nature, are likely the more common approach taken and my main concern is any (possibly non-existent) limitations I might run across in doing so. Hooks seem like the most obvious one, but I suppose those limitations won't be relevant for a while. I'm tempted to simply extract the sandbox gamemode to have a version to mess around with, but this may be overkill and counter-productive for learning.
Thoughts/bricks welcome.
I'm pretty desperate when it comes to [url="http://facepunch.com/showthread.php?t=1231983"]this[/url] right now. I just don't get it! It doesn't make any sense!
Repeat Question: if i was to limit how many times someone can use a command how can i do it?
[QUOTE=gemmono2;38783929]Repeat Question: if i was to limit how many times someone can use a command how can i do it?[/QUOTE]
Do it the lazy way. Set a variable on the player and use that to track the amount. Just increment/decrement as you see fit and check it every time your command is run.
Forgive me if this has been answered already, but why the frack don't any particle effects work with ParticleEffect() or ParticleEffectAttach() anymore? Even if I precache them the game says that they're invalid, even when I know they are valid particles. And yes, I have the appropriate games mounted.
[QUOTE=LostInTheWired;38783959]Do it the lazy way. Set a variable on the player and use that to track the amount. Just increment/decrement as you see fit and check it every time your command is run.[/QUOTE]
and how would one go about this? its 5am dont wanna go looking
[lua]
concommand.Add( "5times", function( ply, ... )
if( IsValid( ply ) )
if( !ply:GetTable().tran )then
ply:GetTable().tran = 1;
else
ply:GetTable().tran = ply:GetTable().tran + 1;
end
if( ply:GetTable().tran > 5 )then
return;
end
end
ply:ChatPrint( "success" );
end );
[/lua]
Pseudo
You can omit GetTable() if you want
I'm pretty sure it's deprecated anyway.
[QUOTE=JetBoom;38786879]I'm pretty sure it's deprecated anyway.[/QUOTE]
Just using the player object doesn't work when you want to use functions that expect tables
[code]
lua_run print(table.Count(player.GetAll()[1]))
> print(table.Count(player.GetAll()[1]))...
L 12/11/2012 - 05:10:52: Lua Error:
[ERROR] lua/includes/extensions/table.lua:162: bad argument #1 to 'pairs' (table expected, got userdata)
1. pairs - [C]:-1
2. Count - lua/includes/extensions/table.lua:162
3. unknown - lua_run:1
[/code]
[code]
> print(table.Count(player.GetAll()[1]:GetTable()))...
16
[/code]
for example, so I wouldn't really call it deprecated
[QUOTE=Feihc;38785020]Forgive me if this has been answered already, but why the frack don't any particle effects work with ParticleEffect() or ParticleEffectAttach() anymore? Even if I precache them the game says that they're invalid, even when I know they are valid particles. And yes, I have the appropriate games mounted.[/QUOTE]
Did you do [URL=http://wiki.garrysmod.com/page/Libraries/game/AddParticles]game.AddParticles()[/URL]?
[QUOTE=A Lost Sandwich;38787729]Did you do [URL=http://wiki.garrysmod.com/page/Libraries/game/AddParticles]game.AddParticles()[/URL]?[/QUOTE]
Beware that particles do not work in compressed workshop addons (*gma) at the moment. The users who subscribe to your addon will get [url=http://facepunch.com/showthread.php?t=1231869][b]this[/b][/url] error when trying to use it.
Though, they work in legacy addons. (non-compressed)
Getting this console spam from my snpc:
[code]
[ERROR] gamemodes/base/entities/entities/base_ai/schedules.lua:119: attempt to compare nil with number
1. NextTask - gamemodes/base/entities/entities/base_ai/schedules.lua:119
2. DoSchedule - gamemodes/base/entities/entities/base_ai/schedules.lua:75
3. unknown - gamemodes/base/entities/entities/base_ai/schedules.lua:22
[/code]
this is the only part that has anything to do with the schedule:
[code]function ENT:SelectSchedule()
local SCChase = ai_schedule.New("AIFighter Chase")
if isPlayerClose then
SCChase:EngTask("TASK_STOP_MOVING", 0)
SCChase:EngTask("TASK_FACE_ENEMY", 1)
SCChase:AddTask("PlaySequence", { Name = "attack1", Speed = 4 } )
else
SCChase:AddTask("FindEnemy", { Class = "player", Radius = 1024 } )
SCChase:EngTask("TASK_GET_PATH_TO_ENEMY", 0)
SCChase:EngTask("TASK_RUN_PATH_TIMED",0)
SCChase:EngTask("TASK_WAIT",0)
end
local isPlayerClose = false
local playertable = player.GetAll()
for k,v in pairs(playertable) do
if v:IsValid() && v:Alive() then
if v:GetPos():Distance(self:GetPos()) < 100 then
isPlayerClose = true
else
isPlayerClose = false
end
end
end
self:StartSchedule(SCChase)
end[/code]
edit: snpc's also "freeze" (but still animate and die if shot) when this error starts spamming itself.
could it be the code i'm using (not mine) to actually damage players? [url]http://pastebin.com/pazJVPzC[/url]
Is there a way I can check if a client has a model?
I am using a specific TF2 model, but not all clients will have TF2 installed, so I don't want people to see errors, so I was thinking only draw the entity (a special entity I made, uses a TF2 model) if the model exists. How can I check this? Thanks!
EDIT: found a way to check, use file.Find
P.S as a side note, am I correct in thinking it would be a breach of copyright to send the client the TF2 model on joining?
[QUOTE=A Lost Sandwich;38787729]Did you do [URL=http://wiki.garrysmod.com/page/Libraries/game/AddParticles]game.AddParticles()[/URL]?[/QUOTE]
Son of a... I hate all these game function calls. I don't care if it takes my game a little bit longer to load because it loads in the particles by default, finding what pcf each particle is in is a PITA and I would rather have the longer load times, imo
[QUOTE=Trumple;38792037]Is there a way I can check if a client has a model?
I am using a specific TF2 model, but not all clients will have TF2 installed, so I don't want people to see errors, so I was thinking only draw the entity (a special entity I made, uses a TF2 model) if the model exists. How can I check this? Thanks!
EDIT: found a way to check, use file.Find
P.S as a side note, am I correct in thinking it would be a breach of copyright to send the client the TF2 model on joining?[/QUOTE]
Technically yes but a) TF2 is free now and b) it's not like Valve check and c) you can get them all through srcds anyway
How can I use Weapon.SendWeaponAnim, Player.SetAnimation, and Entity.EmitSound in a custom function? It seems these work fine if I use them in a weapon function like primaryattack but I need to play weapon animations and emit sounds in a separate function.
[QUOTE=melontech;38793201]How can I use Weapon.SendWeaponAnim, Player.SetAnimation, and Entity.EmitSound in a custom function? It seems these work fine if I use them in a weapon function like primaryattack but I need to play weapon animations and emit sounds in a separate function.[/QUOTE]
Then just make a function? if you need to use the SWEP object then just add "SWEP:" to your function name like so:
[lua]
function SWEP:Animate()
local plyAnim = PLAYER_ATTACK1
local wepAnim = ACT_VM_PRIMARYATTACK
local sound = ""
self.Owner:SetAnimation(plyAnim)
self.Weapon:SendWeaponAnim(wepAnim)
self.Weapon:EmitSound(sound)
end
[/lua]
just call it with self:Animate() in a SWEP object function.
Assume I have an entity stuck to a player (it follows a bone of the biped of the player). How can I show this model on a panel of some kind, so the local player may view their own model and his attachment in the correct location? Is that even possible?
Thanks for any help
[editline]11th December 2012[/editline]
A mirror of sorts is what I'm after
[QUOTE=M0dSe7en;38793367]Then just make a function? if you need to use the SWEP object then just add "SWEP:" to your function name like so:
[lua]
function SWEP:Animate()
local plyAnim = PLAYER_ATTACK1
local wepAnim = ACT_VM_PRIMARYATTACK
local sound = ""
self.Owner:SetAnimation(plyAnim)
self.Weapon:SendWeaponAnim(wepAnim)
self.Weapon:EmitSound(sound)
end
[/lua]
just call it with self:Animate() in a SWEP object function.[/QUOTE]
Thanks, but the problem is that when I call those in my own function, they do not do anything, whereas they work fine when they are exactly the same in, say, primaryattack. In fact, they do not even work in primaryattack if I do not send the attack key press when I call it. I guess the real problem is they all only work at the moment of the attack key press?
I wrote a method to handle MOVETYPE_FLY so that it behaves like noclip, with clipping.
But, I'm having one issue with Angle:Up() not working as designed. I'm not sure if I need to report this as a bug yet, or if I'm using it incorrectly.
Here's the code:
Shared, noclip file.
[lua]function GM:PlayerNoClip ( Player )
if ( Player:GetMoveType( ) == MOVETYPE_FLY ) then
Player:SetMoveType( MOVETYPE_WALK )
else
Player:SetMoveType( MOVETYPE_FLY )
end
return false;
end[/lua]
Shared, setup move, be mindful that the movement code is wrapped in : if ( ply:GetMoveType( ) == MOVETYPE_FLY ) then
[lua]function GM:SetupMove( ply, mv, cmd )
local speed = 1000;
if ( ply:KeyDown( IN_JUMP ) ) then
mv:SetVelocity( pav:Angle():Up() * speed )
elseif ply:KeyDown( IN_DUCK ) then
mv:SetVelocity( pav:Angle():Up() * -speed )
end
end[/lua]
Problem is, it's not working as designed.
This code though, as an example of manipulating the aim vector, does work as designed:
[lua]
local pav = ply:GetAimVector()
if ( ply:KeyDown( IN_IN_MOVELEFT) ) then
pav = pav + ( pav:Angle():Right() * -speed )
end
mv:SetVelocity( pav )
[/lua]
Anyone have an idea, or is Up() broken?
Hi, i'm trying to fix an old script called Permanent Wanted Menu and I am getting this error.
[lua]
[ERROR] addons/wanted/lua/autorun/client/cl_wanted.lua:207: attempt to call method 'EnableHorizontal' (a nil value)
1. Function - addons/wanted/lua/autorun/client/cl_wanted.lua:207
2. unknown - lua/includes/modules/usermessage.lua:87
[/lua]
Any ideas?
snip
My script is supposed to set people who die as TEAM_MAYOR or TEAM_SPY back to TEAM_CITIZEN. However I am recieving this error:
[html]gamemodes/darkrp/gamemode/server/main.lua:60: attempt to call global 'NotifyAll' (a nil value)[/html]
Here is the function.
[lua]function MayorAssassination( ply, wep, killer )
if ply:Team() != TEAM_MAYOR then return end
if !killer:IsPlayer() then return end
ply:ChangeTeam(TEAM_CITIZEN, true)
NotifyAll(1,4,"The Mayor has been killed!") end
function JAssassination( ply, wep, killer)
if ply:Team() != TEAM_SPY then return end
if !killer:IsPlayer() then return end
ply:ChangeTeam(TEAM_CITIZEN, true)
NotifyAll(1,4,"Joey has been killed!") end
hook.Add("PlayerDeath", "MayorAssassination", MayorAssassination)
hook.Add("PlayerDeath", "JAssassination", JAssassination)[/lua]
The script successfully changes the player's TEAM, however the Notification refuses to work. Any ideas/tips? Can anyone help fix this?
Hi. I have this question: would it be theoretically possible to remove a certain "ambient_generic" from a map with a script? Or turn its volume to 0, so that no one can hear it anymore?
[QUOTE=MICKMAC;38797701]My script is supposed to set people who die as TEAM_MAYOR or TEAM_SPY back to TEAM_CITIZEN. However I am recieving this error:
[html]gamemodes/darkrp/gamemode/server/main.lua:60: attempt to call global 'NotifyAll' (a nil value)[/html]
Here is the function.
[lua]function MayorAssassination( ply, wep, killer )
if ply:Team() != TEAM_MAYOR then return end
if !killer:IsPlayer() then return end
ply:ChangeTeam(TEAM_CITIZEN, true)
NotifyAll(1,4,"The Mayor has been killed!") end
function JAssassination( ply, wep, killer)
if ply:Team() != TEAM_SPY then return end
if !killer:IsPlayer() then return end
ply:ChangeTeam(TEAM_CITIZEN, true)
NotifyAll(1,4,"Joey has been killed!") end
hook.Add("PlayerDeath", "MayorAssassination", MayorAssassination)
hook.Add("PlayerDeath", "JAssassination", JAssassination)[/lua]
The script successfully changes the player's TEAM, however the Notification refuses to work. Any ideas/tips? Can anyone help fix this?[/QUOTE]
GAMEMODE:NotifyAll
[QUOTE=sniperdude0;38794962]Hi, i'm trying to fix an old script called Permanent Wanted Menu and I am getting this error.
[lua]
[ERROR] addons/wanted/lua/autorun/client/cl_wanted.lua:207: attempt to call method 'EnableHorizontal' (a nil value)
1. Function - addons/wanted/lua/autorun/client/cl_wanted.lua:207
2. unknown - lua/includes/modules/usermessage.lua:87
[/lua]
Any ideas?[/QUOTE]
Post the cl_wanted file on pastebin and link, we need code!
-snip solved-
I've googled around for a while and couldn't find how to get a material or a texture of an HTML panel? Anyone know how to?
Or for that matter, does anyone know how else can I draw an HTML page in 3D space?
Sorry, you need to Log In to post a reply to this thread.