local function HidePlayer(playingas)
	addHook("PreThinkFrame", function()
		for player in players.iterate
			if(BonkerBox_G.IsPlayingAs(player,playingas)~=true)
				continue
			end
			
			if(BonkerBox_G.IsTable(player[playingas])~=true)
				player[playingas] = {}
			end
		
			//if renderflags was altered by this script at the end of the last frame,
			//un-alter the renderflags
			//it's important we're doing this in prethinkframe because this will
			//restore the hardcode state of .renderflags no matter what that value
			//is intended to be on this frame (even taking into consideration lingering
			//changes to the valye in hardcode from several frames ago)
			//if we actually want these flags to apply to rendering, then no problem,
			//they will simply be set all over again in postthinkframe if we're still
			//playing as the affected racer
			if(player[playingas].hideplayer_record~=nil)
				player.mo.renderflags = $ &~RF_DONTDRAW
				player[playingas].hideplayer_record = nil
				
			end
			
			if(player[playingas].hideskyboxplayer_record~=nil)
				player.mo.renderflags = $ &~RF_HIDEINSKYBOX
				player[playingas].hideskyboxplayer_record = nil
			end
			
			player[playingas].invisdefault = player.mo.renderflags & RF_DONTDRAW ~=0
			player[playingas].invisskybox = player.mo.renderflags & RF_HIDEINSKYBOX ~=0
		end
	end)

	//"all functions bound to the hook are executed in the order that they were added" - https://wiki.srb2.org/wiki/Lua/Hooks
	//this means it is okay to have both this and 'main shit' logic in PostThinkFrame so long as the latter is added after the former
	addHook("PostThinkFrame", function()
		for player in players.iterate
			if(BonkerBox_G.IsPlayingAs(player,playingas,replayaverse)~=true)
				continue
			end
			
			if(BonkerBox_G.IsTable(player[playingas])~=true)
				player[playingas] = {}
			end
			
			//set renderflags only if hardcode or some other lua script hasn't already set them
			//it's important we're doing this in postthinkframe as this will definitely execute
			//at the least after hardcode has done any operations it needs to on player.mo.renderflags
			if(player.mo.renderflags & RF_DONTDRAW ==0)
				player.mo.renderflags = $ | RF_DONTDRAW
				player[playingas].hideplayer_record=true
			end
			
			if(player.mo.renderflags & RF_HIDEINSKYBOX ==0)
				player.mo.renderflags = $ | RF_HIDEINSKYBOX
				player[playingas].hideskyboxplayer_record=true
			end
		end
	end)
end

local bbl = {}
bbl.HidePlayer = HidePlayer
rawset(_G,"BonkerBox_L",bbl)