1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. GTA Network forum is now in archive mode.

Add a custom property to the Client class.

Discussion in 'Archive' started by Roel, Jan 26, 2017.

Thread Status:
Not open for further replies.
  1. Roel

    Roel Member

    Messages:
    26
    Likes Received:
    11
    Joined:
    Sep 14, 2016
    Hello,

    So I'm wondering if I can somehow detach a custom class to the existing class Client.
    I have a custom class Account that holds all database values for a player.
    It would be nice if I can access this in all methods that contain the Client class.

    Is there a way to do this? Or should I seek for the right Account object in each method?

    Thx!
     
  2. Amit_B

    Amit_B Member Language Board Moderator

    Messages:
    40
    Likes Received:
    21
    Joined:
    Oct 27, 2016
    As far as I know, currently you can't extend a class with custom variables.
    You can however use an extension method which is doing basically exactly what you want, just with a function instead of a variable.
    Take a look at a code from my script to understand how to do that:
    Code:
            /// <summary>
            /// An extension method used to find a player's ID.
            /// </summary>
            /// <param name="player">The <c>Client</c> element.</param>
            /// <returns>The player ID, or <c>Constants.INVALID_PLAYER_ID</c> if there is no ID.</returns>
            public static int GetID(this Client player)
            {
                return player.hasData(Global.EntityData.player_id) ? Convert.ToInt32(player.getData(Global.EntityData.player_id)) : Constants.INVALID_PLAYER_ID;
            }
    
    Note that extension methods can only be defined in a static class.
     
    501.legion likes this.
  3. MissMelissa

    MissMelissa New Member

    Messages:
    0
    Likes Received:
    0
    Joined:
    Dec 26, 2016
    Yeah, that suggestion sounds good. The more accurate answer would be to use entity data itself, which is the exact purpose of the entity data.
     
    Amit_B likes this.
  4. Roel

    Roel Member

    Messages:
    26
    Likes Received:
    11
    Joined:
    Sep 14, 2016
    Not sure if i'm misunderstanding , but here is what I would like to manage for example:
    Code:
    public void onPlayerDeath(Client player, NetHandle reason, int weapon)
    {
        player.account.deaths++;
    }
    
    So instead of searching through some list with all account objects which one belongs to this Client, it would be nice if I can do it on this way.
    But is it possible?
     
  5. MissMelissa

    MissMelissa New Member

    Messages:
    0
    Likes Received:
    0
    Joined:
    Dec 26, 2016
  6. Roel

    Roel Member

    Messages:
    26
    Likes Received:
    11
    Joined:
    Sep 14, 2016
    Okay, that page I needed, thanks!
     
Thread Status:
Not open for further replies.

Share This Page