Net images Help!

Hiy guys I have bee working with .net recently and generally getting to terms with using .net between the client and the server side. But I have come into a problem.
I am trying to display an image to the user when they join and then for it to disappear again after 4-5 seconds. Here is my method atm. Try not to judge my code to much but correct me if there is a better way I could do it.

Server side:




function GM:PlayerInitialSpawn( ply )


 util.AddNetworkString( "spawn5" )
	 net.Start("spawn5")
     net.WriteString("some text")
     net.Send(ply)


end

Clientside:

function GM:DrawEndHUD()

net.Receive( "spawn5", function( len, pl )
	if ( IsValid( pl ) and pl:IsPlayer() ) then
		print( "Message from " .. pl:Nick() .. " received. Its length is " .. tostring( len ) .. "." )
		
	else
		print( "Message from server received. Its length is " .. tostring( len ) .. "." )
		timer.Simple(1, function()
		
		local hpsplat = Material("Endgame.png")
surface.SetMaterial(hpsplat)
surface.SetDrawColor(225, 225, 225, 225 )
      surface.DrawTexturedRect(0, ScrH()-1080, 1920, 1100 )
  
		
		end)
	end
end ) 


end



Thankyou for any help and or advice

-Duby ^^

Don’t put the net.Receive inside the hud hook. Put it outside, an initialize a variable. PlayerInitialSpawn is called slightly before they are fully connected, somewhere around Sending Client Info.

Don’t use timers, use CurTime( ) to set a start time / expiry… Here: http://forum.facepunch.com/showthread.php?t=1378030&p=44306307&viewfull=1#post44306307

Similar principle. When it hits 0, close it. But make sure the player can see it before continuing. so, what you could do is call hook.Add inside the net.Receive for HUDPaint. If ( !IsValid( LocalPlayer( ) ) then return; end – At the end of the time, hook.Remove HUDPaint, unique name you gave when you hook.Added

[editline]21st March 2014[/editline]

SERVER:
[lua]util.AddNetworkString( “DisplaySomething” )
hook.Add( “PlayerInitialSpawn”, “DisplaySomething:InitSpawn”, function( _p )
net.Start( “DisplaySomething” );
net.Send( _p );
end );[/lua]

CLIENT:
[lua]local DisplaySomethingDuration = 5; // Display Something for 5 seconds.
local DisplaySomethingMaterial = Material( “Endgame.png” ); // Declare Material outside of hook/loops or face the wrath of memory leaks!
net.Receive( “DisplaySomething”, function( _len )
// Add a HUDPaint hook when the DisplaySomething net message is received
hook.Add( “HUDPaint”, “DisplaySomething:HUDPaint”, function( )
// Helper Variable
local _p = LocalPlayer( );

	// Wait till player is valid to itself
	if ( !IsValid( _p ) then return; end

	// Initialize the timer if it hasn't already been initialized
	if ( !_p.__DisplaySomethingTimer ) then _p.__DisplaySomethingTimer = CurTime( ); end

	// Deal with the length
	local _fraction = math.Clamp( math.TimeFraction( _p.__DisplaySomethingTimer, _p.__DisplaySomethingTimer + DisplaySomethingDuration, CurTime( ) ), 0, 1 );

	// Remove the Something it when the time is right - It's clamped so it'll never be < 0, however if you ever do something where you don't clamp, and it can be out of bounds, use a < or > to ensure code is executed.
	if ( _fraction <= 0 ) then
		hook.Remove( "HUDPaint", "DisplaySomething:HUDPaint" );
	end

	// Display Something...
	surface.SetMaterial( DisplaySomethingMaterial ); // Set the material
	surface.SetDrawColor( Color( 225, 225, 225, 225 ) ); // White ensures normal color
	surface.DrawTexturedRect( 0, ScrH() - 1080, 1920, 1100 ); // Draw the textured Rect
end );

end );[/lua]

That should do it. Look into this code for handling drawing scalable sizes based on screen size…

https://dl.dropboxusercontent.com/u/26074909/tutoring/vgui/understanding_hardcoding_of_screensizes.lua

I see ok thanks for the detail you gave me and the quick response I will have a go at it and see where I end up. :slight_smile:

[editline]21st March 2014[/editline]

Hmmm I am using this. It isn’t creating any errors but nothing is happening. I have extended the timer and fixed a syntax error


 'if ( !IsValid( _p ) )then return; end' 

But yet nothing is happening…

I have checked my image on my hud etc. and thats fine. But I can’t figure out why on earth nothing is happening.

Thanks Duby

It turns out I was using math.TimeFraction incorrectly or something. For some reason it wasn’t giving me desired results so I changed it back to the typical system of how I do things:
https://dl.dropboxusercontent.com/u/26074909/tutoring/vgui/display_something_on_initial_spawn.lua

I made a few minor changes to ensure the var is nulled when calling it, for the debug function. It now works properly. All that has to be changed is the config time to display, and the material in addition to the x, y, w, and h for the texture to be drawn.

I also redid the x, y and that so it’s not hard-coded. It’s in a configuration table to simplify things.

Wow I see thanks a lot for your time and effort with helping me with the matter!