GetAccountData: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
mNo edit summary
No edit summary
Line 1: Line 1:
__NOTOC__  
{{Server function}}
__NOTOC__
{{Note_box|It is strongly recommended that you use the standard ''module.key'' naming for your keys, as shown in the example below. This prevents collisions between different scripts.}}
{{Note_box|It is strongly recommended that you use the standard ''module.key'' naming for your keys, as shown in the example below. This prevents collisions between different scripts.}}
This function retrieves a string that has been stored using [[setAccountData]]. Data stored as account data is persistent across user's sessions and maps, unless they are logged into a guest account.
This function retrieves a string that has been stored using [[setAccountData]]. Data stored as account data is persistent across user's sessions and maps, unless they are logged into a guest account.
Line 15: Line 16:
Returns a [[string]] containing the stored data or ''false'' if no data was stored under that key.
Returns a [[string]] containing the stored data or ''false'' if no data was stored under that key.


==Example==  
==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.
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
root = getRootElement ()
function onPlayerQuit ( )
addEventHandler ( "onClientLogin", root, "onLogin" )
      -- 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 onLogin ( previous_account, current_account, auto-login )
function onPlayerJoin ( )
    playerName = getAccountData (current_account,"playerName"); -- Get the data stored in "playerName" and output it to a string
      -- when a player joins, retrieve his money amount from his account data and set it
    outputChatBox playerName .. " just logged in." -- Output the name to the chat box
      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
end


addEventHandler ( "onPlayerQuit", getRootElement ( ), onPlayerQuit )
addEventHandler ( "onPlayerJoin", getRootElement ( ), onPlayerJoin )
</syntaxhighlight>
</syntaxhighlight>


==See Also==
==See Also==
{{Account_functions}}
{{Account_functions}}

Revision as of 11:59, 21 August 2007

This template is no longer in use as it results in poor readability. This function retrieves a string that has been stored using setAccountData. Data stored as account data is persistent across user's sessions and maps, unless they are logged into a guest account.

Syntax

string getAccountData ( account theAccount, string key )

Required Arguments

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

Returns

Returns a string containing the stored data or false if no data was stored under that key.

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