Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Tiled to SFML using lua.  (Read 2248 times)

0 Members and 1 Guest are viewing this topic.

piluve

  • Newbie
  • *
  • Posts: 7
    • View Profile
    • Email
Tiled to SFML using lua.
« on: November 25, 2015, 11:08:41 pm »
Hey guys!

I found Tiled (http://www.mapeditor.org/) quite a while ago (I was using Unity) and found it really useful.

I want to be able to load "tiled.lua" files into my C++ program.

I am using LuaBridge(https://github.com/vinniefalco/LuaBridge) to do most of the heavy lifting.

But now I am stuck.
Let me show you how a Tiled map looks in lua:

bigtable
{
  version = "2.2.2.2.2"
  luaversion = "0.0.0.0.1"
  tilesets
  {
    name = "Rock"
    ...
    name = "Grass"
  }
  layers
  {
    data{0,0,0,0,0,0,0,0,0,1,2,3,4}
    ...
  }
}

So yeah it is a lot of information in a table.

Using LuaBridge you can use: getGlobal(luaState,"key") to get a handler to that key value and then cast it into the apropiate type.
But,how do I "get" things without using a name(a char*) just using an id for example like:

lua_State* L = luaL_newstate();
luaL_dofile(L, "../data/scripts/untitled.lua");
luaL_openlibs(L);
lua_pcall(L, 0, 0, 0);
LuaRef globalData = getGlobal(L, "globalData");
LuaRef someKey = getGlobal(L,2);//How to do this

I am not sure how it should be done :C

Sorry it the answer is really obvious but I can´t find it :C
« Last Edit: November 25, 2015, 11:14:48 pm by piluve »

GraphicsWhale

  • Full Member
  • ***
  • Posts: 131
    • View Profile
Re: Tiled to SFML using lua.
« Reply #1 on: November 26, 2015, 06:49:25 am »
You can't assign a value to a number in Lua.

While what you're asking is not clear (since it's impossible), assuming what you're looking for is to get the value in a table using a number as an index:

lua_getglobal(L, "global_table"); //Push the table to the top of the stack

/*
    The following three lines are equivalent to this in Lua:

    global_table[4] = 2000
*/

lua_pushinteger(L, 4);
lua_pushinteger(L, 2000);
lua_settable(L, -3);

//Push global_table[4] to the top of the stack
lua_pushinteger(L, 4);
lua_gettable(L, -2);

std::cout << lua_tonumber(L, -1) << std::endl; //Should print 2000

lua_pop(L, 2); //Remove the value and table from the stack
 

« Last Edit: November 26, 2015, 07:09:35 am by GraphicsWhale »

piluve

  • Newbie
  • *
  • Posts: 7
    • View Profile
    • Email
Re: Tiled to SFML using lua.
« Reply #2 on: November 26, 2015, 09:54:48 am »
OK so,what you do is put the value you want at the top of the stack and then get it as an integer.

I will check the manpages and see if I can understand this.

Thx.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10901
    • View Profile
    • development blog
    • Email
Re: Tiled to SFML using lua.
« Reply #3 on: November 28, 2015, 01:17:45 am »
I think, things could be done a lot easier by exporting to JSON and use one of the hundred JSON libs (e.g. this) out there.
Or you could use fallahn's TMX loader.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

 

anything