Jump to content

Tiled: how to store an array?


toto88x
 Share

Recommended Posts

Hi,

 

I'm using tiled for one of my game, to store the levels of a platformer.

 

I'd like to add some metadata to each of my levels. More specifically, I'd like to have an array containing some values. So:

- How can I store an array into a Tiled map?

- And how can I extract the values with Phaser?

 

Thanks!

 

 

Edit: I updated my question below, see http://www.html5gamedevs.com/topic/7168-tiled-how-to-store-an-array/#entry42782

Link to comment
Share on other sites

Could you not just save some JSON files with a similar filename to the levels, and then when you load the levels also load the JSON file using game.load.json and read the extra metadata from that? That's the simplest method I can think of which lets you keep the data separate from your game code. You could of course manually edit the files Tiled exports, but you'd then have to parse out the metadata and pass the rest of the map data to Phaser's tilemap parsing functions.

Link to comment
Share on other sites

The solution for this question is tricky, because imho this kind of data should be able to set at the tiled editor to the json it generates (like you wanted) but to my understanding tiled doesn't support setting "array data". I actually tried it with last game but it takes only strings as map properties... so if I wanted "[1,2,3]" I used "1,2,3" and my own parser to extract those values from there for that map. (ofc. there is a possibility that you might be able to save something as "[1,2,3]" (note string) and turn it to a real array using eval? or something... well, it is tricky anyway.

 

I used to set my custom properties called "collides" using (in tiled) Map -> Map properties -> Add some variable like: "collides" and value: 19,23,18,28

 

Now when you save the example_map.json and open it there should be something like:

 

..."properties":  {    "collides":"19,23,18,28"  }...

Rest is pretty easy actually, you just read the properties of json file... (all of your properties) and do what you want.

 

Here is the example code how stuff works:

 

// Faking example_map.json load which you would create using tiled//// You would access this in your game with e.g. //// this.load.json("mapJson", "../assets/maps/example_map.json");// ...// var exampleJson = self.cache.getJSON("mapJson");var exampleJson = JSON.parse('{"height":3,"layers":[{"data":[0,0,0,0,0,0,0,0,0],"height":3,"name":"Example_layer","opacity":1,"type":"tilelayer","visible":true,"width":3,"x":0,"y":0}],"orientation":"orthogonal","properties":{"collides":"19,23,18,28"},"tileheight":80,"tilesets":[],"tilewidth":80,"version":1,"width":3}');/** * * */var parseMap = function(mapJson) {      // handle your map properties, now only collides  // fetching the block tiles  var _collides = mapJson.properties.collides.split(",");      // from string to number  for (var i = 0; i < _collides.length; i++) {    _collides[i] = parseInt(_collides[i], 10);  }      // do what ever you want with your array  // will print [19, 23, 18, 28] to console  console.log(_collides);};// demoparseMap(exampleJson);

And here is the jsfiddle to show that: http://jsfiddle.net/Rtp4E/1/

 

------------------------------------------------------------------------

 

Another solution would be like lewster32 pointed out to use separate json files, but I didnt prefer it because then your map data is set in two different files (I like one file).. however, I would do the same if metadata of certain map gets too complex to set it inside tiled.

 

Cheers. hopefully, this fast coding madness clarifies something to you.  :D

 

 

Link to comment
Share on other sites

Thanks for your answers! I specify a little more my question:

 

The array I wanted to store is actually a list of x and y positions, where I may want to display an enemy that appears randomly.

So maybe, instead of storing the array, it's easier to actually add objects to the tilemap at each position where the enemy could appear?

 

If so, how can I extract with Phaser all the possible positions, and then store them in an array?

 

Thanks!

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...