Hi facepunch, I am making a gamemode and its been coming nicely and I've decided to make a new HUD. Now the error I'm getting is related to this piece of code here.
[CODE]
function GM:CalcView()
local veh = ply:GetVehicle()
local w = ply:GetActiveWeapon()
local vm = ply:GetViewModel()
local nobob = w.NoViewBob and not IsValid( v )
local self = GM or GAMEMODE;
local view = self.BaseClass:CalcView( ply, origin, angles, ... ) -- Right here
if not ply:Alive() then
local rag = ply:GetObserverTarget()
if IsValid( rag ) then
local rpos = rag:GetPos() + rag:OBBCenter()
self.DeathCam = math.min( ( self.DeathCam or 0 ) + FrameTime() * 12, 230 )
view.origin = rpos + Vector( 0, 0, 100 + self.DeathCam )
view.angles = Angle( 90, 0, 0 )
view.angles:RotateAroundAxis( Vector( 1, 0, 0 ), math.sin( CurTime() * 1.5 ) * 12 )
end
[/CODE]
The code I commented is printing out the BaseClass as nil and removing it gives me self as an error. The really weird thing is, when I make ANY change to the hud lua script and re-save it, the HUD magically stops giving me errors and works again. I have to do this everytime I join and I really have no idea whats going on. It should work but its not.
Here is the full lua script for the HUD.
[CODE]
local bIsHUDActive = true;
local g = _G;
local r = debug.getregistry();
surface.CreateFont( "Font_HUDTiny", {
font = "arial",
size = 14,
weight = 1,
blursize = 0.3,
antialias = 0
});
surface.CreateFont( "Font_HUDTiny_Outline", {
font = "arial",
size = 14,
weight = 1,
blursize = 2,
antialias = 0
});
surface.CreateFont( "Font_HUDSmall", {
font = "arial",
size = 17,
weight = 1,
blursize = 0.3,
antialias = 0
});
surface.CreateFont( "Font_HUDSmall_Outline", {
font = "arial",
size = 17,
weight = 1,
blursize = 2,
antialias = 0
});
surface.CreateFont( "Font_HUDMedium", {
font = "arial",
size = 38 / 1.5,
weight = 1,
blursize = 0.3,
antialias = 0
});
surface.CreateFont( "Font_HUDMedium_Outline", {
font = "arial",
size = 38 / 1.5,
weight = 1,
blursize = 2,
antialias = 0
});
surface.CreateFont( "Font_HUDHalfMedium", {
font = "arial",
size = 38 / 1.1,
weight = 500,
blursize = 0.3,
antialias = 0
});
surface.CreateFont( "Font_HUDHalfMedium_Outline", {
font = "arial",
size = 38 / 1.1,
weight = 500,
blursize = 2,
antialias = 0
});
surface.CreateFont( "Font_HUDBig", {
font = "arial",
size = 38,
weight = 1,
blursize = 0.3,
antialias = 0
});
surface.CreateFont( "Font_HUDBig_Outline", {
font = "arial",
size = 38,
weight = 1,
blursize = 2,
antialias = 0
});
local cam = cam;
local chat = chat;
local concommand = concommand;
local hook = hook;
local player = player;
local Entity = Entity;
local ents = ents;
local entity = ents;
local surface = surface;
local draw = draw;
local table = table;
local math = math;
local team = team;
local halo = halo;
local usermessage = usermessage;
local net = net;
local Vector = Vector;
local Angle = Angle;
local Color = Color;
local pairs = pairs;
local ipairs = ipairs;
local CurTime = CurTime;
local IsValid = IsValid;
local LocalPlayer = LocalPlayer;
local cookPancakes = chat.AddText;
local createPancake = concommand.Add;
local buildFridge = hook.Add;
local print = print;
local entities = entity.GetAll();
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function GetTextWidth(hFont, strString)
surface.SetFont(hFont);
local iWidth, iHeight = surface.GetTextSize(strString);
return iWidth;
end
function DrawRectangle(pointX, pointY, pointW, pointH, colColor)
surface.SetDrawColor(colColor);
surface.DrawOutlinedRect(pointX, pointY, pointW, pointH);
end
function DrawFilledRectangle(pointX, pointY, pointW, pointH, colColor)
surface.SetDrawColor(colColor);
surface.DrawRect(pointX, pointY, pointW, pointH);
end
function DrawString(hFont, bCenter, pointX, pointY, colColor, strText)
draw.SimpleTextOutlined(strText, hFont, pointX, pointY, colColor, bCenter, bCenter, 1, Color(20, 20, 20, 200));
return GetTextWidth(hFont, strText);
end
function DrawStringBF4(hFont, hFontOutline, bCenter, pointX, pointY, colColor, strText)
surface.SetFont(hFont);
local iWidth, iHeight = surface.GetTextSize(strText);
if (bCenter == TEXT_ALIGN_CENTER) then
iWidth = iWidth / 2;
iHeight = iHeight / 2;
elseif (bCenter == TEXT_ALIGN_RIGHT) then
iHeight = 0;
else
iWidth = 0;
iHeight = 0;
end
surface.SetTextColor(255, 255, 255, 255);
surface.SetTextPos(pointX - iWidth, pointY - iHeight);
surface.DrawText(strText);
surface.SetTextColor(colColor);
surface.SetTextPos(pointX - iWidth, pointY - iHeight);
surface.SetFont(hFontOutline);
surface.DrawText(strTex
Use DEFINE_BASECLASS( "baseclass_name" ) at the top of your file. This will create a loca variable BaseClass.
Should I chance baseclass_name to BaseClass?
No, change it to your baseclass definition you are using, like the gamemode you're basing yours off of.
The gamemode name? I don't understand, sorry.
Do you know what a baseclass is?
Not really, This is the first time I'm dealing with it.
Where are you trying to get that other CalcView function from? In other words, where is the CalcView( ply, origin, angles, ... ) located?
[CODE]function GM:CalcView( ply, origin, angles, fov, znear, zfar )
local Vehicle = ply:GetVehicle()
local Weapon = ply:GetActiveWeapon()
local view = {}
view.origin = origin
view.angles = angles
view.fov = fov
view.znear = znear
view.zfar = zfar
view.drawviewer = false
--
-- Let the vehicle override the view and allows the vehicle view to be hooked
--
if ( IsValid( Vehicle ) ) then return hook.Run( "CalcVehicleView", Vehicle, ply, view ) end
--
-- Let drive possibly alter the view
--
if ( drive.CalcView( ply, view ) ) then return view end
--
-- Give the player manager a turn at altering the view
--
player_manager.RunClass( ply, "CalcView", view )
-- Give the active weapon a go at changing the viewmodel position
if ( IsValid( Weapon ) ) then
local func = Weapon.CalcView
if ( func ) then
view.origin, view.angles, view.fov = func( Weapon, ply, origin * 1, angles * 1, fov ) -- Note: *1 to copy the object so the child function can't edit it.
end
end
return view
end[/CODE]
Where did you get that from? The base gamemode?
probably
[editline]26th June 2015[/editline]
Is there a wiki for base class? I can't find one.
[url]http://wiki.garrysmod.com/page/baseclass/Get[/url]
And "probably" doesn't really help. You should ALWAYS know where your code is coming from. In this case, gamemode_base.
[QUOTE=code_gs;48055407][url]http://wiki.garrysmod.com/page/baseclass/Get[/url]
And "probably" doesn't really help. You should ALWAYS know where your code is coming from. In this case, gamemode_base.[/QUOTE]
Of course but, in this case I'm just adapting off of an old F2S hud.
Even that gets its baseclass definition from somewhere. self.BaseClass does nothing when it's undefined.
For example where does F2S get it from?
[editline]26th June 2015[/editline]
It would help a lot.
I don't know what F2S is
Give me an example of anything, something quick.
[code]DEFINE_BASECLASS( "gamemode_base" )
function GM:OnViewModelChanged( vm, old, new )
local ply = vm:GetOwner()
ply:ChatPrint( "This is a really shitty example of baseclass usage." )
BaseClass.OnViewModelChanged( self, vm, old, new )
end[/code]
[url]https://github.com/garrynewman/garrysmod/blob/master/garrysmod/gamemodes/base/gamemode/shared.lua#L292[/url]
Bad example, but in whatever gamemode you use it in, it will print that message to the player, then execute the normal function. You can also get return values from BaseClass functions. Make sure you provide self as the first argument for functions in a table.
Ugh, this is confusing. I don't get why it gives me errors and then it magically works. Is that part of this?
It's probably out of luck that the baseclass was initialised on time, however, you still shouldn't be using self.BaseClass
How should I use it?
Read my example code
The example code doesn't really explain much to me.
[editline]27th June 2015[/editline]
I am looking at it and I'm think that BaseClass is like a framework? I tried doing "base_anim" that didn't work so good, so I tried gamemode_base and that didn't work either. So the base class is meant to be something with the base gamemode right? CalcView is based off of cl_calcview in the base gamemode. What do you think? I tried some other things aswell and it just doesn't make sense.
[editline]27th June 2015[/editline]
I'd really appreciate if someone would help me out, I'm looking to learn and I don't really like asking for help but I am very frustrated by this.
Anyone?
First off, you don't have to define 'self' in GM functions.
You can remove this bit of code.
[CODE]local self = GM or GAMEMODE;[/CODE]
In Lua when you use colon, it calls the function as a method call.
This means that when you call GM:CalcView(), it is the same as calling GM.CalcView(self).
Then, you're missing function arguments for GM:CalcView().
[img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/GM/CalcView]GM/CalcView[/url]
Simply put the functions arguments properly, and remove the baseclass thing as it's doing nothing useful.
Sorry, you need to Log In to post a reply to this thread.