Random Chitchat 2012-2016
Since I'm not storing the Playername instead of the object I'm now using a callback to get the position of the wolfs owner.
Thanks given by:
I'm not exactly sure on how to solve this problem. Getting the player position by name sounds horribly inefficient, but storing a pointer can lead to dangling pointers...
One solution I know is using smart pointers, but usage of smart pointers seem to indicate bad software architecture.
Storing an entity ID, and using that to look up a player position should already be a lot faster, you could also use a (hash)map and it should be fast enough.
But that's all referencing the player indirectly.
Another solution could be to hook into some kind of callback that notifies you when an object is removed, so you can check whether it's the player you're pointing to and clear the reference.

I don't know the perfect solution to this, maybe xoft knows something that is more suitable.
Thanks given by:
FakeTruth Wrote:Storing an entity ID, and using that to look up a player position should already be a lot faster
If we do that then when you log out the wolf isn't your wolf anymore.

I've done a bit more with the wolves (still using callbacks though). I think it's pretty much done now.
Thanks given by:
I mean during runtime use the entity ID to look up the player, but when serializing you store the player name Wink
Thanks given by:
I'm not so sure using an EntityID would be faster:
- EntityID is matched against all entities in the world, requiring a walk of all the currently loaded chunks and comparison to all entities (including mobs, pickups etc.)
- PlayerName is checked against the playerlist only, without even traversing the chunkmap. As long as there arent' that many players connected, it might be quite a bit faster
Other thoughts later.

If the current code proves too inefficient, the first step against making it faster is to make the wolf "recalculate" its destination only once per N ticks, with N = 4 or more.

The best solution probably is to mix a bit of everything in. Store a cPlayer pointer for runtime; write a hook so that when the cPlayer object gets deleted, the wolf gets notified of this event. And also store the playername, so that it may be serialized to NBT and used to check the player when a new cPlayer object is created, and assign the cPlayer object back to the cWolf when appropriate.

It goes against the "single primary key" principle, but it's an optimization in the name of speed and I think it's easy to understand and should be relatively easy to code. Except for the callbacks, that is.
Thanks given by:
I was indeed thinking about storing both the playername and the object, but when the player leaves, is the player object empty or is it kinda there still? If it is still there wasn't there an "IsDestroyed" function where you could see if the object still exists?
I now have this:
if (IsTame())
	{
		if (m_OwnerName == "" && m_OwnerObject == NULL || m_OwnerObject->IsDestroyed())
		{
			class cCallback :
				public cPlayerListCallback
			{
				virtual bool Item(cPlayer * Player) override
				{
					OwnerObject = Player;
					return false;
				}
			public:
				cPlayer * OwnerObject;
			} Callback;
			m_World->DoWithPlayer(m_OwnerName, Callback);
			if (Callback.OwnerObject != NULL)
			{
				m_OwnerObject = Callback.OwnerObject;
				m_OwnerName = m_OwnerObject->GetName();
			}
		}
		else
		{
			Vector3f OwnerCoords = m_OwnerObject->GetPosition();
			double Distance = (OwnerCoords - GetPosition()).Length();
			if (Distance < 3)
			{
				m_bMovingToDestination = false;
			}
			else if ((Distance > 30) && (!IsSitting()))
			{
				TeleportToCoords(OwnerCoords.x, OwnerCoords.y, OwnerCoords.z);
			}
			else
			{
				m_Destination = OwnerCoords;
			}
		}
	}
But the server crashes when there is a tamed wolf.
Thanks given by:
The condition in the third line is unclear and probably buggy. Use parenthesis to indicate the proper order of evaluation, the OR versus the AND.

The IsDestroyed() function has a slightly different meaning. It returns false throughout the object's lifetime, until the Destroy() function is called. The Destroy() function doesn't destroy the object, it only sets a flag that the object should be destroyed (because other objects may be still referencing this player). Since then, the IsDestroyed() function returns true, although the object is still perfectly valid. Then in a well-defined point in time the object is actually deleted, and all calls to it will lead to undefined behavior (crash, if you're lucky).
Thanks given by:
So
if ((m_OwnerName == "" && m_OwnerObject == NULL) || m_OwnerObject->IsDestroyed())
should work?

I started debugging since It didn't work and it seems to crash when I use the IsDestroyed() function.

It even crashes when I use any function.

This is in the debug console thing
Code:
The thread 'Win32 Thread' (0xf48) has exited with code 0 (0x0).
The thread 'Win32 Thread' (0xe94) has exited with code 0 (0x0).
The thread 'Win32 Thread' (0x12f0) has exited with code 0 (0x0).
The thread 'Win32 Thread' (0x13c0) has exited with code 0 (0x0).
First-chance exception at 0x01031642 in MCServer.exe: 0xC0000005: Access violation reading location 0x000000b8.
Unhandled exception at 0x01031642 in MCServer.exe: 0xC0000005: Access violation reading location 0x000000b8.
Thanks given by:
Maybe m_OwnerObject is not initialized?

Anyway, perhaps we could add a tick delay before actually deallocating entities. If we add this delay any object referencing another entity can use the tick function to check whether the referenced entity is destroyed and remove the reference. You won't have to deal with callbacks and stuff, but you'll need to check the validity of your references each tick...
Thanks given by:
How about having the Player handle the ownership? You could have the player update a flag that notifies the wolf that it needs to change to a different state, and then when the player exits, you don't have to worry about a dangling pointer.

This also means that you wouldn't have to add a tick delay or any extra code like that.
Thanks given by:




Users browsing this thread: 13 Guest(s)