I have some things in the meta file (sh_player.lua, to be exact) and I have part of a hud in cl_init.lua that uses some of those functions. The problem is, cl_init.lua loads before sh_player.lua (I think, due to these symptoms) and that causes cl_init.lua to not fully load since it hits an error and will not continue for anything. How do I get the meta file to load before cl_init 100% of the time?
Shared should always load before client / server files.
Here's how to properly create HUDs ( as LocalPlayer is nil for the first few calls ): [url]https://dl.dropboxusercontent.com/u/26074909/tutoring/vgui/proper_hud_creation.lua.html[/url]
[QUOTE=Acecool;46062155]Shared should always load before client / server files.
Here's how to properly create HUDs ( as LocalPlayer is nil for the first few calls ): [url]https://dl.dropboxusercontent.com/u/26074909/tutoring/vgui/proper_hud_creation.lua.html[/url][/QUOTE]
HUDPaint, I can't do that really, I just have a timer to update values for the hud every .5 seconds. The reason is, I tried using HUDPaint, but the hud itself kept stacking and stacking pretty much every frame, removing transparency and causing intense lag.
EDIT: Misread, code is useful sometimes when you want help.
[QUOTE=RonanZer0;46062168]HUDPaint, I can't do that really, I just have a timer to update values for the hud every .5 seconds. The reason is, I tried using HUDPaint, but the hud itself kept stacking and stacking pretty much every frame, removing transparency and causing intense lag.[/QUOTE]
Because you're recreating things each frame instead of just rendering. Don't call CreateFont in the hook, don't create vgui elements if they already exist, etc....
Example, drawing a poly each frame but only refreshing the table when the resolution changes: [url]https://dl.dropboxusercontent.com/u/26074909/tutoring/_hooks/acecool_hook_playerchangedresolution/gm_playerchangedresolution.lua[/url]
[url]https://dl.dropboxusercontent.com/u/26074909/tutoring/_hooks/acecool_hook_playerchangedresolution/example_usage.lua[/url]
[url]https://dl.dropboxusercontent.com/u/26074909/tutoring/poly/simplified_circles_with_poly.lua.html[/url]
Show us your code, we'll help you fix the issues.
[QUOTE=Acecool;46062186]Because you're recreating things each frame instead of just rendering. Don't call CreateFont in the hook, don't create vgui elements if they already exist, etc....
Example, drawing a poly each frame but only refreshing the table when the resolution changes: [url]https://dl.dropboxusercontent.com/u/26074909/tutoring/_hooks/acecool_hook_playerchangedresolution/gm_playerchangedresolution.lua[/url]
[url]https://dl.dropboxusercontent.com/u/26074909/tutoring/_hooks/acecool_hook_playerchangedresolution/example_usage.lua[/url]
[url]https://dl.dropboxusercontent.com/u/26074909/tutoring/poly/simplified_circles_with_poly.lua.html[/url]
Show us your code, we'll help you fix the issues.[/QUOTE]
Meta:
[lua]local meta = FindMetaTable("Player")
function meta:GetMoney()
return self:GetNetworkedInt( "Money" )
end
function meta:AddMoney(amount)
local current_cash = self:GetMoney()
self:SetMoney( current_cash + amount )
end
function meta:SetMoney(amount)
self:SetNetworkedInt( "Money", amount )
self:SaveMoney()
end
function meta:SaveMoney()
local cash = self:GetMoney()
self:SetPData("money", cash)
end
function meta:TakeMoney(amount)
self:AddMoney(-amount)
end
[/lua]
Meta is of course, sh_player, it is included and addcsluafile'd where it should be.
Hud:
[lua]function carrystatus()
surface.CreateFont( "coolvetica2", {
font = "coolvetica",
size = 48,
weight = 1,
blursize = 0,
scanlines = 0,
antialias = true,
underline = false,
italic = false,
strikeout = false,
symbol = false,
rotary = false,
shadow = false,
additive = false,
outline = false,
} )
local cry = vgui.Create( "DFrame" )
cry:SetPos( ScrW() - 330, ScrH() - 430 )
cry:SetSize( 300, 160 )
cry:SetTitle( "" )
cry:ShowCloseButton( false )
cry:SetVisible( true )
cry:SetDraggable( false )
function cry:Paint()
draw.RoundedBox( 0, 0, 0, self:GetWide(), self:GetTall(), Color( 0,0,0,240 ) )
end -- Now we ONLY end the painting function.
local blahe = vgui.Create("DLabel", cry)
blahe:SetPos(4,3)
blahe:SetColor(Color(255,255,255,255))
blahe:SetFont("coolvetica2")
if LocalPlayer():GetMoney() > 0 then
blahe:SetText("Carrying:\n$" .. LocalPlayer():GetMoney())
else
blahe:SetText("Carrying:\nNothing")
end
blahe:SizeToContents()
function closecarry()
if IsValid( cry ) then
cry:Remove()
end
end
concommand.Add("closecarryhud", closecarry)
timer.Simple( 3, function()
if IsValid( cry ) then
cry:Remove()
else end
end)
end
timer.Create("carrystatusrefresh", 3, 0, carrystatus)
//hook.Add( "HUDPaint", "uniqueloljk", carrystatus )
concommand.Add("carryhud", carrystatus)[/lua]Yes, it is pretty messy.
You're calling carrystatus every 3 seconds which in turn creates a FONT and creates a FRAME and DLabel....
Use this ( something similar anyway; I didn't go through and redo everything with my format, just loosely.... ):
[code]//
// Create the font ONCE when the script loads... Fonts are actually kept in memory even after you disconnect from a server unless that has been recently fixed..... I'd recommend creating this once, like in a hook.Initialize...
//
hook.Add( "Initialize", "CreateMyFont", function( )
// Font 1...
surface.CreateFont( "coolvetica2", {
font = "coolvetica",
size = ScreenScale( 48 ),
weight = 1,
blursize = 0,
scanlines = 0,
antialias = true,
underline = false,
italic = false,
strikeout = false,
symbol = false,
rotary = false,
shadow = false,
additive = false,
outline = false,
} )
// Any more fonts?
end );
// Autorefresh compatible global... Set it to itself, or just nil it.. So, when it is set as a panel, we use it on autorefresh...
__crypanels = __crypanels || { };
//
// Helper function to create the vgui panel, but only if it hasn't been created yet. You can have this as a global, you could store it in GAMEMODE, or wherever...
//
local function CreateCryPanel( )
// So, if it is nil or not a panel...
if ( !__crypanels.main ) then
// Create it...
__crypanels.main = vgui.Create( "DFrame" )
__crypanels.main:SetPos( ScrW() - 330, ScrH() - 430 )
__crypanels.main:SetSize( 300, 160 )
__crypanels.main:SetTitle( "" )
__crypanels.main:ShowCloseButton( false )
__crypanels.main:SetVisible( true )
__crypanels.main:SetDraggable( false )
function __crypanels.main:Paint( )
draw.RoundedBox( 0, 0, 0, self:GetWide( ), self:GetTall( ), Color( 0, 0, 0, 240 ) )
end -- Now we ONLY end the painting function.
__crypanels.label = vgui.Create( "DLabel", __crypanels.main )
__crypanels.label:SetPos( 4, 3 )
__crypanels.label:SetColor( Color( 255, 255, 255, 255 ) )
__crypanels.label:SetFont( "coolvetica2" )
elseif( ispanel( __crypanels.main ) && !__crypanels.main:Visible( ) ) then
// But, if it is set, and it isn't visible ( if this is being called then we want it visible?? )
__crypanels.main:SetVisible( true );
end
end
//
// HUDPaint function... Typically I'm not a fan of this format, I usually do the hook.Add.... function( ).... end );... But, I left as much as is but just added protection and certain checks in.
//
local function carrystatus()
// Player may not be valid, if that is the case just ignore this until player is initialized...
if ( !IsValid( LocalPlayer( ) ) ) then return; end
// Will either create it, or make it visible instead of constantly creating it...
// Can be changed to not control visibility, but only control creating if it doesn't exist...
// Something like this is ok, but I'd recommend a control struct more similar to how I do
// PlayerChangedResolution... Right now you have this thing hard-coded in size, it doesn't scale for
// any resolution at all.... IF you used a struct to create once on InitPostEntity / Initialize, then
// have a hook for PlayerChangedResolution ( custom ) where you update the sizes, then yeah....
CreateCryPanel( );
// You can do this...
if LocalPlayer():GetMoney() > 0 then
__crypanels.label:SetText("Carrying:\n$" .. LocalPlayer():GetMoney())
else
__crypanels.label:SetText("Carrying:\nNothing")
end
__crypanels.label:SizeToContents( )
end
-- timer.Create("carrystatusrefresh", 3, 0, carrystatus)
-- concommand.Add("carryhud", carrystatus)
// This will call each frame, it will recreate the vgui if it gets destroyed with how it is set up.
// BUT, look at how this is written..... It is not set to continuously create data that is already initialized....
hook.Add( "HUDPaint", "uniqueloljk", carrystatus )
//
// Concommand to destroy the hud.. Remember, the HUDPaint will recreate it as soon as it is gone...
//
concommand.Add("closecarryhud", function( )
if ispanel( __crypanels.main ) then
// Lets remove the label, and main window, then reset the array so that it can be recreated...
__crypanels.label:Remove( )
__crypanels.main:Remove( )
__crypanels = { };
end
end )[/code]
Edit: Just in case the FP code block messes up: [url]https://dl.dropboxusercontent.com/u/26074909/work/debugging/ronanzero_quick_fix.lua.html[/url] and [url]https://dl.dropboxusercontent.com/u/26074909/work/debugging/ronanzero_quick_fix.lua[/url]
[code]elseif( ispanel( __crypanels.main ) && !__crypanels.main:Visible( ) ) then[/code]
[code]cl_init.lua:160: attempt to call method 'Visible' (a nil value)[/code]
What?
Whoops.. Should be IsVisible; but, that's there to show you wants to tests to see if something exists as a panel but may be hidden at the time being. The function there the way it is isn't that great....
[url]https://dl.dropboxusercontent.com/u/26074909/tutoring/metatables/__metatables_sorted_func_names.rar[/url] > cl_panel_metatable.txt
It is fixed... Located here again: [url]https://dl.dropboxusercontent.com/u/26074909/work/debugging/ronanzero_quick_fix.lua.html[/url] and [url]https://dl.dropboxusercontent.com/u/26074909/work/debugging/ronanzero_quick_fix.lua[/url]
Here are some other vgui things that may help you out:
Proper HUD Creation ( checking LocalPlayer( ) is valid is very important. If you show weapon info, SWEP data needs to be checked because not all weapons are SWEPs...
[url]https://dl.dropboxusercontent.com/u/26074909/tutoring/vgui/proper_hud_creation.lua.html[/url]
Shows how you can hard-code your HUD or panels, but apply a modifier to them so that everyone on every resolution will see essentially the same thing...
[url]https://dl.dropboxusercontent.com/u/26074909/tutoring/vgui/understanding_hardcoding_of_screensizes.lua.html[/url]
Here's that update on changed resolution bit I was talking about
[url]https://dl.dropboxusercontent.com/u/26074909/tutoring/_hooks/acecool_hook_playerchangedresolution/gm_playerchangedresolution.lua[/url]
And actually putting it to use
[url]https://dl.dropboxusercontent.com/u/26074909/tutoring/_hooks/acecool_hook_playerchangedresolution/example_usage.lua[/url]
Example of networking data to show a countdown on the hud
[url]https://dl.dropboxusercontent.com/u/26074909/tutoring/vgui/sh_hud_countdown.lua.html[/url]
Creating shapes with poly.. Its just connect the dots; works best if you apply the PlayerChangedResolution idea to it... Generate the table once, and only update it if resolution changes...
[url]https://dl.dropboxusercontent.com/u/26074909/tutoring/poly/creating_shapes_using_poly.lua.html[/url]
Poly, connect the dots, Circles...
[url]https://dl.dropboxusercontent.com/u/26074909/tutoring/poly/simplified_circles_with_poly.lua.html[/url]
Simplify ideas; a user was having trouble making a tilted rect.. This explains how you can take a step back, simplify the logic to make things easy, then apply the change necessary to accomplish the task at hand
[url]https://dl.dropboxusercontent.com/u/26074909/tutoring/poly/tilted_rectangle_poly_as_health_meter.lua.html[/url]
another tilted bar
[url]https://dl.dropboxusercontent.com/u/26074909/tutoring/poly/another_tilted_health_bar.lua.html[/url]
I think this uses HUDPaint ONCE then removes it self ( runs more than once while LocalPlayer is invalid, but to ensure timer starts properly, it waits until LP is valid, then it removes the hook, etc.. ); Think outside the box, things you think are only used for one thing may have other purposes..
[url]https://dl.dropboxusercontent.com/u/26074909/tutoring/vgui/display_something_on_initial_spawn.lua.html[/url]
// Similar??
[url]https://dl.dropboxusercontent.com/u/26074909/tutoring/vgui/display_something_on_initial_spawn_teams.lua.html[/url]
Alternative to the rounded box
[url]https://dl.dropboxusercontent.com/u/26074909/tutoring/vgui/draw_roundedbox_expand_left_instead_of_right.lua.html[/url]
MOTD... How to create the object once and re-use, or destroy and recreate...?
[url]https://dl.dropboxusercontent.com/u/26074909/tutoring/vgui/motd.lua.html[/url]
// Communicating server to client, or just client to client to get something done...
[url]https://dl.dropboxusercontent.com/u/26074909/tutoring/vgui/open_vgui_based_on_keypress.lua.html[/url]
Sorry, you need to Log In to post a reply to this thread.