# Context

Context is the object provided in the function requirement using the Crafttweaker integration.

It contains various methods for interacting with the machine.

### Methods

<details>

<summary>Set the result of the function</summary>

Every function **MUST** return a result, which is either a success or an error.

```javascript
//Get a success result
ctx.success();

//Get an error result
ctx.error("Some error message");
```

</details>

<details>

<summary>Get the remaining crafting time</summary>

#### Name : `remainingTime`

#### Description :

Return a decimal value that represent the time in ticks before the end of the current recipe process.\
This value does not take into account any speed modifiers applied to the machine.

Note : This method won't work when used inside `requireFunctionToStart`, the other 3 function requirements are fine to use.

#### Example

```java
.requireFunctionEachTick((ctx as Context) => {
		var time = ctx.remainingTime;
                //Use time here
	})
```

</details>

<details>

<summary>Get the crafting process base speed</summary>

#### Name : `baseSpeed`

#### Description :

Return a decimal value that represent the base speed (before upgrades are applied) of the crafting process.

By default it returns 1

Note : This method won't work when used inside `requireFunctionToStart`, the other 3 function requirements are fine to use.

#### Example

```java
.requireFunctionEachTick((ctx as Context) => {
		var baseSpeed = ctx.baseSpeed;
                //Use baseSpeed here
	})
```

</details>

<details>

<summary>Set the crafting process base speed</summary>

#### Name : `baseSpeed`

#### Description :

Set a decimal value as the base speed (before upgrades are applied) of the crafting process.

The new speed must not be negative.

Note : This method won't work when used inside `requireFunctionToStart`, the other 3 function requirements are fine to use.

#### Example :

```java
.requireFunctionOnStart((ctx as Context) => {
		ctx.baseSpeed = 2;
	})
```

</details>

<details>

<summary>Get the crafting process modified speed</summary>

#### Name : `modifiedSpeed`

#### Description :

Return a decimal value that represent the modified speed (after upgrades are applied) of the crafting process.

Note : This method won't work when used inside `requireFunctionToStart`, the other 3 function requirements are fine to use.

#### Example

```java
.requireFunctionEachTick((ctx as Context) => {
		var modifiedSpeed = ctx.modifiedSpeed;
                //Use modifiedSpeed here
	})
```

</details>

<details>

<summary>Get the machine as a tile entity</summary>

#### Name : `tile`

#### Description :

Return a [BlockEntity](https://docs.blamejared.com/1.18/en/vanilla/api/block/entity/BlockEntity), you can use it to get various information about the machine, see the linked CT wiki page.

#### Example

```java
.requireFunctionEachTick((ctx as Context) => {
		var tile = ctx.tile;
                //Use tile here
	})
```

</details>

<details>

<summary>Get the machine as a custom machine</summary>

#### Name : `machine`

#### Description :

Return a [Machine](https://frinn.gitbook.io/custom-machinery-1.19/mod-integrations/crafttweaker/recipes/function/machine), you can use it to check, input and output energy, fluids and items from/to the machine.

#### Example

```java
.requireFunctionEachTick((ctx as Context) => {
		var machine = ctx.machine;
                //Use machine here
	})
```

</details>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://frinn.gitbook.io/custom-machinery-1.19/mod-integrations/crafttweaker/recipes/function/context.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
