SFML community forums

Help => General => Topic started by: piluve on November 25, 2015, 11:08:41 pm

Title: Tiled to SFML using lua.
Post by: piluve 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
Title: Re: Tiled to SFML using lua.
Post by: GraphicsWhale 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
 

Title: Re: Tiled to SFML using lua.
Post by: piluve 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.
Title: Re: Tiled to SFML using lua.
Post by: eXpl0it3r 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 (https://github.com/nlohmann/json)) out there.
Or you could use fallahn's TMX loader (https://github.com/fallahn/sfml-tmxloader).