• failed to create the VGUI component (DImage)
    2 replies, posted
So, I'm working with DImage as you can probably tell, and I tried to store 20 different DImage frames on a table which brought no errors, except the message ONLY ONE TIME WHEN I FIRST START A SINGLE PLAYER GAME failed to create the VGUI component (DImage) I'd think it was because it was in a table, however if i open the code in my autorun folder and save it, it creates all 20 images and works perfectly as intended. Screenshot of it working as it should AFTER re running the code [img]http://i.imgur.com/K81hWSG.jpg[/img] Code: [CODE] local ply, health, armor, containers, pieces, GTH, prevPos local a, b, c local seconds = 0 local function SetHearts() heart_slots = {} heart_slots[1] = vgui.Create("DImage") heart_slots[2] = vgui.Create("DImage") heart_slots[3] = vgui.Create("DImage") heart_slots[4] = vgui.Create("DImage") heart_slots[5] = vgui.Create("DImage") heart_slots[6] = vgui.Create("DImage") heart_slots[7] = vgui.Create("DImage") heart_slots[8] = vgui.Create("DImage") heart_slots[9] = vgui.Create("DImage") heart_slots[10] = vgui.Create("DImage") heart_slots[11] = vgui.Create("DImage") heart_slots[12] = vgui.Create("DImage") heart_slots[13] = vgui.Create("DImage") heart_slots[14] = vgui.Create("DImage") heart_slots[15] = vgui.Create("DImage") heart_slots[16] = vgui.Create("DImage") heart_slots[17] = vgui.Create("DImage") heart_slots[18] = vgui.Create("DImage") heart_slots[19] = vgui.Create("DImage") heart_slots[20] = vgui.Create("DImage") end SetHearts() ply = LocalPlayer() for k, i in pairs(heart_slots) do i:SetImage("vgui/heartcontainers/container_1.png") i:SetSize(ScrW() / 55, ScrW() / 55 - 5) if k == 1 then prevPos = ScrW() / 55 i:SetPos( ScrW() / 40, ScrH() / 40 ) elseif k > 11 then i:SetPos( prevPos + ScrW() / 40, ScrH() / 40 * 2 ) prevPos = prevPos + ScrW() / 55 elseif k == 11 then prevPos = ScrW() / 55 i:SetPos( ScrW() / 40, ScrH() / 40 * 2 ) else i:SetPos( prevPos + ScrW() / 40, ScrH() / 40) prevPos = prevPos + ScrW() / 55 end end [/CODE] So just to tl;dr When i first start it doesnt work When i re run the code ingame it does
This is a common problem caused by the code being run before everything gets initialized. You have to run it a bit later using one of the many initialization hooks. Also, it's probably a better idea to use HUDPaint and DrawTexturedRect instead [editline]18th March 2017[/editline] [CODE] local heartmat = Material( "vgui/heartcontainers/container_1.png" ) local prevpos = 0 hook.Add( "HUDPaint", "DrawHearts", function() surface.SetDrawColor( 255, 255, 255, 255 ) surface.SetMaterial( heartmat ) local size = ScrW() / 55 for k = 1, 20 do if k == 1 then prevPos = ScrW() / 55 surface.DrawTexturedRect( ScrW() / 40, ScrH() / 40, size, size - 5 ) elseif k > 11 then surface.DrawTexturedRect( prevPos + ScrW() / 40, ScrH() / 40 * 2, size, size - 5 ) prevPos = prevPos + ScrW() / 55 elseif k == 11 then prevPos = ScrW() / 55 surface.DrawTexturedRect( ScrW() / 40, ScrH() / 40 * 2, size, size - 5 ) else surface.DrawTexturedRect( prevPos + ScrW() / 40, ScrH() / 40, size, size - 5 ) prevPos = prevPos + ScrW() / 55 end end prevpos = 0 -- reset it end ) [/CODE] [editline]18th March 2017[/editline] ^ This code is untested but is a bit cleaner and doesn't create a ton of vgui elements unnecessarily and should work without errors (hopefully) [editline]18th March 2017[/editline] Also, don't forget you can always multiply a number by k instead of adding to prevPos
Worked like a charm, Thanks a bunch. Also I'm kinda mad I didn't think of that, beforehand considering while waiting for a response i finished coding it, and it's a lot more complicated looking
Sorry, you need to Log In to post a reply to this thread.