[GS] How to call an Event in Lua (UI Context)?

__jack__

Warlord
Joined
Dec 29, 2009
Messages
141
Hi,

I get how to add / remove to an existing event using e.g. Events.PlayerTurnActivated.Add( OnPlayerTurnActivated ) and having a OnPlayerTurnActivated function in the UI file.

My question is how can I call an Event e.g. if I want to desactivate a player's turn using Events.PlayerTurnDeactivated ?

Can it be done ?

(What I am looking to do is stop an AI player from doing anything when the function is called)
 
Hook events are generally "listeners" in Civ6 lua. Your lua code does not "call" or "force" a Hook-Event to trigger nor can it -- it can only react when the desired event occurs. Only the gamecore / DLL can force a hook-event to trigger out to the lua system as a general rule.

Certain "force this to happen" things can be done in UI scripts after an event of some kind triggers, but the code in such cases is not directly attempting to call or force a PlayerTurnEnd or PlayerTurnDeactivated or other hook-event type to trigger out to the lua system. The code within the listener functions is instead reacting to the fact that a pre-determined lua hook-event triggered, and in a UI context can/is requesting an "operation" from the gamecore. This is what is happening in the UI ActionPanel file here:
Code:
-- ===========================================================================
--	Check if the turn can be automatically ended.
-- ===========================================================================
function CheckAutoEndTurn( eCurrentEndTurnBlockingType:number )
	local pPlayer = Players[Game.GetLocalPlayer()];
	if pPlayer ~= nil then
		if not m_unreadiedTurn -- If the player intentionally unreadied their turn, do not auto end turn.
			and eCurrentEndTurnBlockingType == EndTurnBlockingTypes.NO_ENDTURN_BLOCKING 
			and	(UserConfiguration.IsAutoEndTurn() and not UI.SkipNextAutoEndTurn())
			-- In tactical phases, all units must have orders or used up their movement points.
			and (not CheckUnitsHaveMovesState() and not CheckCityRangeAttackState()) 
			-- Not already send turn complete for this turn.
			and not UI.HasSentTurnComplete()
			-- Expansion content is ok with auto ending the turn.
			and AllowAutoEndTurn_Expansion() then 
				if not UI.CanEndTurn() then
					UI.DataError("CheckAutoEndTurn thinks that we can't end turn, but the notification system disagrees!");
				end
			UI.RequestAction(ActionTypes.ACTION_ENDTURN);
		end
	end
end

-- Overridable function for additional AutoEndTurn checks for expansion content.
-- Return true when the user should be able to auto end their turn based on additional expansion conditions.
function AllowAutoEndTurn_Expansion()
	return true;
end
The UI.RequestAction(Argument) is what is causing the end turn to occur, because it is sending an instruction (or request) to the game core / DLL to go ahead and end the turn. The gamecore / DLL is what is actually ending the turn.

The CheckAutoEndTurn() function is being "called" elsewhere within the file and is being passed the appropriate "EndTurnBlockingType" data from the origin-point wherever it may be "called" within the UI file. But some type of lua Hook-Event has to have already been triggered by the gamecore to the lua system before any of this can be executed by ActionPanel.lua.
 
Thanks leeS.

That was my understanding. Alternative Is there anyway to pause the code for a period of time?

Since I can’t call an event, just listen to them, one of my option would be to pause the code moving forward.

I am looking to lock the game when a player is dropping in MP sadly requesting a pause doesn’t work because the AI plays its turn regardless. So I want to freeze the lua from proceeding forward... any idea?
 
There is no direct command to pause an lua script so far as I know in the Civ6 implementation of lua,

Even if there was, this would not pause the game. The game's core engine does not operate on the lua engine. The lua engine is an extra system used by the game primarily for the purpose of creating an interface between the human player and what one would call "the game". All that creating a pause system for an lua User Interface script would do is pause that individual User Interface Script. In addition to this, to complicate the issue, Civ6 operates on two different processing threads -- "Game State" and User Interface. The "Game State" is the actual conditions of the game and its actual processing of what is happening with human and AI players. Then there is for your purposes the extra complication that AI players do not use User Interface -- their "actions" are all directly processed and executed in the "Game State" processing thread without need to refer to the User Interface processing thread for anything.

Lua "Gameplay Scripts" operate directly within the "Game State" processing thread, which is why nearly all the lua "Set" and "Change" methods are only valid in a "Gameplay Scripts" context. These types of scripts can directly affect what happens to both human and AI "stuff", but they do not without really complicated code exchange methods between the UI and Gameplay lua contexts refer to or ask for instructions in real time from any player, human or otherwise: all their "instructions" for any decision-making in a "Gameplay Script" are embedded inherently in the code of the lua file.
 
Last edited:
I see.

One solution I am thinking of then would be:
1. OnMultiplayerPrePlayerDisconnected( playerID ) => trigger a function saving all the position of the dropping player's unit
2. Preventing somehow the AI from finishing its turn. The Congress is doing this, isn't it ?
3. When the dropped player join revert the function to replace the units.

Quite crude. But it might work.
 
Back
Top Bottom