Skip to content

Commit

Permalink
Removed deprecated Molang queries
Browse files Browse the repository at this point in the history
  • Loading branch information
QuazChick committed Dec 3, 2024
1 parent 64720fd commit 6a3c141
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 277 deletions.
39 changes: 21 additions & 18 deletions docs/concepts/molang.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ mentions:
- DoubleShotgun
- Luthorius
- TheItsNameless
description: What is MoLang?
- QuazChick
description: Learn about the basics of Molang.
---

## Introduction
Expand All @@ -22,29 +23,31 @@ An equation evaluates to `true` when any number except `0` is returned. When I r

There are three main ways to access and use values in Molang (queries, variables and temp variables)

- **Queries** are read only values returned by the game. You cannot set these values, only read them. (`query.example_query` | `q.example_query`)
- **Queries** are read only values returned by the game. You cannot set these values, only read them. (`query.example_query` | `q.example_query`)

- **Variables** are read and write values that you can manipulate, these can be set and read through Molang. (`variable.example_variable` | `v.example_variable`)
- There are also hard-coded variables which act practically the same way as queries, but can only be used in certain situations.
- **Variables** are read and write values that you can manipulate, these can be set and read through Molang. (`variable.example_variable` | `v.example_variable`)

- **Temp. Variables** are practically the same as variables, except they only exist in the current scope. (`temp.example_temp` | `t.example_temp`)
- A "scope" can refer to the current `for_each` or `loop` *or* just the current expression, if it's not used within either
- There are also hard-coded variables which act practically the same way as queries, but can only be used in certain situations.

- **Temp. Variables** are practically the same as variables, except they only exist in the current scope. (`temp.example_temp` | `t.example_temp`)
- A "scope" can refer to the current `for_each` or `loop` _or_ just the current expression, if it's not used within either

## Handling values

- **Logical Operators** can be used to convert non-numbers into 1s or 0s. These include: `==`, `!=`, `<`, `>`, `<=`, `>=`.
- Example.) "`q.get_equipped_item_name == 'stick'`" Will evaluate to `1`/`true` when holding a stick
- **Logical Operators** can be used to convert non-numbers into 1s or 0s. These include: `==`, `!=`, `<`, `>`, `<=`, `>=`.

- Example.) "`q.property('wiki:size') == 'small'`" Will evaluate to `1`/`true` when the entity's `wiki:size` property is set to `small`.

- There is also a *second* set of *Logical Operators* which can be used to 'group' values into `and/or` statements, often used in cases where you need *multiple* things to evaluate to `true` or just *one out of many*. `&&` represents an `and` statement, and `||` represents an `or` statement.
- Example.) "`q.is_sneaking && q.is_using_item`" Will evaluate to `1`/`true` when sneaking *and* using an item
- Example.) "`q.is_sneaking || q.is_jumping`" // Evaluates to `1`/`true` when either jumping *or* sneaking
- There is also a _second_ set of _Logical Operators_ which can be used to 'group' values into `and/or` statements, often used in cases where you need _multiple_ things to evaluate to `true` or just _one out of many_. `&&` represents an `and` statement, and `||` represents an `or` statement.
- Example.) "`q.is_sneaking && q.is_using_item`" Will evaluate to `1`/`true` when sneaking _and_ using an item
- Example.) "`q.is_sneaking || q.is_jumping`" // Evaluates to `1`/`true` when either jumping _or_ sneaking

- **Parentheses**, `( )`, are also a major help when grouping values or performing math operations.
- Example.) "`q.is_sneaking && (q.get_equipped_item_name == "stick" || q.get_equipped_item_name == "diamond")`" Will evaluate to `1`/`true` when sneaking *and* holding either a stick *or* a diamond
- **Parentheses**, `( )`, are also a major help when grouping values or performing math operations.

- **Conditional Operators** can be used as `if/else` statements.
- A *binary* conditional operator refers to just using `?`. When this is used, it'll output your value or `0` depending on whether the given input value is `true`.
- Example.) "`q.is_sneaking ? 5`" Will output a `5` when sneaking, otherwise returning a `0`
- A *trinary* conditional operator refers to using `?` and `:`. When this is used, it'll output one of the two given values depending on whether your given input value is `true`.
- Example.) "`q.is_sneaking ? 10 : 3`" Will output a `10` when sneaking, otherwise returning a `3`
- Example.) "`q.is_angry && (q.property('wiki:size') == 'medium' || q.property('wiki:size') == 'large')`" Will evaluate to `1`/`true` when angry _and_ `wiki:size` is `medium` _or_ `large`.

- **Conditional Operators** can be used as `if/else` statements.
- A _binary_ conditional operator refers to just using `?`. When this is used, it'll output your value or `0` depending on whether the given input value is `true`.
- Example.) "`q.is_sneaking ? 5`" Will output a `5` when sneaking, otherwise returning a `0`
- A _trinary_ conditional operator refers to using `?` and `:`. When this is used, it'll output one of the two given values depending on whether your given input value is `true`.
- Example.) "`q.is_sneaking ? 10 : 3`" Will output a `10` when sneaking, otherwise returning a `3`
7 changes: 3 additions & 4 deletions docs/documentation/advanced-molang.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
---
title: Advanced Molang
description: Advanced MoLang.
description: Learn about more advanced topics within Molang.
mentions:
- Ciosciaa
- TheItsNameless
- QuazChick
---

## Values
Expand Down Expand Up @@ -53,12 +54,10 @@ The complete precedence list, from first to last evaluated:

- Assignments return the value assigned. You can therefore chain assignments if you need separate variables to work with from a single value, such as with `v.iterator_x = (v.iterator_z = math.random_integer(16, 32));`.
- The last statement inside a brace scope does not need to end with a `;`.
- Brace scopes can be used anywhere an expression can be used. `v.spawn_point ?? {v.target = false;};`, for example, would set `v.target` to `false` if `v.spawn_point` were not defined.
- Brace scopes can be used anywhere an expression can be used. `v.spawn_point ?? { v.target = false; };`, for example, would set `v.target` to `false` if `v.spawn_point` were not defined.

## Collections

- Entity iterables (such as the result of `q.get_nearby_entities`) are their own "type". They are not compatible with subscripts.
- Arrays, likewise, are not compatible with entity iterable operations, such as `q.count`.
- The result of array subscripts cannot directly be an argument to `+`, `-`, `*`, or `/` but may still be used directly as function parameters (even math functions) or with other operators.

## Evaluation
Expand Down
200 changes: 52 additions & 148 deletions docs/entities/detecting-other-entities.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ mentions:
- SirLich
- Luthorius
- TheItsNameless
- QuazChick
description: Fire an event when other entities are nearby.
---

You might have thought about making your entities fire an event when other entities are nearby. This article details the various known ways to do so.

## minecraft:entity_sensor
## Entity Sensor

This is probably the most basic way to detect other entities. The main issues is it only accepts one entry and testing if the entity is out of range can be very tricky. Because it's an entity component, you can just place into your entity behavior file and edit the Minecraft filters. Here's a demonstration:

Expand All @@ -37,171 +38,73 @@ This is probably the most basic way to detect other entities. The main issues is
}
```

## `/execute`
## Execute Command

Using the new `/execute` command that has been introduced since 1.19.50, you can execute commands as long as another entity is nearby.
Using the `/execute` command, you can execute commands as long as another entity is nearby.

This example you'll be following will make pigs say "oink oink" upon detecting players, though you can replace those with whatever you want. First of all, copy-paste these BP animations.

<CodeHeader>BP/animations/detection_animation.json</CodeHeader>

```json
{
"format_version": "1.10.0",
"animations": {
"animation.pig.find_player": {
"animation_length": 0.05,
"loop": true,
"timeline": {
"0": [
"/execute as @s if entity @e[type=player, r=4] run event entity @s wiki:player_detected"
]
}
},
"animation.pig.find_no_player": {
"animation_length": 0.05,
"loop": true,
"timeline": {
"0": [
"/execute as @s unless entity @e[type=player, r=4] run event entity @s wiki:no_player_detected"
]
}
}
"format_version": "1.10.0",
"animations": {
"animation.pig.find_player": {
"animation_length": 0.05,
"loop": true,
"timeline": {
"0": [
"/execute as @s if entity @e[type=player, r=4] run event entity @s wiki:player_detected"
]
}
},
"animation.pig.find_no_player": {
"animation_length": 0.05,
"loop": true,
"timeline": {
"0": [
"/execute as @s unless entity @e[type=player, r=4] run event entity @s wiki:no_player_detected"
]
}
}
}
}
```

The first one is for detecting if the entity is present, and the other for detecting if the entity is not present. The events used in the `/event` part of the `/execute` commands can be used for adding a [dummy component](/entities/dummy-components) or updating an [actor property](https://learn.microsoft.com/en-us/minecraft/creator/documents/introductiontoentityproperties).

Next of all, copy paste this BP animation controller. This assumes that you set up the `/event` parts of the `/execute` commands to add or remove `minecraft:is_sheared`.
Next of all, copy paste this BP animation controller. This assumes that you set up the `/event` parts of the `/execute` commands to add or remove `minecraft:is_sheared`.

<CodeHeader>BP/animation_controllers/pig_animation_controllers.json</CodeHeader>

```json
{
"format_version": "1.10.0",
"animation_controllers": {
"controller.animation.pig_find_player": {
"initial_state": "default",
"states": {
"default": {
"animations": ["find_player"],
"transitions": [
{
"detected": "q.is_sheared"
}
]
},
"detected": {
"animations": ["find_no_player"],
"transitions": [
{
"default": "!q.is_sheared"
}
],
"on_entry": ["/say oink oink"]
}
"format_version": "1.10.0",
"animation_controllers": {
"controller.animation.pig_find_player": {
"initial_state": "default",
"states": {
"default": {
"animations": ["find_player"],
"transitions": [
{
"detected": "q.is_sheared"
}
]
},
"detected": {
"animations": ["find_no_player"],
"transitions": [
{
"default": "!q.is_sheared"
}
],
"on_entry": ["/say oink oink"]
}
}
}
}
```
Finally, copy-paste this snippet into the behavior file for the pig-like so. Make sure to insert this in `description`.

<CodeHeader>BP/entities/my_entity.json#description</CodeHeader>

```json
"animations": {
"manage_find_player": "controller.animation.pig_find_player",
"find_player": "animation.pig.find_player",
"find_no_player": "animation.pig.find_no_player"
},
"scripts": {
"animate": [
"manage_find_player"
]
}
```

## Molang, BP Animations & Animation Controllers

The `for_each` function and `q.get_nearby_entities` or `q.get_nearby_entities_except_self` can also be used for detecting other entities. They are more effective than using `minecraft:entity_sensor` because they are better at detecting if the entity you want to detect goes away than with `minecraft:entity_sensor`. The only downside is that they're experimental.

Just like in the previous method we will make pigs say "oink oink" upon detecting players, though you can replace those with whatever you want. First of all, copy-paste this BP animation:

<CodeHeader>BP/animations/detection_animation.json</CodeHeader>

```json
{
"format_version": "1.10.0",
"animations": {
"animation.pig.find_player": {
"animation_length": 0.05,
"loop": true,
"timeline": {
"0": [
"v.x = 0.0; for_each(t.player, q.get_nearby_entities_except_self(16, 'minecraft:player'), { v.x = v.x + 1; }); return v.x > 0.0;"
]
}
}
}
}
```

The first parameter that `q.get_nearby_entities_except_self` needs to work is the radius in blocks it will detect other entities in. The other is the identifier of the mob you want to make it detect.

Now that's good and all, but on the off chance, you want to make the pig detect players with some attribute that can be detected with Molang, use this.

<CodeHeader>BP/animations/detection_animation.json</CodeHeader>

```json
{
"format_version": "1.10.0",
"animations": {
"animation.pig.find_player": {
"animation_length": 0.05,
"loop": true,
"timeline": {
"0": [
"v.x = 0.0; for_each(t.player, q.get_nearby_entities_except_self(2, 'minecraft:player'), { v.x = v.x + (t.player -> q.is_sheared); }); return v.x > 0.0;"
]
}
}
}
}
```

Next of all, copy paste this BP animation controller:

<CodeHeader>BP/animation_controllers/pig_animation_controllers.json</CodeHeader>

```json
{
"format_version": "1.10.0",
"animation_controllers": {
"controller.animation.pig_find_player": {
"initial_state": "default",
"states": {
"default": {
"animations": ["find_player"],
"transitions": [
{
"detected": "v.x > 0"
}
]
},
"detected": {
"animations": ["find_player"],
"transitions": [
{
"default": "v.x <= 0"
}
],
"on_entry": ["/say oink oink"]
}
}
}
}
}
}
```

Expand All @@ -211,12 +114,13 @@ Finally, copy-paste this snippet into the behavior file for the pig-like so. Mak

```json
"animations": {
"manage_find_player": "controller.animation.pig_find_player",
"find_player": "animation.pig.find_player"
"manage_find_player": "controller.animation.pig_find_player",
"find_player": "animation.pig.find_player",
"find_no_player": "animation.pig.find_no_player"
},
"scripts": {
"animate": [
"manage_find_player"
]
"manage_find_player"
]
}
```
Loading

0 comments on commit 6a3c141

Please sign in to comment.