SonolusHaniwa
  • 1. 概述
  • 2. ハニプレ
  • 3. ユメステ
  • 4. Stellarity
  • 5. Phigros
  • 首页

    • 1. 概述
  • 玩家

    • 2. 安装服务
    • 3. 配置文件标准
    • 4. 服务使用方法
  • 开发者

    • 5. 自定义搜索函数
    • 6. 自定义资源创建接口
    • 7. 插件开发标准
  • 其他

    • 8. 更新日志
    • 9. 关于 srp 资源包
  • 1. 搭建开发环境
  • 2. Sonolus 基础
  • 3. 配置项目信息
  • 4. 基本数据类型
  • 5. 函数与语句块
  • 6. 注意事项
  • 1. 游玩模式基础
  • Index

    • 1. Introduce
  • Basics

    • 1. Index
    • 2. Types
    • 3. Statements
    • 4. Resources
    • 5. Projects
    • 6. Commands
  • API

    • Index
    • Macros
    • Builtins
    • Engine Options
    • Engine UI
    • Play Mode
    • Tutorial Mode
    • Preview Mode
    • Watch Mode
  • 1. 概述
  • 2. ハニプレ
  • 3. ユメステ
  • 4. Stellarity
  • 5. Phigros
  • 首页

    • 1. 概述
  • 玩家

    • 2. 安装服务
    • 3. 配置文件标准
    • 4. 服务使用方法
  • 开发者

    • 5. 自定义搜索函数
    • 6. 自定义资源创建接口
    • 7. 插件开发标准
  • 其他

    • 8. 更新日志
    • 9. 关于 srp 资源包
  • 1. 搭建开发环境
  • 2. Sonolus 基础
  • 3. 配置项目信息
  • 4. 基本数据类型
  • 5. 函数与语句块
  • 6. 注意事项
  • 1. 游玩模式基础
  • Index

    • 1. Introduce
  • Basics

    • 1. Index
    • 2. Types
    • 3. Statements
    • 4. Resources
    • 5. Projects
    • 6. Commands
  • API

    • Index
    • Macros
    • Builtins
    • Engine Options
    • Engine UI
    • Play Mode
    • Tutorial Mode
    • Preview Mode
    • Watch Mode
  • Index

    • 1. Introduce
  • Basics

    • 1. Index
    • 2. Types
    • 3. Statements
    • 4. Resources
    • 5. Projects
    • 6. Commands
  • API

    • Index
    • Macros
    • Builtins
    • Engine Options
    • Engine UI
    • Play Mode
    • Tutorial Mode
    • Preview Mode
    • Watch Mode

Statements

Most C++ 14 standard statements were supported in Sonolus.h.

Functions

Functions are divided into two types: SonolusApi function and normal C++ function.

SonolusApi function refers to a function that use SonolusApi as its return type. It's basic function type in Sonolus.h.

All the statements in SonolusApi function will be converted into Sonolus statements by interpreter, and all the statements not in SonolusApi function will be treated as normal C++ statement.

You can use the following syntax to define a SonolusApi function:

SonolusApi add(var a, var b) {
    ...
}

var a = add(1, 2);

It will be compiled to:

Set(10000, 1, 1)
Set(10000, 2, 2)
Set(10000, 3, Block(
    Execute(
        Break(1, Add(
            Get(10000, 1),
            Get(10000, 2)
        ))
    )
))

Of course, if you don't use any statements that need to converted into Sonolus statements, you can define a normal C++ function directly, which may be able to reduce the number of nodes in the engine data:

var add(var a, var b) {
    return a + b;
}

var a = add(1, 2);

It will be compiled to:

Set(10000, 1, 1)
Set(10000, 2, 2)
Set(10000, 3, Add(
    Get(10000, 1),
    Get(10000, 2)
))

Selection Statement

Most standard selection statements are supported.

if / else

var a = 1, b = 2;
if (a == b) {
    DebugLog(true);
} else {
    DebugLog(false);
}

Please note that conditional operator is not supported in SonolusApi function, which means you need to use If(<condition>, <trueValue>, <falseValue>) instead:

var a = 1, b = 2;
var c = a == b ? true : false;   // Not ok
var d = If(a == b, true, false); // Ok

switch / case

var a = 1;
switch (a) {
    case 1:
        DebugLog(1);
        break;
    case 2: {
        DebugLog(2);
    } break;
    case 3: { 
        DebugLog(3);
    };
    default: {
        DebugLog(4);
    }
}

注意

It's not recommended to associate too many cases to a switch statement, otherwise interpreter will generate a very very long code (O(n^2) lines) which will increate compilation time of g++.

注意

Current switch statement is not full-supported. Only the switch which sacrifies the following rules is supported:

  • One case / default corresponding to one body.
  • One body corresponding to one case / default.

Iteration Statement

Most standard iteration statements are supported.

for

Only basic for loop in C++ is supported. Range for loop is not supported:

// Ok
for (var i = 1; i <= 10; i++) {
    DebugLog(i);
}

// Not ok
for (Touch touch : touches) {
    DebugLog(touch.id);
}

There also exists a for loop named CppLoop. If you want to treat a for loop as a normal C++ loop instead of Sonolus loop in SonolusApi function, you need to use this type of for loop:

for (CppLoop int i = 1; i <= 10; i++) {
    DebugLog(i);
}

// It will be compiled to:
DebugLog(1);
DebugLog(2);
...
DebugLog(10);

while

while loop is fully supported:

var a = 10;
while (a) {
    DebugLog(a);
    a--;
}

Jump Statement

Most standard jump statements are supported.

continue

continue statement is fully supported in for loop and while loop:

for (var i = 1; i <= 10; i++) {
    continue;
    DebugLog(i);
}

var a = 10;
while (a) {
    a--;
    continue;
    Debuglog(a);
}

break

break statement is also fully supported:

for (var i = 1; i <= 10; i++) {
    DebugLog(i);
    break;
}

var a = 10;
while (a) {
    break;
    Debuglog(a);
}

switch (a) {
    case 1:
        DebugLog(1);
        break;
    default: 
        DebugLog(2);
}

return

return statement is also fully supported:

SonolusApi add(var a, var b) {
    return a + b;
}

SonolusApi abs(var a) {
    return If(a < 0, -a : a);
}

SonolusApi execute() {
    DebugLog(0);
    return; // It will automatically return 0
}

Not-Supported Statements

  • Lambda function.
  • throw. Please use SonolusAssert instead.
  • Range for loop. Please use normal for loop instead.
  • Direct-initialization syntax(VarType VarName(Params)). Please use copy-initialization syntax(VarType VarName = VarType(Params)) instead.
  • C-style explicit type conversion((VarType)Var). Please use function-style explicit type conversion(VarType(Var)) instead.
  • Pointer is not recommended, but you can use it for sure.
  • Conditional operator in SonolusApi function.
最近更新: 2025/6/7 14:45
Contributors: LittleYang0531
Prev
2. Types
Next
4. Resources