Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
408 changes: 304 additions & 104 deletions distr/flecs.c

Large diffs are not rendered by default.

17 changes: 2 additions & 15 deletions distr/flecs_no_addons.c
Original file line number Diff line number Diff line change
Expand Up @@ -4541,20 +4541,6 @@ void flecs_on_component(ecs_iter_t *it) {
"component id must be smaller than %u", ECS_MAX_COMPONENT_ID);
(void)component_id;

if (it->event != EcsOnRemove) {
ecs_entity_t parent = ecs_get_parent(world, e);
if (parent) {
ecs_record_t *parent_record = flecs_entities_get(world, parent);
ecs_table_t *parent_table = parent_record->table;
if (!ecs_table_has_id(world, parent_table, EcsModule)) {
if (!ecs_table_has_id(world, parent_table, ecs_id(EcsComponent))) {
ecs_add_id(world, parent, EcsModule);
}
}

}
}

if (it->event == EcsOnSet) {
if (flecs_type_info_init_id(
world, e, c[i].size, c[i].alignment, NULL))
Expand Down Expand Up @@ -38982,7 +38968,7 @@ void flecs_add_with_property(
ecs_id_t id = ids[i];
ecs_assert(ECS_PAIR_FIRST(id) == EcsWith, ECS_INTERNAL_ERROR, NULL);
ecs_id_t ra = ECS_PAIR_SECOND(id);
ecs_id_t a = ra;
ecs_id_t a = ecs_get_alive(world, ra);
if (o) {
a = ecs_pair(ra, o);
}
Expand Down Expand Up @@ -39638,6 +39624,7 @@ typedef enum ecs_token_kind_t {
EcsTokKeywordInclude = 138,
EcsTokKeywordFn = 139,
EcsTokArrow = 140,
EcsTokKeywordMut = 141,
EcsTokAddAssign = 136,
EcsTokMulAssign = 137,
} ecs_token_kind_t;
Expand Down
13 changes: 12 additions & 1 deletion docs/FlecsScript.md
Original file line number Diff line number Diff line change
Expand Up @@ -1182,7 +1182,7 @@ Templates are commonly used in combination with the kind syntax:
Square my_entity
```

Templates can be parameterized with properties. Properties are variables that are exposed as component members. To create a property, use the `prop` keyword. Example:
Templates can be parameterized with properties. Properties are variables that are exposed as component members. When the component is updated with a new value, the template is reevaluated. To create a property, use the `prop` keyword. Example:

```cpp
template Square {
Expand All @@ -1198,6 +1198,17 @@ Square my_entity(size: 20, color: {38, 25, 13})

Just like `const` variables, `prop` variables can explicitly specify a type or implicitly derive their type from the assigned (default) value.

In addition to property variables, templates can also contain mutables. Mutables that are exposed as component members on a `TemplateComponent::mut` component. To create a mutable, use the `mut` keyword. For example the `hover` mutable variable ends up on a `Button::mut` component:

```cpp
template Button {
prop text: "Howdy"
mut hover: false

// ...
}
```

Template scripts can do anything a regular script can do, including creating child entities. The following example shows how to create an template that uses a nested template to create children:

```cpp
Expand Down
3 changes: 3 additions & 0 deletions src/addons/parser/tokenizer.c
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ const char* flecs_token_kind_str(
case EcsTokKeywordWith:
case EcsTokKeywordUsing:
case EcsTokKeywordProp:
case EcsTokKeywordMut:
case EcsTokKeywordConst:
case EcsTokKeywordIf:
case EcsTokKeywordElse:
Expand Down Expand Up @@ -215,6 +216,7 @@ const char* flecs_token_str(
case EcsTokKeywordWith: return "with";
case EcsTokKeywordUsing: return "using";
case EcsTokKeywordProp: return "prop";
case EcsTokKeywordMut: return "mut";
case EcsTokKeywordConst: return "const";
case EcsTokKeywordMatch: return "match";
case EcsTokKeywordNew: return "new";
Expand Down Expand Up @@ -908,6 +910,7 @@ const char* flecs_token(
Keyword ("using", EcsTokKeywordUsing)
Keyword ("template", EcsTokKeywordTemplate)
Keyword ("prop", EcsTokKeywordProp)
Keyword ("mut", EcsTokKeywordMut)
Keyword ("const", EcsTokKeywordConst)
Keyword ("if", EcsTokKeywordIf)
Keyword ("else", EcsTokKeywordElse)
Expand Down
1 change: 1 addition & 0 deletions src/addons/parser/tokenizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ typedef enum ecs_token_kind_t {
EcsTokKeywordInclude = 138,
EcsTokKeywordFn = 139,
EcsTokArrow = 140,
EcsTokKeywordMut = 141,
EcsTokAddAssign = 136,
EcsTokMulAssign = 137,
} ecs_token_kind_t;
Expand Down
1 change: 1 addition & 0 deletions src/addons/script/ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ typedef enum ecs_script_node_kind_t {
EcsAstAnnotation,
EcsAstTemplate,
EcsAstProp,
EcsAstMut,
EcsAstConst,
EcsAstExportConst,
EcsAstEntity,
Expand Down
1 change: 1 addition & 0 deletions src/addons/script/expr/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,7 @@ int flecs_value_binary(
case EcsTokKeywordNew:
case EcsTokKeywordExport:
case EcsTokKeywordProp:
case EcsTokKeywordMut:
case EcsTokKeywordConst:
case EcsTokKeywordInclude:
case EcsTokKeywordFn:
Expand Down
2 changes: 2 additions & 0 deletions src/addons/script/expr/visit_type.c
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ bool flecs_expr_oper_valid_for_type(
case EcsTokKeywordExport:
case EcsTokKeywordTemplate:
case EcsTokKeywordProp:
case EcsTokKeywordMut:
case EcsTokKeywordConst:
case EcsTokKeywordInclude:
case EcsTokKeywordFn:
Expand Down Expand Up @@ -459,6 +460,7 @@ int flecs_expr_type_for_operator(
case EcsTokKeywordExport:
case EcsTokKeywordTemplate:
case EcsTokKeywordProp:
case EcsTokKeywordMut:
case EcsTokKeywordConst:
case EcsTokKeywordInclude:
case EcsTokKeywordFn:
Expand Down
20 changes: 18 additions & 2 deletions src/addons/script/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,8 @@ const char* flecs_script_parse_var(
var->node.kind = kind;

bool is_prop = kind == EcsAstProp;
bool is_mut = kind == EcsAstMut;
const char *kind_str = is_prop ? "prop" : (is_mut ? "mut" : "const");

Parse(
// const color =
Expand Down Expand Up @@ -391,8 +393,7 @@ const char* flecs_script_parse_var(
Expr('\n',
Warning("'%s var = expr' syntax is deprecated"
", use '%s var: expr' instead",
is_prop ? "prop" : "const",
is_prop ? "prop" : "const");
kind_str, kind_str);
var->expr = EXPR;
EndOfRule;
)
Expand Down Expand Up @@ -421,6 +422,15 @@ const char* flecs_script_parse_const(
return flecs_script_parse_var(parser, pos, tokenizer, EcsAstConst);
}

static
const char* flecs_script_parse_mut(
ecs_parser_t *parser,
const char *pos,
ecs_tokenizer_t *tokenizer)
{
return flecs_script_parse_var(parser, pos, tokenizer, EcsAstMut);
}

static
const char* flecs_script_parse_export_const(
ecs_parser_t *parser,
Expand Down Expand Up @@ -574,6 +584,7 @@ const char* flecs_script_stmt(
case EcsTokKeywordUsing: goto using_stmt;
case EcsTokKeywordTemplate: goto template_stmt;
case EcsTokKeywordProp: goto prop_var;
case EcsTokKeywordMut: goto mut_var;
case EcsTokKeywordConst: goto const_var;
case EcsTokKeywordExport: goto export_var;
case EcsTokKeywordIf: goto if_stmt;
Expand Down Expand Up @@ -741,6 +752,11 @@ prop_var: {
return flecs_script_parse_prop(parser, pos, tokenizer);
}

// mut
mut_var: {
return flecs_script_parse_mut(parser, pos, tokenizer);
}

// export
export_var: {
// export const
Expand Down
2 changes: 1 addition & 1 deletion src/addons/script/script.c
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ int ecs_script_update(
const char *name = ecs_get_name(world, e);
EcsScript *s = ecs_ensure(world, e, EcsScript);
if (s->template_) {
char *template_name = ecs_get_path(world, s->template_->entity);
char *template_name = ecs_get_path(world, s->template_->props.type);
ecs_err("cannot update scripts for individual templates, "
"update parent script instead (tried to update '%s')",
template_name);
Expand Down
Loading
Loading