Basic player position saving system?

So, I’m working on a certain roleplay gamemode, and the gamemode is missing a system where if a player logs out and back in, their character will start back at the spawn.

Does anyone have any theories on how to implement a basic position saving system?

“if a player logs out and back in, their character will start back at the spawn”

What do you mean?

Like, when someone logs out, I want their position to be saved so that next time they log in, they log in the place that they disconnected from.

You might want to take into concideration that if they log off and someone builds a house / buy the place they were in, and they log in, they could easly just go and walk out with all the goods…



//Pseudo



function CreateDB()

//write your DB with mysql
http://wiki.garrysmod.com/page/sql

end

function WriteinDB( ply, vector )
// write in DB
end

function Playerdisconnect(pl) // hook me

// call me if i say goodbye
WriteinDB( pl, pl:GetPos()+Vector(0,0,5)) // dont stuck me :D

end


greetz

|Royal|

The gamemode in question is ‘Vein’, a very clean gamemode to work on, but one of the tricky things I’ve come along is character position saving.

Any idea how I’d implement this with the current SQlite database it has?

Create an externally addon for it. Dont paste it in any Gamemode if you not the owner.

He can do what he wants to do on the gamemode but he cant republish it with changes or change the gamemode’s name.

[lua]
hook.Add( “PlayerDisconnected”, “Save Player Position”, function( ply )

if ply:Alive() and ply:Team() != TEAM_SPECTATOR then
	ply:SetPData( "lastPosition", ply:GetPos() )
else
	ply:SetPData( "lastPosition", "" )
end

end )

hook.Add( “PlayerSpawn”, “Get Old Player Position”, function( ply )

if not ply._firstSpawn then -- PlayerInitialSpawn is called when they connect to the server- not the first time PlayerSpawn is called. So we do this check.
	ply._firstSpawn = true
	local pdata = ply:GetPData( "lastPosition", "" )
	if pdata != "" then
		ply:SetPos( Vector(pdata) )
	end
end

end )[/lua]

Also, Royal, you don’t need to sign your posts. Your name is to the left of your post, we can see that just fine.

Wow, this is very helpful Shadow, thanks a ton!

Now, I’m thinking on how to implement this properly, would I create a new lua file that is called on gamemode init?

And I’m also going to try and make this work for each of a character’s position, instead of the entire player. (E.g rpchar1 is at a forest, and rpchar2 is somewhere else, and they can both make use of the position code.)

http://wiki.garrysmod.com/page/Hooks/Base/Initialize

What I mean is, my first instinct to do with this code is to shove it directly at the bottom of the gamemode init lua file, but I get a feeling that is a baaad idea.

So can someone clarify what I’d do with this code?

You should create a whole new file, imo. Just make it autorun on the server.

On 2nd thought, the bottom of my gamemode init has this code segement:


function GM:Initialize()
	
	game.ConsoleCommand( "net_maxfilesize 64
" );
	game.ConsoleCommand( "sv_kickerrornum 0
" );
	game.ConsoleCommand( "sv_autorefresh 0
" );
	game.ConsoleCommand( "lua_log_sv 1
" );
	
	V.ValidateDirectories();
	
	V.SQL.CheckMode();
	
	V.W.Initialize();
	
end


Would I try to insert the code into there?

EDIT: No that seems like a bad idea, I’l just put the hooks below that code and see how it goes.

[editline]9th January 2013[/editline]

Bingo! Thanks a ton for the code Shadow, but now I’m going to try and make it work with individual characters instead of the whole player.

A basic system is better than no system, so I’m grateful.

[editline]9th January 2013[/editline]

Ok, so this is some code I thought of using for a character’s position, now I know that the characters are using SQlite as the database.



function GM:PlayerDisconnect( ply )


    V.SQL.SaveCharacterField( ply, "SavedPos", ply:GetPos()+Vector(0,0,10) );
    V.PreLoadCharData( ply, V.SQL.CharID( ply ) );
    
end


And this:



 function V.LoadCharData( ply, d )
    
    if( !ply:Alive() ) then
        
        ply:Spawn();
        
    end