Lua ConVar Questions

Status
Not open for further replies.
Alright, basically I'm trying to make a little Lua script to draw custom HUD at a user-specified scale, but I've run into some issues, so I have 2 questions.

1 - How do I set a "float" console variable's default value to a decimal number? If I type 1, it becomes 1, of course. But if I type 0.75 as the default value, Lua complains. If I type 3/4, it turns into 0. If I type simply FRACUNIT, Lua crashes SRB2 about 65536 being too high a number (as I've set the minimum and maximum to 0 and 1, respectively), so in no way can I use 3*FRACUNIT/4 for it.

2 - How do I properly acquire a console variable's value and then store it as a different variable... within a HUD function thing? For a while when I tried, SRB2 exclaimed "warning, attempted to call something on an upvalue (userdata structure)", and then later after some more messing around with the script (adding ".value" at the end of the equals thing), it started calling it a nil value instead of an "upvalue".


So far, I'm only trying to draw a single graphic on-screen with very basic set-up and stuff before trying to go advanced with it, just to get a hang of it. And here is the script, in case you want to see it to help:
Code:
//Initialize console stuff
COM_AddCommand("zhud",function(player) CONS_Printf(player,"ZHud by Zwip-Zwap Zapony\nList of variables:\nzhudsize <0.0-1.0>\nzhudsplitsize <0.0-1.0>\nzhudabilitybar <off/on/always>") end)
local zhudcvarsize=CV_RegisterVar({"zhudsize",1,CV_FLOAT,{MIN=0,MAX=1}})
local zhudcvarsplitsize=CV_RegisterVar({"zhudsplitsize",1,CV_FLOAT,{MIN=0,MAX=1}})
local zhudcvarabilitybar=CV_RegisterVar({"zhudabilitybar",0,0,{Off=0,On=1,Always=2}})

//Disable vanilla HUD
hud.disable("coopemeralds")
hud.disable("lives")
hud.disable("nightsdrill")
hud.disable("nightslink")
hud.disable("nightsrecords")
hud.disable("nightsrings")
hud.disable("nightsscore")
hud.disable("nightstime")
hud.disable("rankings")
hud.disable("rings")
hud.disable("score")
hud.disable("stagetitle")
hud.disable("tabemblems")
hud.disable("textspectator")
hud.disable("time")
hud.disable("tokens")

//Draw own HUD
hud.add(function(v,player)

//Sized co-ordinate stuff
if splitscreen==0
    local zhudvarsize=zhudcvarsize.value
    local zhudvartop=0
    local zhudvarbottom=200*FRACUNIT
    local zhudvarcenterv=100*FRACUNIT
    local zhudvarleft=0
    local zhudvarright=320*FRACUNIT
    local zhudvarcenterh=160*FRACUNIT
else
    local zhudvarsize=zhudcvarsplitsize.value
    if #player==0
        local zhudvartop=0
        local zhudvarbottom=100*FRACUNIT
        local zhudvarcenterv=50*FRACUNIT
    else
        local zhudvartop=100*FRACUNIT
        local zhudvarbottom=200*FRACUNIT
        local zhudvarcenterv=150*FRACUNIT
    end
    local zhudvarleft=0
    local zhudvarright=320*FRACUNIT
    local zhudvarcenterh=160*FRACUNIT
end

//Lives
if G_GametypeUsesLives() and player.mo and player.mo.skin
    v.drawScaled(zhudvarleft+(16*zhudvarsize),zhudvarbottom-(24*zhudvarsize),zhudvarsize,v.cachePatch("LIVSONIC"),V_HUDTRANS,v.getColormap(player.mo.skin,player.skincolor))
end

end)
The variable in question SRB2 says is nil is "zhudvarsize", at the v.drawScaled line, but "zhudvarsize" should be set by the splitscreen-checking part above it.
(Also, yes, I know the v.drawScaled line is missing the snap to left and bottom flags, but again, I'm just trying to get a hang of it before going advanced.)
 
Last edited:
I would go about it this way: The user types in 1 as the value, but the system reads it as 100. So if the user would type in 0.75, the sytem would read it as 75. Then you could do the calculations within the script internally without having to use the exact same value that the user types in.
 
I would go about it this way: The user types in 1 as the value, but the system reads it as 100. So if the user would type in 0.75, the sytem would read it as 75. Then you could do the calculations within the script internally without having to use the exact same value that the user types in.
The biggest problem right now is I can't even read and store the value properly. If I will be told how to do that, I could also find some work-around thing for the float value thingie. But thanks for the suggestion, however.
(I was actually originally using the whole numbers 1-100, until I remembered console variables can be floats, which are even in the range of 0-FRACUNIT so I don't have to multiply and divide the value while reading it.)
 
Code:
COM_AddCommand("example", function(player, val)
	if not val
		return
	end
	if val
	and tonumber(val) != nil
		player.value = val * 100
	end
end)

addHook("ThinkFrame", do
	for player in players.iterate
		if player.value != nil
			print(player.value)
		end
	end
end)

This is tested and fully working :). This desmonstrates the ability to hand over variables from one function to another, as I had to do with Blendcolor. I'm sure you'll also find a good use for it. I put in the console "example 1" and, sure enough, it printed "100" with no problem. Of course you'd have to put in some checks to make sure that it's only numbers, but you get the point.

Edit: "Only numbers allowed" included in the script.
 
Last edited:
I specifically want to use console variables (CV_RegisterVar), not console commands that set a player's variables, so people don't have to be in-game to use them and they don't maybe reset on forced map changes and/or exits and re-enters of games.
 
Status
Not open for further replies.

Who is viewing this thread (Total: 1, Members: 0, Guests: 1)

Back
Top