General > Developers Corner
Lua n00b questions
krooks:
Hey all, been a busy couple weeks, back with a new code:
--- Code: ---function GlowT(ply)
if IsValid(LocalPlayer()) and LocalPlayer():IsActiveTraitor() then
for _, v in pairs (player.GetAll()) do
if v:IsActiveTraitor() then
effects.halo.Add({v}, Color(255,10,0), 2, 2, 2, true, true)
end
end
end
end
hook.Add("Think", "GlowT", GlowT)
--- End code ---
I wrote this to make T buddies glow, it works, and I've been running it for a few weeks now, but I've noticed it crashes players every so often. It will crash me almost every time a map changes, and I'm a T the first round.
Is there anything that stands out to anyone that I might be able to optimize or that could be done better?
===
EDIT
On the wiki ( http://wiki.garrysmod.com/page/Libraries/halo/Add ) it shows them using "PreDrawHalos" as the hook, I'll test with that and give results.
JamminR:
What I see -
Optimization thoughts -
You check for valid player, then if they are an active traitor. - Does the TTT IsActiveTraitor function not already do the valid player check? If so, seems redundant.
Your using 'think' hook. That's fine when absolutely necessary, but, code must be optimized like crazy. Think is called every frame.
Every frame, you're asking the server to cycle through all players, verify they are traitor, AND if so add a halo.
I've no experience with the halo effect, but, if it will add itself to the player once and stay with them until you tell it otherwise, then don't use think to add it.
Find the hooks/functions, if there are custom ones, where TTT actually updates the player to become 'Traitor' (team change?), THEN add the halo effect (or remove, if left)
And, even less familiar to me, yes, check out the wiki link you provided, and see if you can make glow's 'view' only.
General oddity -
You write GlowT(PLY) - do you ever actually call the function passing it a object(player or otherwise)?
Doesn't seem to be a ply object used in the rest of the function.
Possible crash ??
During your code, you check yourself (local player) to see if you are valid.
THEN, you grab all players, check if they are traitor, then apply the effect.
I get the idea that, pertaining to my original "does IsActiveTraitor check player validity?" question above, the answer is no.
So, I'm thinking it may be crashing, especially during a map change, when the code is trying to apply an effect to a player that just isn't quite valid yet, or if mid game, trying to apply to a player that left/isn't valid but is still queued up in the for loop.
krooks:
I am adding a tab to Pointshop that displays a webpage.
--- Code: ---local XTab = vgui.Create( "DHTML" )
XTab:SetSize( self:GetWide() - 20, self:GetTall())
XTab:OpenURL( "http://XXXXX.com/X.php" )
tabs:AddSheet('Hello There', XTab, 'icon16/badge.png', false, false, '')
--- End code ---
It displays properly, scrolls properly, <input type="submit"> is clickable, links are clickable;
but I can't use dropdown <select> menus, and <input type="text"> does not take focus.
I can select the text within the text box with the cursor, but when I type nothing shows up in the box, instead it just executes the game binds like walk, say, etc.
Someone suggested toggling "mouse and keyboard clickers" first.
So I added all of these separately, and together with no luck:
--- Code: ---XTab:SetMouseInputEnabled(true)
XTab:SetKeyboardInputEnabled(true)
gui.EnableScreenClicker(true)
--- End code ---
If I XTab:MakePopup() I am able to enter text in the <input> (delete key doesn't work) but the <select> box still doesn't work at all.
And I'd rather this not be a popup, because of how pointshop is already built.
Any ideas?
note, I've also tried XTab = vgui.Create( "HTML" ), (didn't work) but from the wiki it sounds like DHTML is best.
krooks:
Hiya! Been super busy lately but recently got back into messing with lua stuffs for my server, quick question:
What's the best way to go about calling ulx.playsound (or other ulx functions like it) within another script that is called from a hook event?
Currently it just looks like ulx.playsound(nil,"blah/blah.mp3")
The sound plays, but I'm getting an error because it's not being called by a player, so the fancy logging gets confused.
Thankyuh! Hope you've all been well
JamminR:
Welcome back. You've been missed.
One question - do you really need logging of the command, if being used in a hook?
I imagine not needing it for a hook event (such as somebody said something, did something, do you really need "console played sound <x>" in logging/notify when the something happened.?
EDIT - to save you time -
If you need the same log the command does, use actual command, not function. RunConCommand (or whatever it is now) ("ulx playsound " .. your_sound)
The actual command will determine it's the console and pass/log it as calling_ply.
If you don't need the log, verify your sound exists on the server and then use the ULib usermessage sound command (actually used within ulx playsound itself)
--- Code: --- umsg.Start( "ulib_sound" )
umsg.String( Sound( your_sound ) )
umsg.End()
--- End code ---
Navigation
[0] Message Index
[#] Next page
[*] Previous page
Go to full version