• New to Lua, trying to use Umsg, need some easy help!
    6 replies, posted
I'm trying to send a boolean from init to cl_init using Umsg. Part of init.lua code [CODE]function ENT:Break() umsg.Start("PrinterState") umsg.Bool(true) umsg.End() printTimeStart = 1000 printTimeEnd = 2000 GAMEMODE:Notify(self.dt.owning_ent, 0, 4, "Your basic money printer has clogged up.") if math.random(1, 11) == 3 then self:BurstIntoFlames() end end[/CODE] cl_init.lua code [CODE]include("shared.lua") function ENT:Initialize() end function my_message_hook( um ) isBroken = um:ReadBool(); end function ENT:Draw() self.Entity:DrawModel() local Pos = self:GetPos() local Ang = self:GetAngles() local owner = self.dt.owning_ent owner = (ValidEntity(owner) and owner:Nick()) or "unknown" surface.SetFont("HUDNumber5") local TextWidth = surface.GetTextSize("Basic Money Printer"); local TextWidth2 = surface.GetTextSize(owner); local TextWidth3 = surface.GetTextSize("Working"); Ang:RotateAroundAxis(Ang:Up(), 90) cam.Start3D2D(Pos + Ang:Up() * 11.5, Ang, 0.11) draw.WordBox(2, -TextWidth*0.5, -30, "Basic Money Printer", "HUDNumber5", Color(140, 0, 0, 100), Color(255,255,255,255)) draw.WordBox(2, -TextWidth2*0.5, 18, owner, "HUDNumber5", Color(140, 0, 0, 100), Color(255,255,255,255)) if isBroken == false then draw.WordBox(2, -TextWidth3*0.5, 56, "Working", "HUDNumber5", Color(140, 0, 0, 100), Color(0,255,0,255)) else draw.WordBox(2, -TextWidth3*0.5, 56, "Broken", "HUDNumber5", Color(140, 0, 0, 100), Color(255,0,0,255)) end cam.End3D2D() end function ENT:Think() end usermessage.Hook("PrinterState", my_message_hook)[/CODE] The printer is constantly displaying broken, when it shouldn't be. The Break function is rarely called, and it's sending false everywhere else. It's probably a simple mistake. I'm new to Lua and help would be appreciated, thank you :).
you have to define isBroken from what I can see, just put local isBroken at the top of cl_init
Yay, thank you. It worked.
I'm not absolutely sure if having a local at top of the code will work when you spawn another printer. They will share the same "isBroken" variable. Instead you should send self in the usermessage: [lua] umsg.Start("PrinterState") umsg.Entity(self) umsg.Bool(true) umsg.End() [/lua] Then on the client, do: [lua] function my_message_hook( um ) local self = um:ReadEntity() local broken = um:ReadBool() self.isBroken = broken end [/lua]
As a sidenote I suggest you read up more on code indentation, you did it mostly right but it could do with some polishing.
[QUOTE=Ylsid;35852611]As a sidenote I suggest you read up more on code indentation, you did it mostly right but it could do with some polishing.[/QUOTE] He made it "correct" in all places except two, why do you care, it looks like he knows how to indent, he probably just forgot it at that place.
[QUOTE=Donkie;35852910]He made it "correct" in all places except two, why do you care, it looks like he knows how to indent, he probably just forgot it at that place.[/QUOTE] Whoops, I misread the indentation as double tabs. :dumb:
Sorry, you need to Log In to post a reply to this thread.