Cẩm nang người chơi

Block Scripts


This article describes how to edit block scripts.

Interface description

image-20240719154714450

  1. Canvas, main editing area
  2. File currently being edited
  3. Classification of blocks, click on any block to expand the block selection UI
  4. Properties panel

Explanation of block classification

The classifications of the blocks in the 3. area are:

  1. Search: blocks searched for by entering keywords.
  2. Commonly used: blocks set as commonly used blocks will be here.
  3. Event: will be triggered when the condition is met, which is the beginning of the logic.
  4. Condition: used for flow control of the script.
  5. Behavior: The actual manipulation of entities and data.
  6. Module: Some module-specific behaviors.
  7. Data: Manipulation of data.
  8. Variable: Use existing variables or add variables.
  9. Function: Use or create custom functions.
  10. External call: Call custom functions in other scripts.

Using blocks

Block description

The bulge below the block indicates that other blocks can be linked after this block.

image-20240719162414368

The indentation above the block indicates that other blocks can be linked before this block and that the block must be run after the previous block is run.

image-20240719162458868

If there is no indentation at the top but a bulge at the bottom, it means that the block is the starting point of a piece of logic, usually an event.

image-20240719162532230

If there are no bulges or depressions above or below, it means that the block represents a piece of data that can be applied to other blocks.

image-20240719162637872

The block will contain input and output parameters:

Input parameters:

The input parameters are left blank by default, and the type marked inside is the type of the required parameter.

image-20240719163219101

The script name in this block is also the required parameter, but it uses a resource selector, which allows you to directly select the corresponding resource in the project without having to consider type matching.

Output parameters:

The default output parameter is a colored square, which can be dragged and dropped into the required input parameter box:

image-20240719163455421

Using blocks

After selecting the block you want to use in the category, click or drag it to the canvas to use the corresponding block.

image-20240719161426471

image-20240719161444643

You can join two blocks together by using the block with the groove on top:

image-20240719161910607

image-20240719161930593

The logic of a group of blocks is always top-to-bottom:

image-20241105153444063

image-20241105153522814

For a group of elements connected in series, the necessary input parameters need to be filled in with the appropriate data to ensure that the script works properly. Inappropriate variables will cause an error. Click on the error to view the error message:

image-20240719163823327

image-20240719163854644

When dragging a block, the blocks below it will also be dragged:

image-20240906110832061

image-20240906110844840

Hold down the Ctrl key and drag to drag only the current block. The blocks below will be automatically connected to the top:

image-20240906110855565

Block properties panel

Click on a block to select it

image-20241122190755370

In the Inspector panel on the right, you can view detailed information about the selected block, including its name, description, parameter name, parameter type, return value type, and return value description.

image-20241122190944942

Flow control

The default execution order of block scripts is from top to bottom. You may often need code flow control to implement complex logic.

Please refer to the link below for detailed instructions on block script flow control:

[Script Additional Instructions - User Manual.md](./Script Additional Instructions - User Manual.md)

Variables

There are three types of variables that can be defined or modified in the variables section:

  1. Global entity properties: properties of global components that can be customized. You can access and modify custom global entity properties in any script.
  2. Script variables: variables that are only used in the current script and can be accessed and modified externally by other scripts.
  3. Local variables: variables that are only used in the current code block and are not valid outside the code block.

Global entity properties

Global entity properties can be added through the entity component properties in the component settings.

image-20240719170731723

image-20240719170751222

image-20240719170812335

After creating the global entity property, you can use GetGlobalBlock to get the property:

image-20240719170915428

Set the property using the Set Global Block:

image-20240719170953632

Script variables

In the Variables category, there is a button for creating variables:

image-20240719171622005

image-20240719171647910

Variables created in this way are script variables and are used for data processing in the current script.

They can be accessed and set in other scripts by means of an external reference:

image-20240719171819031

When referencing external scripts, you need to specify the corresponding entity mounted by the referencing script when getting and setting.

Local variables

Local variable blocks can be used to create variables that are only available in the current code block:

image-20240719171508081

You can rename a variable by double-clicking on its name:

image-20240719171540482

A code block is a continuous sequence of elements. It is worth noting that the “if-else” elements in the conditional classification are by default two code blocks. Each if, else, and else if is a separate code block.

image-20240722192115698

image-20240722192055277

Custom functions and use

Block scripts support functions with and without return values.

Functions with return values cannot be linked to other blocks when called, and the called block is the return value.

image-20240719180031043

image-20240719180117743

Functions without a return value can be linked to other blocks when called.

image-20240719180039563

image-20240719180134147

Wait: If this element is an asynchronous function, it will be blocked and the following elements will not be executed until the asynchronous function has been executed.

Execute: If this element is an asynchronous function, it will not be blocked and the following elements will continue to be executed.

An asynchronous function is a function implementation that uses an element that will perform asynchronous processing, such as “wait”.

image-20240722105341566

Note: Asynchronous blocks are not supported in functions with a return value.

image-20240806115625031

If you need the return value of an asynchronous function, use a function without a return value and use an output variable.

image-20240806115658582

Regardless of whether or not there is a return value, the custom function can use the output variable of the function below the calling block to get the output.

image-20240806113746376

image-20240806121329099

Searching for blocks and frequently used

keywords can search for corresponding blocks

image-20240719181845696

Right-click on a block in the UI to add it to your frequently used blocks. You can quickly access frequently used blocks in the frequently used block category.

image-20240719182033312

image-20240719182130450

Example

The use of scripts is demonstrated using a simple example:

It is designed as follows:

  1. When each player joins the game, they are issued an M4A1.
  2. When the player fires, they will lose 1 health point each time they fire.

Creating the script:

The requirements of 1. are global, and 2. are specific to each player. Therefore, you need to attach a script to each of the global and player. The issuing of consumables and the deduction of health both need to be known to the server, so they are both created as server scripts.

image-20240719183317332

image-20240719183333435

Edit script:

For 1, it is necessary for each player to join the competition to execute the release of consumables once:

image-20240719183627320

To add a consumable block, you need to add three parameters: the target of the consumable, the consumable to be added, and the quantity of the consumable to be added.

The target of the consumable is the player who triggered the event when the player joined the match. The consumable is selected from the Explorer as M4A1, and the quantity is 1.

image-20240719183756275

For 2, the health needs to be deducted once every time it fires:

We find that there is actually no block that deducts health:

image-20240719183925677

Restoring the parameters of the health block to a negative value will not deduct health

However, as health is a property of the player, it can be adjusted by setting properties:

image-20240719184034769

Setting a property block requires several parameters: the entity to be set, the property to be set and the value, as well as the operation.

The entity to be set is the current player, so you can use this entity and double-click in the parameter position to quickly fill in this entity.

image-20240719184236329

The property value that needs to be changed is the current health value, so select the current health value.

The value to be set is the current health minus one, so you need to get the current health.

image-20240719184443713

Subtract one from it using subtraction in the data category and add the parameters:

image-20240719184604249

image-20240719184622223

The logic can also be implemented directly using the operation for setting the properties of the block:

image-20240719184700828

Run debugging to view the results:

image-20240719184826220

Everyone was issued an M4A1.

image-20240719184935284

8 shots fired, 8 points of health lost.

Triggered when firing. This event is triggered each time the command to fire is executed. For a weapon that fires continuously, each shot before the weapon is put down is considered one fire and can only trigger this event once.