• Trying to display text above entity (Garrysmod - lua)
    7 replies, posted
So, I'm trying to display a text above a entity. Kinda like the drug mods used in DarkRP. But the code i created won't work. No text appears. I get these errors : [CODE] ERROR: Trying to derive entity basic_entity_tutorial from non existant entity base_gmodentity! [/CODE] Here's my code : File name : basic_entity_tutorial.lua [CODE]AddCSLuaFile() ENT.Type = "anim" ENT.Base = "base_gmodentity" ENT.PrintName = "Basic Entity" ENT.Author = "Happy Crayfish" ENT.Purpose = "Does Something" ENT.Instructions = "Do something with me" ENT.Spawnable = true ENT.AdminSpawnable = true function ENT:Initialize() self:SetModel("models/props_c17/consolebox01a.mdl") self:SetHealth(10) self:SetSolid(2) self:SetMoveType(MOVETYPE_VPHYSICS) local phys = self:GetPhysicsObject() if (phys:IsValid()) then phys:Wake() end end function ENT:Draw() self:DrawModel() surface.CreateFont( "TheDefaultSettings", { font = "Arial", size = 500, weight = 500, blursize = 0, scanlines = 0, antialias = true, underline = false, italic = false, strikeout = false, symbol = false, rotary = false, shadow = false, additive = false, outline = false, } ) cam.Start3D2D( Vector(0, 0, 0), Angle(0, 0, 0), 1 ) surface.DrawText("Your Mother", "TheDefaultSettings", 0, 0, Color(255, 255, 255), TEXT_ALIGN_CENTER ) cam.End3D2D() end function ENT:Use(ply) if (ply:IsPlayer()) then ply:Kill() MsgN("Good job... You killed yourself!") end end[/CODE] And here's the init.lua file : (posting the required parts only) [CODE]hook.Add("ShowTeam", "SpawnOurEnt", function(ply) local ent = ents.Create("basic_entity_tutorial") ent:SetPos(ply:GetPos() +Vector(0, 0, 100)) ent:Spawn() ent:Activate() end)[/CODE] This is also my first time posting here. Sorry if i did something wrong.
Putting Vector( 0, 0, 0 ) will show it on 0, 0, 0 on the map.
As Neth stated, putting in Vector( 0, 0, 0 ) will put it on the map, so you'll want to replace it with self:GetPos(). [code]cam.Start3D2D( self:GetPos(), Angle(0, 0, 0), 1 )[/code] Also, the font you're creating should NEVER be done in a HUD or Draw function, since these are called every frame and will make your game/server become a very sad one. It'd be better to define it outside of the function, but even better if you kept all font creation to a file, if it's a gamemode/addon. Finally, to solve your error, given you followed my tutorial, and I fucked up in it, the base entity is actually just 'base_entity' [code]ENT.Base = "base_entity";[/code]
[QUOTE=CosmicSeagull;48234823]As Neth stated, putting in Vector( 0, 0, 0 ) will put it on the map, so you'll want to replace it with self:GetPos(). [code]cam.Start3D2D( self:GetPos(), Angle(0, 0, 0), 1 )[/code] Also, the font you're creating should NEVER be done in a HUD or Draw function, since these are called every frame and will make your game/server become a very sad one. It'd be better to define it outside of the function, but even better if you kept all font creation to a file, if it's a gamemode/addon. Finally, to solve your error, given you followed my tutorial, and I fucked up in it, the base entity is actually just 'base_entity' [code]ENT.Base = "base_entity";[/code][/QUOTE] So, i changed the position of it, went into garrysmod, spawned the entity but no text anywhere. I also changed my font size to 12 instead of 500. EDIT : I fixed the CreateFont error. But there is still no text anywhere However my other error was solved! :D
you can't use surface/draw functions outside of a draw hook
[QUOTE=tyguy;48236609]you can't use surface/draw functions outside of a draw hook[/QUOTE] So, my draw function now looks like this : (with a doNotCreateFontAgain variable online 1 of the file) [CODE]function ENT:Draw() if (doNotCreateFontAgain == false) then surface.CreateFont( "TheDefaultSettings", { font = "Arial", size = 12, weight = 500, blursize = 0, scanlines = 0, antialias = true, underline = false, italic = false, strikeout = false, symbol = false, rotary = false, shadow = false, additive = false, outline = false, } ) doNotCreateFontAgain = true end self:DrawModel() cam.Start3D2D( self:GetPos(), Angle(0, 0, 0), 1 ) surface.DrawText("Text", "TheDefaultSettings", 0, 0, Color(255, 255, 255), TEXT_ALIGN_CENTER ) cam.End3D2D() end[/CODE] But there is still no text anywhere. :/
Why aren't you just putting the font creation outside of the function? You're making it hard for yourself.
[QUOTE=mib999;48236727]Why aren't you just putting the font creation outside of the function? You're making it hard for yourself.[/QUOTE] It wouldn't work, so i just did it like this instead... :3
Sorry, you need to Log In to post a reply to this thread.