Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

How to access custom properties of tiled in corona?
Started by architectvijeshkumarv Apr 28 2018 09:50 PM

8 replies to this topic

Best Answer ldurniat , 29 April 2018 - 01:46 AM

You can use Ponytiled and Berry libs for this purposes like that

----------------------------------------
-- Ponytiled library --
-- https://github.com/ponywolf/ponytiled
----------------------------------------
-- Load map
local data   = json.decodeFile( system.pathForFile( "map.json", system.ResourceDirectory ) )
local map    = tiled.new( data )
map.x, map.y = display.contentCenterX - map.designedWidth / 2, display.contentCenterY - map.designedHeight / 2

-- Find character
local hero = map:findObject( 'hero' )
-- Print custom property for tiled object
print( hero.propertyName )
----------------------------------------



----------------------------------------
-- Berry library --
-- https://github.com/ldurniat/Berry
----------------------------------------

local map = berry.loadMap( 'map.json' )
local visual = berry.createVisual( map )

-- Find character
local heroRepresentation = map:getObjectWithName( 'hero' )
local hero = heroRepresentation:getVisual()
-- Print custom property for tiled object
print( hero.propertyName )

-- Get collision shape for hero
local tileset = map:getTileSet( 'sprites' )
local shapeForHero = tileset:getCollisionShapeForTile( heroRepresentation.tileid ) 


print( json.prettify( shapeForHero.polygon ) )

I prepared simple project to demostrate how it may works.

 

 

Note:

  • Ponytiled supports json and lua but berry supports only json,
  • As I know Ponytiled don't support custom collision shapes

I found myself that Ponytiled is great library :) You may want also check Dusk but I don't use it. 

 

Berry is my own project.

 

ldurniat

[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

architectvijeshkumarv

[GLOBAL: userInfoPane.html]
architectvijeshkumarv
  • Enthusiast

  • 45 posts
  • Corona SDK

How to access the custom properties of tiled in corona? For example the collision shape as polygon? or hitpoints?



[TOPIC: post.html]
#2

architectvijeshkumarv

[GLOBAL: userInfoPane.html]
architectvijeshkumarv
  • Enthusiast

  • 45 posts
  • Corona SDK

return {
  name = "demotrialset",
  tilewidth = 800,
  tileheight = 250,
  spacing = 0,
  margin = 0,
  tileoffset = {
    x = 0,
    y = 0
  },
  grid = {
    orientation = "orthogonal",
    width = 1,
    height = 1
  },
  properties = {},
  terrains = {},
  tilecount = 11,
  tiles = {
    {
      id = 0,
      image = "../trial/9659745dbad5e81.jpg",
      width = 800,
      height = 250
    },
    {
      id = 1,
      image = "../trial/alter.png",
      width = 160,
      height = 192
    },
    {
      id = 2,
      image = "../trial/column1.png",
      width = 128,
      height = 192
    },
    {
      id = 3,
      image = "../trial/column2.png",
      width = 128,
      height = 192
    },
    {
      id = 4,
      image = "../trial/doorBlueStroked.png",
      width = 160,
      height = 192
    },
    {
      id = 5,
      image = "../trial/flare.png",
      width = 192,
      height = 192
    },
    {
      id = 6,
      type = "gem",
      properties = {
        ["points"] = 15
      },
      image = "../trial/gemBlueStroked.png",
      width = 64,
      height = 64,
      objectGroup = {
        type = "objectgroup",
        name = "",
        visible = true,
        opacity = 1,
        offsetx = 0,
        offsety = 0,
        draworder = "index",
        properties = {},
        objects = {
          {
            id = 1,
            name = "",
            type = "",
            shape = "polygon",
            x = 31,
            y = 61,
            width = 0,
            height = 0,
            rotation = 0,
            visible = true,
            polygon = {
              { x = 0, y = 0 },
              { x = -30.75, y = -38.5 },
              { x = -22.75, y = -58.5 },
              { x = 23.5, y = -58.75 },
              { x = 30.75, y = -39.25 },
              { x = 0, y = -0.25 }
            },
            properties = {}
          }
        }
      }
    },
    {
      id = 7,
      type = "hero",
      properties = {
        ["anchorY"] = 0.5,
        ["bodyType"] = "dynamic",
        ["density"] = 0.5,
        ["friction"] = 1
      },
      image = "../trial/hero.png",
      width = 128,
      height = 160
    },
    {
      id = 8,
      type = "platform",
      properties = {
        ["bodyType"] = "static",
        ["density"] = 1,
        ["friction"] = 0.04999999999999982
      },
      image = "../trial/platformBase1.png",
      width = 256,
      height = 96,
      objectGroup = {
        type = "objectgroup",
        name = "",
        visible = true,
        opacity = 1,
        offsetx = 0,
        offsety = 0,
        draworder = "index",
        properties = {},
        objects = {
          {
            id = 1,
            name = "",
            type = "",
            shape = "polygon",
            x = 2,
            y = 2,
            width = 0,
            height = 0,
            rotation = 0,
            visible = true,
            polygon = {
              { x = 0, y = 0 },
              { x = -2, y = 61.3333 },
              { x = 254, y = 61.3333 },
              { x = 253.333, y = -1.33333 }
            },
            properties = {}
          }
        }
      }
    },
    {
      id = 9,
      type = "platform",
      properties = {
        ["bodyType"] = "static",
        ["density"] = 1,
        ["friction"] = 0.050000000000000044
      },
      image = "../trial/platformBase2.png",
      width = 256,
      height = 96,
      objectGroup = {
        type = "objectgroup",
        name = "",
        visible = true,
        opacity = 1,
        offsetx = 0,
        offsety = 0,
        draworder = "index",
        properties = {},
        objects = {
          {
            id = 1,
            name = "",
            type = "",
            shape = "polygon",
            x = -0.666667,
            y = 64,
            width = 0,
            height = 0,
            rotation = 0,
            visible = true,
            polygon = {
              { x = 0, y = 0 },
              { x = 0, y = -63.3333 },
              { x = 255.333, y = -63.3333 },
              { x = 256.667, y = -0.666667 }
            },
            properties = {}
          }
        }
      }
    },
    {
      id = 10,
      image = "../trial/skeleton.png",
      width = 133,
      height = 160
    }
  }
}

This is objects.lua I exported from tiled. How do I extract certain information like polygon for collision main?



[TOPIC: post.html]
#3

architectvijeshkumarv

[GLOBAL: userInfoPane.html]
architectvijeshkumarv
  • Enthusiast

  • 45 posts
  • Corona SDK

print(objects.tiles[7].properties["points"]) -- for retrieving points from there.

gemShape = (objects.tiles[7].objectGroup.objects[1].polygon) -- for retrieving gem polygon shape

 

any other easy method?



[TOPIC: post.html]
#4

ldurniat

[GLOBAL: userInfoPane.html]
ldurniat
  • Contributor

  • 278 posts
  • Corona SDK

  Best Answer

You can use Ponytiled and Berry libs for this purposes like that

----------------------------------------
-- Ponytiled library --
-- https://github.com/ponywolf/ponytiled
----------------------------------------
-- Load map
local data   = json.decodeFile( system.pathForFile( "map.json", system.ResourceDirectory ) )
local map    = tiled.new( data )
map.x, map.y = display.contentCenterX - map.designedWidth / 2, display.contentCenterY - map.designedHeight / 2

-- Find character
local hero = map:findObject( 'hero' )
-- Print custom property for tiled object
print( hero.propertyName )
----------------------------------------



----------------------------------------
-- Berry library --
-- https://github.com/ldurniat/Berry
----------------------------------------

local map = berry.loadMap( 'map.json' )
local visual = berry.createVisual( map )

-- Find character
local heroRepresentation = map:getObjectWithName( 'hero' )
local hero = heroRepresentation:getVisual()
-- Print custom property for tiled object
print( hero.propertyName )

-- Get collision shape for hero
local tileset = map:getTileSet( 'sprites' )
local shapeForHero = tileset:getCollisionShapeForTile( heroRepresentation.tileid ) 


print( json.prettify( shapeForHero.polygon ) )

I prepared simple project to demostrate how it may works.

 

Attached File  maplibs.zip   58.35KB   1 downloads

 

Note:

  • Ponytiled supports json and lua but berry supports only json,
  • As I know Ponytiled don't support custom collision shapes

I found myself that Ponytiled is great library :) You may want also check Dusk but I don't use it. 

 

Berry is my own project.

 

ldurniat



[TOPIC: post.html]
#5

architectvijeshkumarv

[GLOBAL: userInfoPane.html]
architectvijeshkumarv
  • Enthusiast

  • 45 posts
  • Corona SDK

local berry = require( 'Berry-master.pl.ldurniat.berry' )

local map = berry.loadMap( "maps.trial.demo.json", "maps.trial" )
local visual = berry.createVisual( map )
berry.buildPhysical( map )

Error Noted below. Why?

 
16:35:51.060  Copyright © 2009-2017  C o r o n a   L a b s   I n c .
16:35:51.060  Version: 3.0.0
16:35:51.060  Build: 2017.3184
16:35:51.070  Platform: iPhone / x64 / 6.1 / ATI Radeon HD 5400 Series / 4.5.13399 Compatibility Profile Context 15.200.1062.1004 / 2017.3184 / en_US | US | en_US | en
16:35:51.070  Loading project from:   C:\Users\user\Documents\Corona Projects\ponytiled-master - Test
16:35:51.070  Project sandbox folder: C:\Users\user\AppData\Local\Corona Labs\Corona Simulator\Sandbox\ponytiled-master - test-3D9E7C65AC2A387CA8272061669E75AB\Documents
16:35:51.110  ERROR: Runtime error
16:35:51.110  module 'pl.ldurniat.lib.30log-clean' not found:
16:35:51.110  no field package.preload['pl.ldurniat.lib.30log-clean']
16:35:51.110  no file 'C:\Users\user\AppData\Roaming\Corona Labs\Corona Simulator\Plugins\pl\ldurniat\lib\30log-clean.lua'
16:35:51.110  no file 'C:\Users\user\Documents\Corona Projects\ponytiled-master - Test\pl\ldurniat\lib\30log-clean.lua'
16:35:51.110  no file 'C:\Program Files (x86)\Corona Labs\Corona\Resources\pl\ldurniat\lib\30log-clean.lua'
16:35:51.110  no file '.\pl\ldurniat\lib\30log-clean.lua'
16:35:51.110  no file 'C:\Program Files (x86)\Corona Labs\Corona\lua\pl\ldurniat\lib\30log-clean.lua'
16:35:51.110  no file 'C:\Program Files (x86)\Corona Labs\Corona\lua\pl\ldurniat\lib\30log-clean\init.lua'
16:35:51.110  no file 'C:\Program Files (x86)\Corona Labs\Corona\pl\ldurniat\lib\30log-clean.lua'
16:35:51.110  no file 'C:\Program Files (x86)\Corona Labs\Corona\pl\ldurniat\lib\30log-clean\init.lua'
16:35:51.110  no file 'C:\Users\user\AppData\Roaming\Corona Labs\Corona Simulator\Plugins\pl\ldurniat\lib\30log-clean.dll'
16:35:51.110  no file '.\pl\ldurniat\lib\30log-clean.dll'
16:35:51.110  no file 'C:\Program Files (x86)\Corona Labs\Corona\Resources\pl\ldurniat\lib\30log-clean.dll'
16:35:51.110  no file '.\pl\ldurniat\lib\30log-clean.dll'
16:35:51.110  no file 'C:\Program Files (x86)\Corona Labs\Corona\pl\ldurniat\lib\30log-clean.dll'
16:35:51.110  no file 'C:\Program Files (x86)\Corona Labs\Corona\loadall.dll'
16:35:51.110  no file 'C:\Users\user\AppData\Roaming\Corona Labs\Corona Simulator\Plugins\pl.dll'
16:35:51.110  no file '.\pl.dll'
16:35:51.110  no file 'C:\Program Files (x86)\Corona Labs\Corona\Resources\pl.dll'
16:35:51.110  no file '.\pl.dll'
16:35:51.110  no file 'C:\Program Files (x86)\Corona Labs\Corona\pl.dll'
16:35:51.110  no file 'C:\Program Files (x86)\Corona Labs\Corona\loadall.dll'
16:35:51.110  no file 'C:\Users\user\AppData\Roaming\Corona Labs\Corona Simulator\Plugins\pl.ldurniat.lib.30log-clean.dll'
16:35:51.110  no file '.\pl.ldurniat.lib.30log-clean.dll'
16:35:51.110  no file 'C:\Program Files (x86)\Corona Labs\Corona\Resources\pl.ldurniat.lib.30log-clean.dll'
16:35:51.110  no file '.\pl.ldurniat.lib.30log-clean.dll'
16:35:51.110  no file 'C:\Program Files (x86)\Corona Labs\Corona\pl.ldurniat.lib.30log-clean.dll'
16:35:51.110  no file 'C:\Program Files (x86)\Corona Labs\Corona\loadall.dll'
16:35:51.110  stack traceback:
16:35:51.110  [C]: in function 'require'
16:35:51.110  ?: in function 'require'
16:35:51.110  C:\Users\user\Documents\Corona Projects\ponytiled-master - Test\Berry-master\pl\ldurniat\berry.lua:16: in main chunk
16:35:51.110  [C]: in function 'require'
16:35:51.110  ?: in function 'require'
16:35:51.110  C:\Users\user\Documents\Corona Projects\ponytiled-master - Test\main.lua:4: in main chunk


[TOPIC: post.html]
#6

ldurniat

[GLOBAL: userInfoPane.html]
ldurniat
  • Contributor

  • 278 posts
  • Corona SDK

@architectvijeshkumarv:

 

local berry = require( 'Berry-master.pl.ldurniat.berry' )

 

You need preserve folders structure as it is and put it in project folder. The only way is use 

berry = require( 'pl.ldurniat.berry' ) -- berry is global variable

like in zip file in my first post. It should fix problem with missing 'pl.ldurniat.lib.30log-clean' module.

 

local map = berry.loadMap( "maps.trial.demo.json", "maps.trial" )

 

You need provide relative path using forward slashes for loadMap as argument.  Something like that

local map = berry.loadMap( 'scene/game/map/sandbox.json', 'scene/game/map' ) -- look for images in /scene/game/map/

Here (github project) you have example.

 

Hope it helps.

 

Sorry for my english. Berry is my first "big" project :)

 

 

ldurniat


Edited by ldurniat, 29 April 2018 - 08:24 AM.


[TOPIC: post.html]
#7

architectvijeshkumarv

[GLOBAL: userInfoPane.html]
architectvijeshkumarv
  • Enthusiast

  • 45 posts
  • Corona SDK

Thank you so much. Am trying it hard. I did now. Am not making the project in a scene, will that be a problem?. Am just trying out the possibilities. When I tried this again the error coming in is:

 

19:30:21.691  Copyright © 2009-2017  C o r o n a   L a b s   I n c .
19:30:21.691  Version: 3.0.0
19:30:21.691  Build: 2017.3184
19:30:21.701  Platform: iPhone / x64 / 6.1 / ATI Radeon HD 5400 Series / 4.5.13399 Compatibility Profile Context 15.200.1062.1004 / 2017.3184 / en_US | US | en_US | en
19:30:21.701  Loading project from:   C:\Users\user\Documents\Corona Projects\ponytiled-master - Test
19:30:21.701  Project sandbox folder: C:\Users\user\AppData\Local\Corona Labs\Corona Simulator\Sandbox\ponytiled-master - test-3D9E7C65AC2A387CA8272061669E75AB\Documents
19:30:21.761  0,0
19:30:21.761  -30.75,-38.5
19:30:21.761  -22.75,-58.5
19:30:21.761  23.5,-58.75
19:30:21.761  30.75,-39.25
19:30:21.761  0,-0.25
19:30:21.932  ERROR: Runtime error
19:30:21.932  C:\Users\user\Documents\Corona Projects\ponytiled-master - Test\pl\ldurniat\Map.lua:49: attempt to index global 'berry' (a nil value)
19:30:21.932  stack traceback:
19:30:21.932  C:\Users\user\Documents\Corona Projects\ponytiled-master - Test\pl\ldurniat\Map.lua:49: in function 'init'
19:30:21.932  C:\Users\user\Documents\Corona Projects\ponytiled-master - Test\pl\ldurniat\lib\30log-clean.lua:58: in function <C:\Users\user\Documents\Corona Projects\ponytiled-master - Test\pl\ldurniat\lib\30log-clean.lua:44>
19:30:21.932  (tail call): ?
19:30:21.932  (tail call): ?
19:30:21.932  (tail call): ?
19:30:21.932  C:\Users\user\Documents\Corona Projects\ponytiled-master - Test\main.lua:64: in main chunk


[TOPIC: post.html]
#8

ldurniat

[GLOBAL: userInfoPane.html]
ldurniat
  • Contributor

  • 278 posts
  • Corona SDK

 Am not making the project in a scene, will that be a problem?. Am just trying out the possibilities. 

 

 

I don't think so.

 

19:30:21.932  ERROR: Runtime error

19:30:21.932  C:\Users\user\Documents\Corona Projects\ponytiled-master - Test\pl\ldurniat\Map.lua:49: attempt to index global 'berry' (a nil value)

 

berry variable need to be global so use (without local key word)

berry = require( 'pl.ldurniat.berry' )

Should fix problem:)



[TOPIC: post.html]
#9

architectvijeshkumarv

[GLOBAL: userInfoPane.html]
architectvijeshkumarv
  • Enthusiast

  • 45 posts
  • Corona SDK

I don't think so.

 

 

berry variable need to be global so use (without local key word)

berry = require( 'pl.ldurniat.berry' )

Should fix problem:)

thanks. I will check




[topic_controls]
[/topic_controls]