trying to use draw.simpletext to print out a variable but it just says nil
11 replies, posted
I've got one hook that has a 'nanoshield' variable (different from the hook below but same lua file)
when I print that variable to console (from within the hook its in) it says it's 40
I'm trying to draw that value on the screen but it says the value is NIL in the hud
hook.Add( "HUDPaint", "HelloThere", function()
surface.SetDrawColor( 0, 0, 0, 128 )
surface.DrawRect( 50, 50, 128, 128 )
draw.SimpleText(nanoshield, "NaniteFont",100,100,bluenano,20,20)
end )
I think I'm not setting up the variable correctly or calling or correctly
The complete code is here if you are curious nanite shield testing
This looks like a classic case of referencing a variable that is out of scope.
Scope refers to the visibility of variables. In other words, which parts of your program can see or use it. To give some concrete examples, in your script the variable "healthamount" is declared at the top level of the script, which means any functions within that script can access it. Similarly with "bluenano". However the problematic variable "nanoshield" is only ever declared and set within your "ENT:StartTouch" function, as you pointed out in your follow up edit.
Since it is only ever set in that StartTouch, once you are outside of that function, that variable "leaves scope" and becomes nil, as if it was never set at all. Separate functions that try to reference it will throw an exception when this nil is used as if its a valid value which is what you're seeing here.
The first thing I would try is declaring nanoshield outside of StartTouch (even if you just set it to nil or don't set it at all) as this will place it in the global scope. This should at least cause your code to run as expected, however this will likely cause issues if you expect to use this in multiplayer, as all players would end up sharing the same "nanoshield" variable (I think, it has been a long time since I wrote Lua for Gmod). The solution to that is to store the nanoshield variable int the player table, which is a separate task.
Hey yeah that sounds right. I tried to set it at the 'top level' (I dunno what you'd call that) same as the healthamount variable. However, changes from within the starttouch function don't affect that variable. So if nanoshield is 40 inside starttouch it remains whatever the initial value is outside of it. I also tried not calling it 'local' and left it a global variable and that didn't help.
So the logic is as follows
nanoshield = 0
starttouch -> nanoshield = 40
starttouch -> print(nanoshield)
prints 40 to console
print(nanoshield)
prints 0 to console
I think I would need to do something like
hudshield = function ENT:StartTouch( activator, ent )
return nanoshield
end
so that the value of nanoshield became the value of the function? But if I try that it keeps telling me it's expecting parenthesis somewhere but won't tell me where.
Its giving you and error because you're declaring the function wrong, you have two names, you either do
function ENT:StartTouch(act,ent)
return nanoshield
end
or
ENT:StartTouch = function(act, ent)
return nanoshield
end
then just call `ENT:StartTouch()`
The function works but it's getting the variable out of it I can't figure out
just put a local nanoshield variable outside of all hooks and modify it within the hooks
I tried that but it doesn't get modified. I suppose if I could get hudpaint to work within starttouch then it wouldn't matter because that's where the variable is. But, then the hud doesn't get drawn for whatever reason. No errors just don't get drawn.
well firstly you're passing 20, 20 as the last two arguments of draw.SimpleText, which is wrong, but it might not effect this issue.
you have debug prints everywhere, do any of those print?
no actually the real issue is that the HUDPaint hook is only called is nanoshield is nil, which means nothing, which means the text is being drawn, but it's always nil.
I found that everything is working now except that the hook.Add( "HUDPaint", "nanohud", function () will not accept returned values.
I tried making a function that contains the nanoshield value called xy and it always returns the value. Below I call xy() and it's set so that if it does not receive a parameter then it should give me the nanoshield value. But it doesn't. dangus gets a nil value every time. But if I test out the code on lua.org it works fine. Calling xy() with a function or anywhere else in the code other than within a hook and the function returns the value. But if it's in the hook, doesn't work.
hook.Add( "HUDPaint", "nanohud", function ()
dangus = xy()
surface.SetDrawColor( 0, 0, 0, 128 )
surface.DrawRect( 50, 50, 128, 128 )
draw.SimpleText(dangus, "NaniteFont",100,100,bluenano,20,20)
end )
The code I tested on lua.org works fine and is here
function xy(int)
nanoshield = int
--store nanoshield value in texas so it can be retrieved
if int == nil then else
texas = nanoshield
end
return texas
end
--enter nanoshield value into the function as int
nanoshield = 25
xy(nanoshield)
--retrieve nanoshield value
dangus = xy()
print(dangus)
why would the function give you any value if you pass no value?
also what are you even trying to do with
if int == nil then else
you just need to read up on some programming stuff then some glua examples, because you're having a lot of issues with conditional logic, scope, and functions.
you can try it in Lua: demo if you confused
Sorry, you need to Log In to post a reply to this thread.