Comments (5)
Hi @majonathany,
Hmm, that's interesting. I've never really tried to call swap from within a system to be honest, that's not where my head was going when I designed the component (not saying it's wrong or anything).
The swap
method is on the GameEngine component itself. Perhaps I should also make it an argument that gets passed into the systems - I'll have a think about it. In any case, here's how it was designed to be used:
const updatePhysics = (entities) => {
/* DO PHYSICS STUFF */
return entities;
}
const updateGame = (entities, { touches, dispatch }) => {
if ( /* SOME CONDITION */ ) {
dispatch({ type: "game-over"});
}
return entities;
}
const getEntities = () => {
return { /* SOME ENTITIES */ };
};
export default class App extends React.Component {
handleEvent = (ev) => {
if (ev.type === "game-over")
this.refs.engine.swap(getEntities());
};
render() {
return (
<GameEngine
systems={[updatePhysics, updateGame]}
entities={getEntities()}
style={{ paddingLeft: widthWindow * 0.05 }}
running={this.state.running}
ref="engine"
onEvent={this.handleEvent}
/>
);
}
}
Let me know if that helps!
from react-native-game-engine.
Glad it helped @majonathany!
I personally define the systems outside of the class definition because eventually, when your game becomes more complex, the number of systems will increase to the point where you will bloat your class.
I also find that keeping the systems outside of the class will stop the temptation to access class variables or properties. Systems should be treated almost as pure functions, and should only act on the provided entities and read/use the supplied args. The library provides ways to facilitate communication between your class and your systems (using the dispatch method as an example).
Finally, keeping your systems separate from your class will allow you to re-use your systems in other projects (via an npm module perhaps) and platforms (I've recently released a similar game engine for the web.
And yep, you are correct, the GameEngine supplies the entities and other args for the systems. This is what the second parameter (args) into the system calls contains:
let args = {
touches,
screen,
events,
dispatch,
time
};
Hope that helps!
from react-native-game-engine.
Wow, this worked!!! I was just missing the dispatch() method. Thanks for the prompt response! This is helpful information. I am new to all the libraries and JavaScript.
So, just a few questions - the methods that you have outside the class definition are systems, right? Can they go inside the class or do they have to defined outside the class? I know these are functions that you describe in the systems prop.
Also, for their parameters (entities, {touches, dispatch}), what function is passing these parameters? Where is entities, touches and dispatch coming from? Is it correct to say that they are keys that are essentially passed in as dictionaries that the GameEngine automatically provides? How do I know what other parameters these systems can support?
from react-native-game-engine.
This is immensely helpful. Where can I find an exhaustive list of the types of parameters that systems can take, besides entities and the five args? Is there anything else that a system can accept?
Finally, does the React Native Game Engine supply these programming interfaces, or did someone else pick those 5 arguments and determine what the inputs to those arguments would be?
from react-native-game-engine.
Hi @majonathany,
These two parameters (entities, args)
are supplied by the React Native Game Engine because I felt that they are general enough to be used in a lot of cases. I will expand them as required - but no immediate plans.
A useful pattern that I've used in my recent games is to write a system that injects other properties and functions into the args
that I know will be useful by the subsequent systems. This can be things like a controller
interface or whatever might be useful for your games. See this system for an example.
Hope that helps!
from react-native-game-engine.
Related Issues (20)
- Touch array is empty in iOS HOT 1
- Functions not in TypeScript definition files HOT 8
- How can i catch in Entity onPress event and value on System.js HOT 3
- Custom fonts used inside GameEngine not working
- Default timer doesn't stop when calling `.stop()` HOT 3
- Nested entity property change doesn't trigger rerender process HOT 3
- How to use TouchEvent for continious handelrs? HOT 1
- Systems functions
- FPS issues HOT 9
- Add animation for moving HOT 10
- Best Way to handle touch as directional control HOT 3
- Support for Expo SDK 43, 44, 45 HOT 7
- Cant handle multitouch operations and it is really slow HOT 4
- How to change Game Engine's coordinate ?
- Tilemap free pan
- while dragging create line like WordSearch game
- What type of game can i build???
- is there anyone who is maintaining this library yet? HOT 2
- GameEngine re-rendering on entities change HOT 3
- Types not importing; not a real fix, but raising the issue HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react-native-game-engine.