> ## Documentation Index
> Fetch the complete documentation index at: https://devdocs.xbox.com/llms.txt
> Use this file to discover all available pages before exploring further.

# 使用 Azure Functions 內容模型的 PlayFab CloudScript

> 當指令碼透過 ExecuteFunction、已排程的工作、玩家或實體執行時，公開給 Azure Functions 的 PlayFab CloudScript 內容模型參考。

# 教學課程：使用 CloudScript 內容模型

PlayFab 透過多種機制執行指令碼，包括透過 API 執行、透過已排程的工作執行、透過 PlayStream 事件執行，以及在玩家進入和離開區段時執行。在許多情況下，指令碼執行時所處的內容對其執行方式很重要。例如，需要知道代表哪位玩家執行指令碼的玩家識別碼。指令碼執行時所處的內容決定了可用的資料模型，並提供指令碼中所使用的內容特定資料。

在本教學課程中，您將了解如何：

<Note>
  \[!div class="checklist"]
</Note>

>

<Note>
  * 使用共用內容模型 \* 透過 ExecuteFunction API 執行時使用內容模型。\* 透過已排程的工作執行時使用內容模型。\* 在玩家的內容中執行時使用內容模型。\* 在實體的內容中執行時使用內容模型。
</Note>

## 使用共用遊戲驗證內容模型

無論執行指令碼的方法為何，一律會提供遊戲驗證內容。這包括用來執行指令碼的遊戲識別碼和實體權杖 (如需詳細資料，請參閱 [GetEntityToken](https://learn.microsoft.com/en-us/rest/api/playfab/authentication/authentication/get-entity-token))。了解此內容可讓您在指令碼中使用伺服器 API 對 PlayFab 進行其他 API 呼叫。

```C# theme={null}
// Shared models
public class TitleAuthenticationContext
{
    public string Id { get; set; }
    public string EntityToken { get; set; }
}
```

## 透過 ExecuteFunction API 執行時使用內容模型

當您使用 [ExecuteFunction API](https://learn.microsoft.com/en-us/rest/api/playfab/cloudscript/server-side-cloud-script/execute-function) 執行指令碼時，提供的內容包括下列資訊：

* 呼叫端的實體設定檔
* 遊戲驗證內容
* 表示是否在執行函式時傳送 PlayStream 事件的布林值
* 呼叫指令碼時使用的函式引數

```C# theme={null}
// Models via ExecuteFunction API
public class FunctionExecutionContext<T>
{
    public PlayFab.ProfilesModels.EntityProfileBody CallerEntityProfile { get; set; }
    public TitleAuthenticationContext TitleAuthenticationContext { get; set; }
    public bool? GeneratePlayStreamEvent { get; set; }
    public T FunctionArgument { get; set; }
}

public class FunctionExecutionContext : FunctionExecutionContext<object>
{
}
```

## 透過已排程的工作執行時使用內容模型

當您使用[已排程的工作](/zh-TW/services/playfab/data-analytics/acting-data/scheduled-tasks)執行指令碼時，提供的內容包括下列資訊：

* 已排程工作的名稱識別碼
* 包含 PlayStream 事件堆疊的事件歷程記錄
* 遊戲驗證內容
* 表示是否在執行函式時傳送 PlayStream 事件的布林值
* 呼叫指令碼時使用的函式引數

```C# theme={null}
// Models via Scheduled task
public class PlayStreamEventHistory
{
    public string ParentTriggerId { get; set; }
    public string ParentEventId { get; set; }
    public bool TriggeredEvents { get; set; }
}

public class ScheduledTaskFunctionExecutionContext<T>
{
    public PlayFab.CloudScriptModels.NameIdentifier ScheduledTaskNameId { get; set; }
    public Stack<PlayStreamEventHistory> EventHistory { get; set; }
    public TitleAuthenticationContext TitleAuthenticationContext { get; set; }
    public bool? GeneratePlayStreamEvent { get; set; }
    public T FunctionArgument { get; set; }
}

public class ScheduledTaskFunctionExecutionContext : ScheduledTaskFunctionExecutionContext<object>
{
}
```

## 在玩家的內容中執行時使用內容模型

透過玩家 PlayStream 事件、進入或離開區段，或作為以區段為基礎之已排程工作的一部分執行指令碼時，提供的內容包括下列資訊：

* 玩家設定檔
* 表示玩家設定檔是否遭到截斷的布林值。
  * 如果玩家設定檔超過 2048 個位元組，就會遭到截斷。如果發生這種情況，您需要使用設定檔 API (伺服器、用戶端或實體 API) 來擷取完整的設定檔。
* 觸發指令碼的 PlayStream 事件。
* 表示是否在執行函式時傳送 PlayStream 事件的布林值
* 呼叫指令碼時使用的函式引數

```C# theme={null}
// Models via Player PlayStream event, entering or leaving a 
// player segment or as part of a player segment based scheduled task.
public class PlayerPlayStreamFunctionExecutionContext<T>
{
    public PlayFab.CloudScriptModels.PlayerProfileModel PlayerProfile { get; set; }
    public bool PlayerProfileTruncated { get; set; }
    public PlayFab.CloudScriptModels.PlayStreamEventEnvelopeModel PlayStreamEventEnvelope { get; set; }
    public TitleAuthenticationContext TitleAuthenticationContext { get; set; }
    public bool? GeneratePlayStreamEvent { get; set; }
    public T FunctionArgument { get; set; }
}

public class PlayerPlayStreamFunctionExecutionContext : PlayerPlayStreamFunctionExecutionContext<object>
{
}
```

## 透過實體 PlayStream 事件、進入或離開實體區段，或作為以實體區段為基礎之已排程工作的一部分執行時使用內容模型

透過實體 PlayStream 事件、進入或離開實體區段，或作為以實體區段為基礎之已排程工作的一部分執行指令碼時，提供的內容包括下列資訊：

* 實體設定檔
* 觸發指令碼的 PlayStream 事件。
* 表示是否在執行函式時傳送 PlayStream 事件的布林值

```C# theme={null}
// Models via Entity PlayStream event, entering or leaving an 
// entity segment or as part of an entity segment based scheduled task.
public class EventFullName
{
    public string Name { get; set; }
    public string Namespace { get; set; }
}

public class OriginInfo
{
    public string Id { get; set; }
    public DateTime? Timestamp { get; set; }
}

public class EntityPlayStreamEvent<T>
{
    public string SchemaVersion { get; set; }
    public EventFullName FullName { get; set; }
    public string Id { get; set; }
    public DateTime Timestamp { get; set; }
    public PlayFab.CloudScriptModels.EntityKey Entity { get; set; }
    public PlayFab.CloudScriptModels.EntityKey Originator { get; set; }
    public OriginInfo OriginInfo { get; set; }
    public T Payload { get; set; }
    public PlayFab.ProfilesModels.EntityLineage EntityLineage { get; set; }
}

public class EntityPlayStreamEvent : EntityPlayStreamEvent<object>
{
}

public class EntityPlayStreamFunctionExecutionContext<TPayload, TArg>
{
    public PlayFab.ProfilesModels.EntityProfileBody CallerEntityProfile { get; set; }
    public EntityPlayStreamEvent<TPayload> PlayStreamEvent { get; set; }
    public TitleAuthenticationContext TitleAuthenticationContext { get; set; }
    public bool? GeneratePlayStreamEvent { get; set; }
    public TArg FunctionArgument { get; set; }
}

public class EntityPlayStreamFunctionExecutionContext : EntityPlayStreamFunctionExecutionContext<object, object>
{
}
```

<Note>
  您可以下載完整的[使用 Azure Functions 的 CloudScript 協助程式類別](https://github.com/PlayFab/PlayFab-Samples/blob/master/Samples/CSharp/AzureFunctions/CS2AFHelperClasses.cs)。
</Note>


## Related topics

- [使用 Azure Functions 的 PlayFab CloudScript 快速入門指南](/zh-TW/services/playfab/live-service-management/service-gateway/automation/cloudscript-af/quickstart.md)
- [使用 Azure Functions 的 PlayFab CloudScript](/zh-TW/services/playfab/live-service-management/service-gateway/automation/cloudscript-af/index.md)
- [PlayFab 即時服務管理文件](/zh-TW/services/playfab/live-service-management/index.md)
- [在 Visual Studio Code 中偵錯使用 Azure Functions 的 CloudScript](/zh-TW/services/playfab/live-service-management/service-gateway/automation/cloudscript-af/debugging-with-CloudScript-AF-VSCode.md)
- [CloudScript](/zh-TW/services/playfab/live-service-management/service-gateway/automation/cloudscript/index.md)
