How to set up game levels based on data from a text file: using the ParamMap.
This tutorial gives an example of how to create game levels using the ParamMap-type parameters available in the HTML5 Game Editor from WiMi5. These parameters allow us to, for example, be able to create a level-based game automatically. In this type of game, its logic is created with WiMi5’s proprietary visual scripting system and the levels are set up via a series of data extracted from the ParamMap.
To give some context to this concept in a game we can look at the example project StunPig, although to simplify and make the tutorial a bit easier to understand, instead of focusing on the game, we’re going to create an example using some scripts and resources from it, but skipping over everything the game has which doesn’t teach us anything.
The values to be loaded which will define each game level are the number of little pigs to hit numOfPigs, the time to hit all the little pigs numOfMilliseconds, and the points to add for each pig eliminated pointsByPig, with there being as many game levels as defined value groups.
Contents of the text file that defines the levels (4 in this case)
We can clone the example project ParametrizedRoundByJSON from the WiMi5 dashboard. At the same time, we can load the data file with the levels’ configurations and show their content on the screen. To start, we show the first level’s setup and as we activate the devices (mouse, space bar, or touchscreen), we can see the setup for the next level, until all the level configurations have been used and we show “Game Over”.
As for resources, we will use the counter we built in the How to Implement Counters (Lives, Time, and Points) tutorial, which is also used in the “StunPig” game, adding some more information to it for the example. We’ll use it to show the points to add and the time duration defined for each level. On the other hand, we’ll use the little pigs (sprites) from “StunPig” to randomly place as many of them on the screen as there are defined by the level.
In our example, we will create a base Script to manage the loaded setup, which we’ll call RoundConfigManager. At the same time, we’ll load up the definition of the levels via a ParamMap and an associated JSON file. The Script has two activators, provideFirst and provideNext. The provideFirst input provides the three parameters that the script presents with the number of pigs, the milliseconds, and the points per pig for the first level and the provideNext input provides the three parameters the script presents with the number of pigs, the milliseconds, and the points per pig of the next level. When the parameters have been loaded with new data, the provided output is triggered and if there are no data to be provided, the complete output is triggered.
The source of the data is a text file with the extension .json, which can be pre-loaded. To access the data it contains which define the levels, we use a ParamMap-type parameter, which means we’ll have to have uploaded the file to the cloud (to our space on the server), which will generate a new element on the list of ParamMaps in the AssetManager. If we wanted to change or update the data in the text file once it’s been uploaded, it’s necessary to upload the text file again with the same name and extension so that when the file is imported, the AssetManager will overwrite the old file. In any case, the ParamMap that loads the data from the file can be modified when the game logic is run.
We use this by dragging the element created in the list to the script. This will generate an ActionOnParam blackbox which we use to extract a collection of paramMaps, assigning it the getItems action. Later, we will iterate this for the Collection we extract and which contains the definition for each of the levels.
To implement the Script RoundConfigManager, we will build a graph from which we will take from the ParamMap a list with the data from each level in a Collection-type parameter using an ActionOnParam blackbox with the getItem action. We will run through the Collection making use of a Traverse Blackbox for Collections. In each iteration on the Collection, we have a new ParamMap with the data extracted for the level in question. This means that we in turn make use of the GetItems blackbox of the ParamMaps in order to extract the data we’re looking for: the number of pigs, the number of milliseconds, and the number of points per pig. We put these values in the roundPigs, roundMillisecs, and pointsToWin parameters presented by the Script.
With these data extracted, we will now be able to do what it takes to define the level to be shown, making use of the same Scripts which implemented the construction of the level.
In the case of our example, we simply show the time and the points on the screen, and on the screen we randomly position as many pig faces as are defined as the number for this level. But in a more complex game, we would define more complex concepts, different characters or enemies to be shown, elements to position, damage to cause, etc. by level, which can be defined by the level so that we can build the level automatically; of course, for that, we will require the necessary resources for the scene of the project, and the pertinent logic to manage it.
In this very simple example, at the initial activation of the onLoaded, the provideFirst input is triggered in order to get the data for the first level and for the Script that listens for devices (keyboard, mouse, touchscreen), which will trigger in these activations a new petition of data for the next level. When the data for a level are available, the provided output from the RoundConfigManager Script will be triggered. This activates the scripts to show the data on the screen. But when there are no more data for more levels, the completed output is triggered, which activates an ActionOnParam-type blackbox called showGameOver with the setVisibility action over a bitmap, to show the Game Over image on screen.
With these instructions we have summarized the basic characteristics about the use of the ParamMaps. Remember that you can ask any questions in the help section of the WiMi5 forum. We’ll be happy to resolve your issues and help you create great games.14/11/2014 / 1 Comment
- How to Integrate a Game into Your Website
- How to integrate a WiMi5 game in Moodle
- How to create texts from sprites using a SpriteText Blackbox
- How to clone a project
- New ways of interacting in wimi5
- How to easily adapt WiMi5 game templates to create your own games
- How to monetize your games using Virtual Goods
- How to use the CodeRunner blackbox
- How to use Game Session Storage
- How to Use Rankings
- How to publish your HTML5 game to other game platforms
- How to publish your HTML5 game on the Google Chrome Web Store
- How to set up game levels based on data from a text file: using the ParamMap.
- How to create a scoreboard for lives, time, and points
- How to Make a Scroll and a Parallax Scroll in 2D Games
- How to create collisions
- How to debug in WiMi5