Thunder
#1
Anyone know why cWorld:GetNumPlayers is commented out? It could be used to make thunder work correctly.

int NumPlayers = GetNumPlayers();

class cThunderCaster : public cPlayerListCallback
{
	int m_PlayerToCastThunderAt;
	int m_CurrentPlayer;
	
	virtual bool Item(cPlayer * a_Player)
	{
		if (m_CurrentPlayer != m_PlayerToCastThunderAt)
		{
			return false;
		}
		
		// Cast thunder somewhere around the player
		return true;
	}
	
	public:
		cThunderCaster(int a_PlayerToCastThunderAt) :
			m_PlayerToCastThunderAt(a_PlayerToCastThunderAt),
			m_CurrentPlayer(0)
		{}
} ThunderCaster(GetTickRandomNumber(NumPlayers));

ForEachPlayer(ThunderCaster);
Reply
Thanks given by:
#2
Its commented out because it encourages dangerous code. For example the code given has a chance of not casting any thunder at all.
Reply
Thanks given by:
#3
It's mostly due to multithreading - it was causing deadlocks when used, and once you got your answer you still wouldn't know any better, because players may have disconnected from another thread in the meantime.

How about adding cWorld:DoWithRandomPlayer(a_Callback)?
Reply
Thanks given by:
#4
That's two specific, How about DoWithPlayerList?
Reply
Thanks given by:
#5
Good enough, except for the name. It's not a list. How about simple DoWithPlayers() ?
Reply
Thanks given by:
#6
Too similar to DoWithPlayer. And its a list of things, even if it's not a linked list, c++ is about the only language where list automatically means linked list.
Reply
Thanks given by:
#7
These kind of issues where a player disconnecting causing issues is confusing to me. In the environment I worked in we usually had methods that would search for a player so something like

Code:
local Player = game.Players:FindPlayer("Player")

Then the Player variable would store the player object then I can simply do
Code:
if Player then
--code
end

Is this not feasible with your API? This method of things seems a lot better instead of worrying about player disconnecting. Player will just become nil when the player disconnects. That if statement can handle the player disconnecting easily.
Reply
Thanks given by:
#8
Due to multithreading it cannot be so simple - the "player disconnecting" will be processed by a different thread running in parallel to your plugin, so the Player object can become invalid right after the IF has checked it to be valid, but before the --code executes:
-- Player is currently connected, PlayerObj is a valid object
if (PlayerObj) then
  -- Player disconnects right here
  -- PlayerObj becomes invalid
  PlayerObj:SendMessage("...")  -- fails - PlayerObj invalid, despite the condition
end
This is the price we pay for being multithreaded - we do need to use the DoWith and ForEach calls. Those guarantee that the object is valid as long as the callback is executing (but, again, you cannot store the object to be used later after the call).
Reply
Thanks given by:
#9
(05-13-2015, 06:40 AM)xoft Wrote: Due to multithreading it cannot be so simple - the "player disconnecting" will be processed by a different thread running in parallel to your plugin, so the Player object can become invalid right after the IF has checked it to be valid, but before the --code executes:
-- Player is currently connected, PlayerObj is a valid object
if (PlayerObj) then
  -- Player disconnects right here
  -- PlayerObj becomes invalid
  PlayerObj:SendMessage("...")  -- fails - PlayerObj invalid, despite the condition
end
This is the price we pay for being multithreaded - we do need to use the DoWith and ForEach calls. Those guarantee that the object is valid as long as the callback is executing (but, again, you cannot store the object to be used later after the call).

I see, what about using pcall? This way your code won't break the whole script and if it errors out anywhere in it, it can be handled? If used rarely then I can see it working fine. I know pcall is relatively slow compared to regular function calls.
Reply
Thanks given by:
#10
The problem is that the PlayerObj is invalid, but not nil, so accessing it will hard-crash the server. The underlying C++ cPlayer object is gone, but the Lua object is still pointing to the memory it used to occupy.
Reply
Thanks given by:




Users browsing this thread: 3 Guest(s)