SetAccountData: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 18: Line 18:


==Example==  
==Example==  
On a roleplaying server, save the money of a player when he quits, so it can be loaded again when he comes back later.
For a pirate roleplaying gametype, the amount of money a player has is made persistent by storing it in his account. Note the code uses "piraterpg.money" as key instead of just "money", as the player may be participating in other gametypes that also save his money amount to his account. If both gametypes would use "money" as the account key, they'd overwrite each other's data.
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
function onPlayerQuit ( )
function onPlayerQuit ( )
      -- when a player leaves, store his current money amount in his account data
       local playeraccount = getClientAccount ( source )
       local playeraccount = getClientAccount ( source )
       local playermoney = getPlayerMoney ( source )
       if ( playeraccount ) then
      setAccountData ( playeraccount, "piraterpg.money", playermoney )
            local playermoney = getPlayerMoney ( source )
            setAccountData ( playeraccount, "piraterpg.money", playermoney )
      end
end
end


function onPlayerJoin ( )
function onPlayerJoin ( )
      -- when a player joins, retrieve his money amount from his account data and set it
       local playeraccount = getClientAccount ( source )
       local playeraccount = getClientAccount ( source )
       local playermoney = getAccountData ( playeraccount, "piraterpg.money" )
       if ( playeraccount ) then
      setPlayerMoney ( source, playermoney )
            local playermoney = getAccountData ( playeraccount, "piraterpg.money" )
            -- make sure there was actually a value saved under this key (check if playermoney is not false).
            -- this will for example not be the case when a player plays the gametype for the first time
            if ( playermoney ) then
                  setPlayerMoney ( source, playermoney )
            end
      end
end
end



Revision as of 11:59, 21 August 2007

This template is no longer in use as it results in poor readability. This function sets a string to be stored in an account. This can then be retrieved using getAccountData. Data stored as account data is persistent across user's sessions and maps, unless they are logged into a guest account.

Syntax

bool setAccountData ( account theAccount, string key, string value )

Required Arguments

  • theAccount: The account you wish to retrieve the data from.
  • key: The key under which you wish to store the data
  • value: The value you wish to store

Returns

Returns a true if the account data was set, false if an invalid argument was specified.

Example

For a pirate roleplaying gametype, the amount of money a player has is made persistent by storing it in his account. Note the code uses "piraterpg.money" as key instead of just "money", as the player may be participating in other gametypes that also save his money amount to his account. If both gametypes would use "money" as the account key, they'd overwrite each other's data.

function onPlayerQuit ( )
      -- when a player leaves, store his current money amount in his account data
      local playeraccount = getClientAccount ( source )
      if ( playeraccount ) then
            local playermoney = getPlayerMoney ( source )
            setAccountData ( playeraccount, "piraterpg.money", playermoney )
      end
end

function onPlayerJoin ( )
      -- when a player joins, retrieve his money amount from his account data and set it
      local playeraccount = getClientAccount ( source )
      if ( playeraccount ) then
            local playermoney = getAccountData ( playeraccount, "piraterpg.money" )
            -- make sure there was actually a value saved under this key (check if playermoney is not false).
            -- this will for example not be the case when a player plays the gametype for the first time
            if ( playermoney ) then
                  setPlayerMoney ( source, playermoney )
            end
      end
end

addEventHandler ( "onPlayerQuit", getRootElement ( ), onPlayerQuit )
addEventHandler ( "onPlayerJoin", getRootElement ( ), onPlayerJoin )

See Also