Simmo23

Get identifier from Entity

Recommended Posts

Hey guys,

I use c# with the bridge.

Is there any possibility to get an identifier from an Entity (Client, Vehicle)?.

I like to manage additional data for clients (level, money, ...) which are stored in a DB.  Just like that i like to manage additional data for vehicles later.

Once an event is triggerd by a client, i like to manipulate the additional data. So i have to create a connection from the Client object - which i got from the event -  and my User object. (Currently I'm using a dictionarylist with the client.Name as Key an my User object as Value and everytime i like to manipulate the data, i have to search for the client.Name as key in the List to get the User object). But if i think about the vehicles, I don't find any identifier.

When I remember SA:MP, everything had an identifier, which has never change since the creation.

 

What i've already tried:

I tried to create a sub class user with the Client as base. So the User has got all the properties from its base class and I can add additional properties. The problem i have related to the constructor of the base class.

Share this post


Link to post
Share on other sites

I find it easier to work with Entity Data. Take a look here:

https://wiki.gtanet.work/index.php?title=Getting_Started_with_EntityData

Whenever an a player joins, goes through Authentication and his data is retrieved from your storage (database) you can create an instance of a class you defined for your players with all that custom data. Then you can use entity data to attach the reference to that instance.

 

For example:

We have a class called account.

class Account {

	public string sqlId { set; get; }
	public string playerName { set; get; }
	public string money { set; get; }
  	...
}

 

When a players joins in the server, you go through your regular authentication routine. If all goes well, you then load that data into an instance of that Account class.

public static boolean AuthenticatePlayer(username, password, ....)
{

	// Authentication routine
	...
	// 
	
	Account playerAccountData = Database.RecoverAccount(sqlID, ...);
	NAPI.Data.SetEntityData(player, "MY_CUSTOM_PLAYER_DATA", playerAccountData);
	return true;
}

 

If at some point you wish to access that data

{
	.....

	if(NAPI.Data.HasEntityData(player, "MY_CUSTOM_PLAYER_DATA"))
	{
		Account account = NAPI.Data.GetEntityData(player, "MY_CUSTOM_PLAYER_DATA");
		account.money += 500;
		....
	}
	else
	{
		// No entity data: throw exception, error, message etc.
	}

	....
}

 

Edited by Static

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.