• My code for a HUD isn't working
    108 replies, posted
[QUOTE=Jcw87;26439787] Hang on, is this an external Lua interpreter? AddCSLuaFile doesn't exist anywhere other than gmod, so run it in gmod.[/QUOTE] [url]http://en.wikipedia.org/wiki/SciTE[/url] That is what I used for coding the debugger found a few errors but most of the errors I found were in the console for gmod such as this recent one I found after I edited the code: [lua\autorun\init.lua:2] attempt to call global 'AddCSLua' (a nil value)
[QUOTE=TheFallen(TF2);26457365][url]http://en.wikipedia.org/wiki/SciTE[/url] That is what I used for coding the debugger found a few errors but most of the errors I found were in the console for gmod such as this recent one I found after I edited the code: [lua\autorun\init.lua:2] attempt to call global 'AddCSLua' (a nil value)[/QUOTE] AddCSLua is not a function. You are looking for AddCSLuaFile. Did you read my post thoroughly?
[QUOTE=Jcw87;26459899]AddCSLua is not a function. You are looking for AddCSLuaFile. Did you read my post thoroughly?[/QUOTE] Yes I did so I corrected the problem and now its not showing any errors relating to AddCSLuaFile however I did get this error: lua: HUDDistrict9.lua:4: attempt to index global 'surface' (a nil value)
[QUOTE=TheFallen(TF2);26468279]Yes I did so I corrected the problem and now its not showing any errors relating to AddCSLuaFile however I did get this error: lua: HUDDistrict9.lua:4: attempt to index global 'surface' (a nil value)[/QUOTE] surface is a client-side library. It has to be run client-side. Change the top of your file to look like this: [lua] if SERVER then AddCSLuaFile("filename.lua") return end [/lua]
[QUOTE=Jcw87;26468604]surface is a client-side library. It has to be run client-side. Change the top of your file to look like this: [lua] if SERVER then AddCSLuaFile("filename.lua") return end [/lua][/QUOTE] Okay, but do I include the directory or not?
[QUOTE=TheFallen(TF2);26473331]Okay, but do I include the directory or not?[/QUOTE] You don't need to.
I checked the gmod wiki about this error here: lua: HUDDistrict9.lua:2: '<eof>' expected near 'end' and the gmod wiki has nothing about it, can anyone tell me what this means?
[QUOTE=TheFallen(TF2);26475444]I checked the gmod wiki about this error here: lua: HUDDistrict9.lua:2: '<eof>' expected near 'end' and the gmod wiki has nothing about it, can anyone tell me what this means?[/QUOTE] google the error, its a common one, it means your missing (or exceeding, forgot which) 1 or more "end"s
[QUOTE=TheFallen(TF2);26475444]I checked the gmod wiki about this error here: lua: HUDDistrict9.lua:2: '<eof>' expected near 'end' and the gmod wiki has nothing about it, can anyone tell me what this means?[/QUOTE] You are not using the correct number of 'end's. Load your script with notepad++, and pay close attention to the left margin. There will be buttons for "collapsing" your code. Pay attention to where the lines that extend from those buttons go. Example: [img]http://img574.imageshack.us/img574/1396/notepadpp.png[/img] Notice how the lines connect the beginning statement to the appropriate 'end'. Search your script for any irregularities with these lines.
[QUOTE=Jcw87;26475896]You are not using the correct number of 'end's. Load your script with notepad++, and pay close attention to the left margin. There will be buttons for "collapsing" your code. Pay attention to where the lines that extend from those buttons go. Example: [img_thumb]http://img574.imageshack.us/img574/1396/notepadpp.png[/img_thumb] Notice how the lines connect the beginning statement to the appropriate 'end'. Search your script for any irregularities with these lines.[/QUOTE] Okay that error has been taken care of but now I'm getting another error: lua: HUDDistrict9.lua:5: attempt to index global 'surface' (a nil value) Do I put function in front of it? Or Hook?
Are you running this script on the client? [editline]4th December 2010[/editline] What color is the error? Is it blue or yellow when it pops up in the upper right?
[QUOTE=Lord Ned;26486041]Are you running this script on the client? [editline]4th December 2010[/editline] What color is the error? Is it blue or yellow when it pops up in the upper right?[/QUOTE] I run it on the client for testing but this time the surface error didn't come up this time, but this did and it was blue: [lua\autorun\init.lua:2] attempt to call global 'AddCSLua' (a nil value) and when I tried to debug it in SciTE it came up with this red error: lua: HUDDistrict9.lua:4: attempt to index global 'surface' (a nil value) So this is a big double wammy for me, because I have no clue why it is doing one thing in SciTE and another totally different thing in Garry's Mod.
[QUOTE=TheFallen(TF2);26486400]I run it on the client for testing but this time the surface error didn't come up this time, but this did and it was blue: [lua\autorun\init.lua:2] attempt to call global 'AddCSLua' (a nil value) and when I tried to debug it in SciTE it came up with this red error: lua: HUDDistrict9.lua:4: attempt to index global 'surface' (a nil value) So this is a big double wammy for me, because I have no clue why it is doing one thing in SciTE and another totally different thing in Garry's Mod.[/QUOTE] Because in SciTE the lua debugger does not have garrysmod lua functions here's a tip, don't use SciTE anymore for debugging.
[QUOTE=Zephilinox;26486652]Because in SciTE the lua debugger does not have garrysmod lua functions here's a tip, don't use SciTE anymore for debugging.[/QUOTE] Okay, then what do I use then?
[QUOTE=TheFallen(TF2);26487522]Okay, then what do I use then?[/QUOTE] You don't use anything, you enter your game, and look for errors which get printed in the console, simple huh?
[QUOTE=Zephilinox;26487633]You don't use anything, you enter your game, and look for errors which get printed in the console, simple huh?[/QUOTE] Alright then, but what about this error here: [lua\autorun\init.lua:2] attempt to call global 'AddCSLua' (a nil value)
[QUOTE=TheFallen(TF2);26490598]Alright then, but what about this error here: [lua\autorun\init.lua:2] attempt to call global 'AddCSLua' (a nil value)[/QUOTE] put your entire code here again but it should be [lua] if SERVER then AddCSLua "init.lua" end[/lua] although it could be because you named it init.lua, try naming it something else.
[QUOTE=Zephilinox;26491253]put your entire code here again but it should be [lua] if SERVER then AddCSLua "init.lua" end[/lua] although it could be because you named it init.lua, try naming it something else.[/QUOTE] Its not named init.lua. Any way here is the code: [lua]if SERVER then AddCSLua ("cl_hud_remove.lua") print("cl_hud_remove.lua loaded!") end surface.CreateFont ("D9 non-humans",54, 400, false, false, "Text32") function MyCustonHUDPaint() print ( "GM:HUDPaint() blah blah" ) local person = LocalPlayer() print ("Player: "..tostring(person)) local Health = LocalPlayer(): Health() print ("Health: "..tostring(helath)) surface.SetText( 255, 0, 0, 255 ) surface.SetTextPos( 34, ScrH()/2 + ScrH()/4 ) surface.SetFont("Text32") surface.DrawText( Health ) end hook.Add( "HUDPaint", "MyCustomHUDPaint", MyCustomHUDPaint)[/lua]
Do this: [lua] if (SERVER) then AddCSLuaFile( "test_hud.lua" ) end if (CLIENT) then function HideHudBits( name ) if ( name == "CHudHealth" or name == "CHudBattery") then return false end end hook.Add( "HUDShouldDraw", "HideHudBits", HideHudBits ) surface.CreateFont( "D9 non-humans", 54, 400, false, false, "D9NonHumans" ) hook.Add( "HUDPaint", "D9HudPaint", function() local hudPlayer = LocalPlayer() local hudHealth = hudPlayer:Health() surface.SetTextColor( 255, 255, 255, 255 ) surface.SetTextPos ( 34, (ScrH()/2) + (ScrH()/4) ) surface.SetFont( "D9NonHumans" ) surface.DrawText( hudHealth ) end) end [/lua] Then put that in "D9Hud.lua" inside your lua/autorun folder. It works, throws no errors, etc. Which results in this: [img]http://i52.tinypic.com/148lttx.png[/img] [editline]4th December 2010[/editline] Also the D9 Non-Humans font is worthless, you can't tell anything about it really. I tried making it bigger and giving it a drop shadow/outline to no avail.. [img]http://i51.tinypic.com/mr8p51.png[/img]
[QUOTE=TheFallen(TF2);26492258]Its not named init.lua. Any way here is the code: [lua]if SERVER then AddCSLua ("cl_hud_remove.lua") print("cl_hud_remove.lua loaded!") end surface.CreateFont ("D9 non-humans",54, 400, false, false, "Text32") function MyCustonHUDPaint() print ( "GM:HUDPaint() blah blah" ) local person = LocalPlayer() print ("Player: "..tostring(person)) local Health = LocalPlayer(): Health() print ("Health: "..tostring(helath)) surface.SetText( 255, 0, 0, 255 ) surface.SetTextPos( 34, ScrH()/2 + ScrH()/4 ) surface.SetFont("Text32") surface.DrawText( Health ) end hook.Add( "HUDPaint", "MyCustomHUDPaint", MyCustomHUDPaint)[/lua][/QUOTE] then why is your error [code][lua\autorun\[u][b]init.lua[/b][/u]:2[/code]
Thanks for the help guys it finally works! I owe all of you for the help big time! But I have a few questions before this thread is locked: 1. How do I add boxes to go around the the health text? 2. How do I add a health bar? 3. And when I add armor do I add a separate function or not?
Take a look at this thread: [url]http://www.facepunch.com/threads/1032991[/url] Just make sure you draw the box first, and then draw the text over the box's position. also take a look at this: [url]http://wiki.garrysmod.com/?title=Clientside:draw[/url] this shows you how to make a health bar, and add text to it: [url]http://wiki.garrysmod.com/?title=Simple_HUD_Tut[/url] And there were several spellings mistakes in the script you posted which would stop it from working, heres the fixed version: [lua] if SERVER then AddCSLuaFile ("cl_hud_remove.lua") print("cl_hud_remove.lua loaded!") end surface.CreateFont ("D9 non-humans",54, 400, false, false, "Text32") function MyCustomHUDPaint() print ( "GM:HUDPaint() blah blah" ) local person = LocalPlayer() print ("Player: "..tostring(person)) local health = LocalPlayer():Health() print ("Health: "..tostring(health)) surface.SetText( 255, 0, 0, 255 ) surface.SetTextPos( 34, ScrH()/2 + ScrH()/4 ) surface.SetFont("Text32") surface.DrawText( health ) end hook.Add( "HUDPaint", "MyCustomHUDPaint", MyCustomHUDPaint) [/lua]
1. Learn Lua 2. Learn Lua 3. Learn Lua However... Only since I still have the file open: [lua] if (SERVER) then AddCSLuaFile( "test_hud.lua" ) end if (CLIENT) then function HideHudBits( name ) --This is run once, when the hud first tries to draw if ( name == "CHudHealth" or name == "CHudBattery") then return false end end hook.Add( "HUDShouldDraw", "HideHudBits", HideHudBits ) surface.CreateFont( "D9 non-humans", 78, 400, true, false, "D9NonHumans", true, true ) hook.Add( "HUDPaint", "D9HudPaint", function() --This is run every frame. Anything that needs to get drawn can be added here local hudPlayer = LocalPlayer() local hudHealth = hudPlayer:Health() local hudArmor = --What's it equal? Here's a hint, it's found here: [url]http://wiki.garrysmod.com/?title=Player[/url] It's also similar to "hudPlayer:Health() --If we want to draw backgrounds, we have to do it before we draw the stuff ontop of it, or else it'd draw over soo.. --More stuff/information here: [url]http://wiki.garrysmod.com/?title=Surface[/url] surface.SetDrawColor( 128, 128, 128, 255) --Set it's background to a generic grey surface.DrawRect( x position, y position, width, height) --FILL THESE OUT --DRAW THE FONTS ONTOP surface.SetTextColor( 255, 255, 255, 255 ) surface.SetTextPos ( 34, (ScrH()/2) + (ScrH()/4) ) surface.SetFont( "D9NonHumans" ) surface.DrawText( hudHealth ) --Now do more surface commands. You've already set the color, and the font... So maybe if you just change the position and what text it should draw? surface.SetTextPos --Fill this out surface.DrawText( ) --What do we want to draw now, armor? We have a variable, maybe put that in here? end) end [/lua] [b]You'll probably want to use this over Zeph's just because it matches your other code and is written to be run on both the client/server without issues.[/b]
You mean sort of like this: [lua]if (SERVER) then AddCSLuaFile( "cl_hud_remove.lua" ) end if (CLIENT) then function HideHudBits( name ) if ( name == "CHudHealth" or name == "CHudBattery") then return false end end hook.Add( "HUDShouldDraw", "HideHudBits", HideHudBits ) surface.CreateFont( "D9 non-humans", 100, 400, false, false, "D9NonHumans" ) hook.Add( "HUDPaint", "D9HudPaint", function() local hudPlayer = LocalPlayer() local hudHealth = hudPlayer:Health() local hudArmor = hudPlayer:Armor() surface.SetDrawColor( 128, 128, 128, 255) surface.DrawRect( 1, 34, 25, 25 ) surface.SetTextColor( 255, 0, 0, 255 ) surface.SetTextPos ( 34, (ScrH()/2) + (ScrH()/4) ) surface.SetFont( "D9NonHumans" ) surface.DrawText( hudHealth ) surface.SetTextColor(255,0, 0, 255) surface.SetTextPos(24 (ScrH()/2) + (ScrH()/4) ) surface.SetFont("D9NonHumans") surface.DrawText( HudArmor ) end) end [/lua]
Looks like it'd work, does it not? Also the: if (SERVER) then AddCSLuaFile( "cl_hud_remove.lua" ) end You don't need cl_hud_remove.lua anymore, I built it into the start of the if (CLIENT) section. I had it downloading the hud itself, which I don't know if it's necessary, depends if you're using it as a gamemode or just an addon people can install.
[QUOTE=Lord Ned;26506620]Looks like it'd work, does it not? Also the: if (SERVER) then AddCSLuaFile( "cl_hud_remove.lua" ) end You don't need cl_hud_remove.lua anymore, I built it into the start of the if (CLIENT) section. I had it downloading the hud itself, which I don't know if it's necessary, depends if you're using it as a gamemode or just an addon people can install.[/QUOTE] -snip- ..
[QUOTE=Lord Ned;26506620]Looks like it'd work, does it not? Also the: if (SERVER) then AddCSLuaFile( "cl_hud_remove.lua" ) end You don't need cl_hud_remove.lua anymore, I built it into the start of the if (CLIENT) section. I had it downloading the hud itself, which I don't know if it's necessary, depends if you're using it as a gamemode or just an addon people can install.[/QUOTE] I'm going to be using it in a remake of GoFish2 I'm planning on making which will have a menu that can enable custom HUDS. Heres the code for the HUD so far btw: [lua]function HideHudBits( name ) if ( name == "CHudHealth" or name == "CHudBattery" or name == "CHudAmmo" or name == "CHudSecondaryAmmo") then return false end end hook.Add( "HUDShouldDraw", "HideHudBits", HideHudBits ) surface.CreateFont( "D9 non-humans", 100, 400, false, false, "D9NonHumans" ) hook.Add( "HUDPaint", "D9HudPaint", function() local hudPlayer = LocalPlayer() local hudHealth = hudPlayer:Health() local hudArmor = hudPlayer:Armor() surface.SetDrawColor ( 128, 128, 128, 255) surface.DrawRect ( 1, 34, 25, 25 ) surface.SetTextColor( 255, 0, 0, 255 ) surface.SetTextPos ( 34, (ScrH()/2) + (ScrH()/4) ) surface.SetFont ( "D9NonHumans" ) surface.DrawText ( hudHealth ) surface.SetTextColor (255,0, 0, 255) surface.SetTextPos (24 (ScrH()/2) + (ScrH()/4) ) surface.SetFont ("D9NonHumans") surface.DrawText ( HudArmor ) end) end [/lua] It stopped working and I don't know why.
You have to tell us what's wrong, we're not mind readers. [editline]5th December 2010[/editline] What error, what line, what message, etc. Look in your console and scroll up to find one.
[QUOTE=TheFallen(TF2);26509606]I'm going to be using it in a remake of GoFish2 I'm planning on making which will have a menu that can enable custom HUDS. Heres the code for the HUD so far btw: [lua]function HideHudBits( name ) if ( name == "CHudHealth" or name == "CHudBattery" or name == "CHudAmmo" or name == "CHudSecondaryAmmo") then return false end end hook.Add( "HUDShouldDraw", "HideHudBits", HideHudBits ) surface.CreateFont( "D9 non-humans", 100, 400, false, false, "D9NonHumans" ) hook.Add( "HUDPaint", "D9HudPaint", function() local hudPlayer = LocalPlayer() local hudHealth = hudPlayer:Health() local hudArmor = hudPlayer:Armor() surface.SetDrawColor ( 128, 128, 128, 255) surface.DrawRect ( 1, 34, 25, 25 ) surface.SetTextColor( 255, 0, 0, 255 ) surface.SetTextPos ( 34, (ScrH()/2) + (ScrH()/4) ) surface.SetFont ( "D9NonHumans" ) surface.DrawText ( hudHealth ) surface.SetTextColor (255,0, 0, 255) surface.SetTextPos (24 (ScrH()/2) + (ScrH()/4) ) surface.SetFont ("D9NonHumans") surface.DrawText ( HudArmor ) end) end [/lua] It stopped working and I don't know why.[/QUOTE] Proper indentation of code helps to find silly errors. [lua] function HideHudBits( name ) if ( name == "CHudHealth" or name == "CHudBattery" or name == "CHudAmmo" or name == "CHudSecondaryAmmo") then return false end end hook.Add( "HUDShouldDraw", "HideHudBits", HideHudBits ) surface.CreateFont( "D9 non-humans", 100, 400, false, false, "D9NonHumans" ) hook.Add( "HUDPaint", "D9HudPaint", function() local hudPlayer = LocalPlayer() local hudHealth = hudPlayer:Health() local hudArmor = hudPlayer:Armor() surface.SetDrawColor ( 128, 128, 128, 255) surface.DrawRect ( 1, 34, 25, 25 ) surface.SetTextColor( 255, 0, 0, 255 ) surface.SetTextPos ( 34, (ScrH()/2) + (ScrH()/4) ) surface.SetFont ( "D9NonHumans" ) surface.DrawText ( hudHealth ) surface.SetTextColor (255,0, 0, 255) surface.SetTextPos (24 (ScrH()/2) + (ScrH()/4) ) surface.SetFont ("D9NonHumans") surface.DrawText ( HudArmor ) end) end -- One too many 'end's [/lua]
[lua\autorun\client\huddistrict9.lua:25] ')' expected near '(' I don't understand what that means I checked the wiki and it didn't say what the error was, plus the game has reverted back to using the HL2 HUD.
Sorry, you need to Log In to post a reply to this thread.