Properly link flecs library
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
#ifndef BASICS_H
|
||||
#define BASICS_H
|
||||
|
||||
/* This generated file contains includes for project dependencies */
|
||||
#include "basics/bake_config.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
)
|
||||
(.)
|
||||
.|.
|
||||
| |
|
||||
_.--| |--._
|
||||
.-'; ;`-'& ; `&.
|
||||
\ & ; & &_/
|
||||
|"""---...---"""|
|
||||
\ | | | | | | | /
|
||||
`---.|.|.|.---'
|
||||
|
||||
* This file is generated by bake.lang.c for your convenience. Headers of
|
||||
* dependencies will automatically show up in this file. Include bake_config.h
|
||||
* in your main project file. Do not edit! */
|
||||
|
||||
#ifndef BASICS_BAKE_CONFIG_H
|
||||
#define BASICS_BAKE_CONFIG_H
|
||||
|
||||
/* Headers of public dependencies */
|
||||
#include <flecs.h>
|
||||
|
||||
#endif
|
||||
|
||||
11
engine/libs/flecs/examples/cpp/observers/basics/project.json
Normal file
11
engine/libs/flecs/examples/cpp/observers/basics/project.json
Normal file
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"id": "basics",
|
||||
"type": "application",
|
||||
"value": {
|
||||
"use": [
|
||||
"flecs"
|
||||
],
|
||||
"language": "c++",
|
||||
"public": false
|
||||
}
|
||||
}
|
||||
68
engine/libs/flecs/examples/cpp/observers/basics/src/main.cpp
Normal file
68
engine/libs/flecs/examples/cpp/observers/basics/src/main.cpp
Normal file
@@ -0,0 +1,68 @@
|
||||
#include <basics.h>
|
||||
#include <iostream>
|
||||
|
||||
// Observers provide a mechanism for responding to builtin and user defined
|
||||
// events. They are similar to systems, in that they have the same callback
|
||||
// signature and use the same query interface to match with entities, but
|
||||
// instead of a phase they have an event kind.
|
||||
//
|
||||
// The most commonly used builtin events are:
|
||||
// - flecs::OnAdd: a component was added
|
||||
// - flecs::OnRemove: a component was removed
|
||||
// - flecs::OnSet: a component's value was changed
|
||||
//
|
||||
// The OnAdd and OnRemove events are only thrown when a component is
|
||||
// actually added or removed. If an application invokes add and the entity
|
||||
// already has the component, no event is emitted. Similarly, if an application
|
||||
// invokes remove for a component the entity doesn't have, no event is
|
||||
// emitted. That is in contrast to OnSet, which is invoked each time set
|
||||
// or modified is invoked.
|
||||
//
|
||||
// Observers are different from component hooks in a number of ways:
|
||||
// - A component can only have one hook, whereas it can match many observers
|
||||
// - A hook matches one component, whereas observers can match complex queries
|
||||
// - Hooks are for add/set/remove events, observers can match custom events.
|
||||
|
||||
struct Position {
|
||||
double x, y;
|
||||
};
|
||||
|
||||
int main(int, char *[]) {
|
||||
flecs::world ecs;
|
||||
|
||||
// Create an observer for three events
|
||||
ecs.observer<Position>()
|
||||
.event(flecs::OnAdd)
|
||||
.event(flecs::OnRemove)
|
||||
.event(flecs::OnSet)
|
||||
.each([](flecs::iter& it, size_t i, Position& p) {
|
||||
if (it.event() == flecs::OnAdd) {
|
||||
// No assumptions about the component value should be made here. If
|
||||
// a ctor for the component was registered it will be called before
|
||||
// the EcsOnAdd event, but a value assigned by set won't be visible.
|
||||
std::cout << " - OnAdd: " << it.event_id().str() << ": "
|
||||
<< it.entity(i).name() << "\n";
|
||||
} else {
|
||||
// EcsOnSet or EcsOnRemove event
|
||||
std::cout << " - " << it.event().name() << ": "
|
||||
<< it.event_id().str() << ": "
|
||||
<< it.entity(i).name()
|
||||
<< ": {" << p.x << ", " << p.y << "}\n";
|
||||
}
|
||||
});
|
||||
|
||||
// Create entity, set Position (emits EcsOnAdd and EcsOnSet)
|
||||
flecs::entity e = ecs.entity("e")
|
||||
.set<Position>({10, 20});
|
||||
|
||||
// Remove component (emits EcsOnRemove)
|
||||
e.remove<Position>();
|
||||
|
||||
// Remove component again (no event is emitted)
|
||||
e.remove<Position>();
|
||||
|
||||
// Output
|
||||
// - OnAdd: Position: e
|
||||
// - OnSet: Position: e: {10, 20}
|
||||
// - OnRemove: Position: e: {10, 20}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
#ifndef CUSTOM_EVENT_H
|
||||
#define CUSTOM_EVENT_H
|
||||
|
||||
/* This generated file contains includes for project dependencies */
|
||||
#include "custom_event/bake_config.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
)
|
||||
(.)
|
||||
.|.
|
||||
| |
|
||||
_.--| |--._
|
||||
.-'; ;`-'& ; `&.
|
||||
\ & ; & &_/
|
||||
|"""---...---"""|
|
||||
\ | | | | | | | /
|
||||
`---.|.|.|.---'
|
||||
|
||||
* This file is generated by bake.lang.c for your convenience. Headers of
|
||||
* dependencies will automatically show up in this file. Include bake_config.h
|
||||
* in your main project file. Do not edit! */
|
||||
|
||||
#ifndef CUSTOM_EVENT_BAKE_CONFIG_H
|
||||
#define CUSTOM_EVENT_BAKE_CONFIG_H
|
||||
|
||||
/* Headers of public dependencies */
|
||||
#include <flecs.h>
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"id": "custom_event",
|
||||
"type": "application",
|
||||
"value": {
|
||||
"use": [
|
||||
"flecs"
|
||||
],
|
||||
"language": "c++",
|
||||
"public": false
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
#include <custom_event.h>
|
||||
#include <iostream>
|
||||
|
||||
// Observers can be used to match custom events. Custom events can be emitted
|
||||
// using the ecs_emit function. This function is also used by builtin events,
|
||||
// so builtin and custom events use the same rules for matching with observers.
|
||||
//
|
||||
// An event consists out of three pieces of data used to match with observers:
|
||||
// - An single event kind (EcsOnAdd, EcsOnRemove, ...)
|
||||
// - One or more event ids (Position, Velocity, ...)
|
||||
// - A source (either an entity or a table)
|
||||
|
||||
struct Position {
|
||||
double x, y;
|
||||
};
|
||||
|
||||
// Create tag type to use as event (could also use entity)
|
||||
struct MyEvent { };
|
||||
|
||||
int main(int, char *[]) {
|
||||
flecs::world ecs;
|
||||
|
||||
// Create observer for custom event
|
||||
ecs.observer<Position>()
|
||||
.event<MyEvent>()
|
||||
.each([](flecs::iter& it, size_t i, Position&) {
|
||||
std::cout << " - " << it.event().name() << ": "
|
||||
<< it.event_id().str() << ": "
|
||||
<< it.entity(i).name() << "\n";
|
||||
});
|
||||
|
||||
// The observer filter can be matched against the entity, so make sure it
|
||||
// has the Position component before emitting the event. This does not
|
||||
// trigger the observer yet.
|
||||
flecs::entity e = ecs.entity("e")
|
||||
.set<Position>({10, 20});
|
||||
|
||||
// Emit the custom event
|
||||
ecs.event<MyEvent>()
|
||||
.id<Position>()
|
||||
.entity(e)
|
||||
.emit();
|
||||
|
||||
// Output
|
||||
// - MyEvent: Position: e
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
#ifndef ENTITY_EVENT_H
|
||||
#define ENTITY_EVENT_H
|
||||
|
||||
/* This generated file contains includes for project dependencies */
|
||||
#include "entity_event/bake_config.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
)
|
||||
(.)
|
||||
.|.
|
||||
| |
|
||||
_.--| |--._
|
||||
.-'; ;`-'& ; `&.
|
||||
\ & ; & &_/
|
||||
|"""---...---"""|
|
||||
\ | | | | | | | /
|
||||
`---.|.|.|.---'
|
||||
|
||||
* This file is generated by bake.lang.c for your convenience. Headers of
|
||||
* dependencies will automatically show up in this file. Include bake_config.h
|
||||
* in your main project file. Do not edit! */
|
||||
|
||||
#ifndef ENTITY_EVENT_BAKE_CONFIG_H
|
||||
#define ENTITY_EVENT_BAKE_CONFIG_H
|
||||
|
||||
/* Headers of public dependencies */
|
||||
#include <flecs.h>
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"id": "entity_event",
|
||||
"type": "application",
|
||||
"value": {
|
||||
"use": [
|
||||
"flecs"
|
||||
],
|
||||
"language": "c++",
|
||||
"public": false
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
#include <iostream>
|
||||
#include <entity_event.h>
|
||||
|
||||
// Entity events are events that are emitted and observed for a specific entity.
|
||||
// They are a thin wrapper around regular observers, which match against queries
|
||||
// instead of single entities. While they work similarly under the hood, entity
|
||||
// events provide a much simpler API.
|
||||
//
|
||||
// An entity event only needs two pieces of data:
|
||||
// - The entity on which to emit the event
|
||||
// - The event to emit
|
||||
|
||||
// An event without payload
|
||||
struct Click { };
|
||||
|
||||
// An event with payload
|
||||
struct Resize {
|
||||
double width, height;
|
||||
};
|
||||
|
||||
int main(int, char *[]) {
|
||||
flecs::world ecs;
|
||||
|
||||
// Create a widget entity
|
||||
flecs::entity widget = ecs.entity("MyWidget");
|
||||
|
||||
// Observe the Click event on the widget entity.
|
||||
widget.observe<Click>([]() {
|
||||
std::cout << "clicked!\n";
|
||||
});
|
||||
|
||||
// Observers can have an entity argument that holds the event source.
|
||||
// This allows the same function to be reused for different entities.
|
||||
widget.observe<Click>([](flecs::entity src) {
|
||||
std::cout << "clicked on " << src.path() << "!\n";
|
||||
});
|
||||
|
||||
// Observe the Resize event. Events with payload are passed as arguments
|
||||
// to the observer callback.
|
||||
widget.observe([](Resize& p) {
|
||||
std::cout << "resized to {"
|
||||
<< p.width << ", " << p.height << "}!\n";
|
||||
});
|
||||
|
||||
// Emit the Click event
|
||||
widget.emit<Click>();
|
||||
|
||||
// Emit the Resize event
|
||||
widget.emit<Resize>({100, 200});
|
||||
|
||||
// Output
|
||||
// clicked!
|
||||
// clicked on ::MyWidget!
|
||||
// resized to {100, 200}!
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
#ifndef MONITOR_H
|
||||
#define MONITOR_H
|
||||
|
||||
/* This generated file contains includes for project dependencies */
|
||||
#include "monitor/bake_config.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
)
|
||||
(.)
|
||||
.|.
|
||||
| |
|
||||
_.--| |--._
|
||||
.-'; ;`-'& ; `&.
|
||||
\ & ; & &_/
|
||||
|"""---...---"""|
|
||||
\ | | | | | | | /
|
||||
`---.|.|.|.---'
|
||||
|
||||
* This file is generated by bake.lang.c for your convenience. Headers of
|
||||
* dependencies will automatically show up in this file. Include bake_config.h
|
||||
* in your main project file. Do not edit! */
|
||||
|
||||
#ifndef MONITOR_BAKE_CONFIG_H
|
||||
#define MONITOR_BAKE_CONFIG_H
|
||||
|
||||
/* Headers of public dependencies */
|
||||
#include <flecs.h>
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"id": "monitor",
|
||||
"type": "application",
|
||||
"value": {
|
||||
"use": [
|
||||
"flecs"
|
||||
],
|
||||
"language": "c++",
|
||||
"public": false
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
#include <monitor.h>
|
||||
#include <iostream>
|
||||
|
||||
// A monitor observer triggers when an entity starts/stop matching the observer
|
||||
// filter. The observer communicates whether an entity is "entering/leaving" the
|
||||
// monitor by setting ecs_iter_t::event to EcsOnAdd (for entering) or
|
||||
// EcsOnRemove (for leaving).
|
||||
//
|
||||
// To specify that an observer is a monitor observer, the EcsMonitor tag must be
|
||||
// provided as event. No additional event kinds should be provided for a monitor
|
||||
// observer.
|
||||
|
||||
struct Position {
|
||||
double x, y;
|
||||
};
|
||||
|
||||
struct Velocity {
|
||||
double x, y;
|
||||
};
|
||||
|
||||
int main(int, char *[]) {
|
||||
flecs::world ecs;
|
||||
|
||||
// Create observer for custom event
|
||||
ecs.observer<Position, Velocity>()
|
||||
.event(flecs::Monitor) // Monitor entities entering/leaving the query
|
||||
.each([](flecs::iter& it, size_t i, Position&, Velocity&) {
|
||||
if (it.event() == flecs::OnAdd) {
|
||||
std::cout << " - Enter: "
|
||||
<< it.event_id().str() << ": "
|
||||
<< it.entity(i).name() << "\n";
|
||||
} else if (it.event() == flecs::OnRemove) {
|
||||
std::cout << " - Leave: "
|
||||
<< it.event_id().str() << ": "
|
||||
<< it.entity(i).name() << "\n";
|
||||
}
|
||||
});
|
||||
|
||||
// Create entity
|
||||
flecs::entity e = ecs.entity("e");
|
||||
|
||||
// This does not yet trigger the monitor, as the entity does not yet match.
|
||||
e.set<Position>({10, 20});
|
||||
|
||||
// This triggers the monitor with EcsOnAdd, as the entity now matches.
|
||||
e.set<Velocity>({1, 2});
|
||||
|
||||
// This triggers the monitor with EcsOnRemove, as the entity no longer matches.
|
||||
e.remove<Position>();
|
||||
|
||||
// Output
|
||||
// - Enter: Velocity: e
|
||||
// - Leave: Position: e
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
#ifndef PROPAGATE_H
|
||||
#define PROPAGATE_H
|
||||
|
||||
/* This generated file contains includes for project dependencies */
|
||||
#include "propagate/bake_config.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
)
|
||||
(.)
|
||||
.|.
|
||||
| |
|
||||
_.--| |--._
|
||||
.-'; ;`-'& ; `&.
|
||||
\ & ; & &_/
|
||||
|"""---...---"""|
|
||||
\ | | | | | | | /
|
||||
`---.|.|.|.---'
|
||||
|
||||
* This file is generated by bake.lang.c for your convenience. Headers of
|
||||
* dependencies will automatically show up in this file. Include bake_config.h
|
||||
* in your main project file. Do not edit! */
|
||||
|
||||
#ifndef PROPAGATE_BAKE_CONFIG_H
|
||||
#define PROPAGATE_BAKE_CONFIG_H
|
||||
|
||||
/* Headers of public dependencies */
|
||||
#include <flecs.h>
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"id": "propagate",
|
||||
"type": "application",
|
||||
"value": {
|
||||
"use": [
|
||||
"flecs"
|
||||
],
|
||||
"language": "c++",
|
||||
"public": false
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
#include <propagate.h>
|
||||
#include <iostream>
|
||||
|
||||
// Events are propagated along relationship edges. This means that observers can
|
||||
// listen for events from a parent or prefab, like triggering when a component
|
||||
// inherited from a prefab was set.
|
||||
//
|
||||
// Event propagation happens automatically when an observer contains a filter
|
||||
// with the EcsUp flag set (indicating upwards traversal). Observers use the
|
||||
// same matching logic as queries: if a query with upwards traversal matches an
|
||||
// entity, so will an observer.
|
||||
//
|
||||
// Events are only propagated along traversable relationship edges.
|
||||
|
||||
struct Position {
|
||||
double x, y;
|
||||
};
|
||||
|
||||
int main(int, char *[]) {
|
||||
flecs::world ecs;
|
||||
|
||||
// Create observer that listens for events from both self and parent
|
||||
ecs.observer<Position, Position>()
|
||||
.term_at(2).parent() // select 2nd Position from parent
|
||||
.event(flecs::OnSet)
|
||||
.each([](flecs::iter& it, size_t i, Position& p_self, Position& p_parent) {
|
||||
std::cout << " - " << it.event().name() << ": "
|
||||
<< it.event_id().str() << ": "
|
||||
<< it.entity(i).name() << ": "
|
||||
<< "self: {" << p_self.x << ", " << p_self.y << "}, "
|
||||
<< "parent: {" << p_parent.x << ", " << p_parent.y << "}\n";
|
||||
});
|
||||
|
||||
// Create entity and parent
|
||||
flecs::entity p = ecs.entity("p");
|
||||
flecs::entity e = ecs.entity("e").child_of(p);
|
||||
|
||||
// Set Position on entity. This doesn't trigger the observer yet, since the
|
||||
// parent doesn't have Position yet.
|
||||
e.set<Position>({10, 20});
|
||||
|
||||
// Set Position on parent. This event will be propagated and trigger the
|
||||
// observer, as the observer query now matches.
|
||||
p.set<Position>({1, 2});
|
||||
|
||||
// Output
|
||||
// - OnSet: Position: e: self: {10, 20}, parent: {1, 2}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
#ifndef TWO_COMPONENTS_H
|
||||
#define TWO_COMPONENTS_H
|
||||
|
||||
/* This generated file contains includes for project dependencies */
|
||||
#include "two_components/bake_config.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
)
|
||||
(.)
|
||||
.|.
|
||||
| |
|
||||
_.--| |--._
|
||||
.-'; ;`-'& ; `&.
|
||||
\ & ; & &_/
|
||||
|"""---...---"""|
|
||||
\ | | | | | | | /
|
||||
`---.|.|.|.---'
|
||||
|
||||
* This file is generated by bake.lang.c for your convenience. Headers of
|
||||
* dependencies will automatically show up in this file. Include bake_config.h
|
||||
* in your main project file. Do not edit! */
|
||||
|
||||
#ifndef TWO_COMPONENTS_BAKE_CONFIG_H
|
||||
#define TWO_COMPONENTS_BAKE_CONFIG_H
|
||||
|
||||
/* Headers of public dependencies */
|
||||
#include <flecs.h>
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"id": "two_components",
|
||||
"type": "application",
|
||||
"value": {
|
||||
"use": [
|
||||
"flecs"
|
||||
],
|
||||
"language": "c++",
|
||||
"public": false
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
#include <two_components.h>
|
||||
#include <iostream>
|
||||
|
||||
// An observer can match multiple components/tags. Only entities that match the
|
||||
// entire observer filter will be forwarded to the callback. For example, an
|
||||
// observer for Position,Velocity won't match an entity that only has Position.
|
||||
|
||||
struct Position {
|
||||
double x, y;
|
||||
};
|
||||
|
||||
struct Velocity {
|
||||
double x, y;
|
||||
};
|
||||
|
||||
int main(int, char *[]) {
|
||||
flecs::world ecs;
|
||||
|
||||
// Create observer for custom event
|
||||
ecs.observer<Position, Velocity>()
|
||||
.event(flecs::OnSet)
|
||||
.each([](flecs::iter& it, size_t i, Position& p, Velocity& v) {
|
||||
std::cout << " - " << it.event().name() << ": "
|
||||
<< it.event_id().str() << ": "
|
||||
<< it.entity(i).name() << ": "
|
||||
<< "p: {" << p.x << ", " << p.y << "} "
|
||||
<< "v: {" << v.x << ", " << v.y << "}\n";
|
||||
});
|
||||
|
||||
// Create entity, set Position (emits EcsOnSet, does not yet match observer)
|
||||
flecs::entity e = ecs.entity("e")
|
||||
.set<Position>({10, 20});
|
||||
|
||||
// Set Velocity (emits EcsOnSet, matches observer)
|
||||
e.set<Velocity>({1, 2});
|
||||
|
||||
// Output
|
||||
// - OnSet: Velocity: e: p: {10, 20} v: {1, 2}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
#ifndef YIELD_EXISTING_H
|
||||
#define YIELD_EXISTING_H
|
||||
|
||||
/* This generated file contains includes for project dependencies */
|
||||
#include "yield_existing/bake_config.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
)
|
||||
(.)
|
||||
.|.
|
||||
| |
|
||||
_.--| |--._
|
||||
.-'; ;`-'& ; `&.
|
||||
\ & ; & &_/
|
||||
|"""---...---"""|
|
||||
\ | | | | | | | /
|
||||
`---.|.|.|.---'
|
||||
|
||||
* This file is generated by bake.lang.c for your convenience. Headers of
|
||||
* dependencies will automatically show up in this file. Include bake_config.h
|
||||
* in your main project file. Do not edit! */
|
||||
|
||||
#ifndef YIELD_EXISTING_BAKE_CONFIG_H
|
||||
#define YIELD_EXISTING_BAKE_CONFIG_H
|
||||
|
||||
/* Headers of public dependencies */
|
||||
#include <flecs.h>
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"id": "yield_existing",
|
||||
"type": "application",
|
||||
"value": {
|
||||
"use": [
|
||||
"flecs"
|
||||
],
|
||||
"language": "c++",
|
||||
"public": false
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
#include <yield_existing.h>
|
||||
#include <iostream>
|
||||
|
||||
// Observers can enable a "yield_existing" feature that upon creation of the
|
||||
// observer produces events for all entities that match the observer query. The
|
||||
// feature is only implemented for the builtin EcsOnAdd and EcsOnSet events.
|
||||
//
|
||||
// Custom events can also implement behavior for yield_existing by adding the
|
||||
// Iterable component to the event (see EcsIterable for more details).
|
||||
|
||||
struct Position {
|
||||
double x, y;
|
||||
};
|
||||
|
||||
int main(int, char *[]) {
|
||||
flecs::world ecs;
|
||||
|
||||
// Create existing entities with Position component
|
||||
ecs.entity("e1").set<Position>({10, 20});
|
||||
ecs.entity("e2").set<Position>({20, 30});
|
||||
|
||||
// Create an observer for three events
|
||||
ecs.observer<Position>()
|
||||
.event(flecs::OnSet)
|
||||
.yield_existing() // Trigger for existing matching entities
|
||||
.each([](flecs::iter& it, size_t i, Position& p) {
|
||||
std::cout << " - " << it.event().name() << ": "
|
||||
<< it.event_id().str() << ": "
|
||||
<< it.entity(i).name()
|
||||
<< ": {" << p.x << ", " << p.y << "}\n";
|
||||
});
|
||||
|
||||
// Output
|
||||
// - OnSet: Position: e1: {10, 20}
|
||||
// - OnSet: Position: e2: {20, 30}
|
||||
}
|
||||
Reference in New Issue
Block a user