Help with your scripting

Setting Up Scene from XML

Setting Up Scene from XML

Postby Shando » 13 Mar 2013, 04:29

Hi,

Just wondering if it is possible to set up the Scene when the Game starts via XML?

For example, I would like to set up a board at the start of every Game that is pseudo-random (see this image - https://skydrive.live.com/redir?resid=738BB67A6530C3EE!368&authkey=!AJ3qWQaRM4puG68).

The XML file would contain the following data:

x value
y value
z value
type (either Square or Octagon based on 1 = Square & 0 = Octagon)
texture (as a String)

When the XML file is read I would like to create the board based on these values and apply textures to the Squares/Octagons based on the texture parameter. That way I could create several of these files and select which one I want to load randomly at the start of each Game.

Any help would be greatly appreciated.

Regards

Shando

PS: I could also see a use for this for Games like Mahjong
PPS: It doesn't have to be XML, it could be a link to a database (such as SQL) or a CSV file
User avatar
Shando
Skyline Moderator
Skyline Moderator
 
Posts: 560
Joined: 06 Mar 2013, 22:35
Location: Moffat Beach, Queensland
Skill: Programmer
Skill: Scripter
Skill: Level Designer

Re: Setting Up Scene from XML

Postby Shando » 13 Mar 2013, 07:56

Would the following work for importing data from a CSV file?????

Code: Select all
loadCSV ( "H:\Aurasoft\MapData.csv" )

function loadCSV ( sCSV )
   local tTab={};

   for sLine in io.lines ( sCSV ) do
      tTab [ #tTab + 1 ] = fromCSV ( sLine );
   end
end

-- Convert from CSV string to table (converts a single line of a CSV file)
function fromCSV ( sLine )
   s = s .. ",";        -- ending comma
   local t = {};        -- table to collect fields
   local fieldstart = 1;
 
   repeat
      local nexti = string.find ( sLine, ",", fieldstart );
    
      table.insert ( t, string.sub ( sLine, fieldstart, nexti - 1 ) );
         
      fieldstart = nexti + 1;
   until fieldstart > string.len ( sLine )
 
   return t;
end


Thanks

Shando
User avatar
Shando
Skyline Moderator
Skyline Moderator
 
Posts: 560
Joined: 06 Mar 2013, 22:35
Location: Moffat Beach, Queensland
Skill: Programmer
Skill: Scripter
Skill: Level Designer

Re: Setting Up Scene from XML

Postby StarFire » 13 Mar 2013, 10:10

We have included many base Lua libraries with Skyline so please experiment with standard (non skyline) lua and let us know how you get on.

We can also write an xml wrapper for sky lua or/and add file io. I will have a think about this today and see if we can get a solution in for the next release for you ;)

I have given it a quick try:

Code: Select all
function onInit(objID)
   sky.lprint("LUA: Script Active!");
   file = io.open("Skyline.log")

   if file == nil then
      sky.lprint("NO FILE")
   else
      sky.lprint("YES FILE")
   end
end


This finds a file if it exists. I will see about parsing a file.....
User avatar
StarFire
Skyline Founder
Skyline Founder
 
Posts: 1678
Joined: 03 Jan 2012, 18:50
Location: UK
Skill: Great creative
Skill: Programmer
Skill: 3D Modeller
Skill: 2D Artist
Skill: Level Designer

Re: Setting Up Scene from XML

Postby StarFire » 13 Mar 2013, 10:19

If we add:

Code: Select all
--read file contents to a string
str = file:read("*all")
sky.lprint(str)


we can read the contents of the file and print out to the console.
So yes with a bit of experimentation you should be able to create a file parser directly from lua :D

If you need any help doing this let us know and we will help out. If you find a cool result, it would be cool to see what you have come up with. ;)
User avatar
StarFire
Skyline Founder
Skyline Founder
 
Posts: 1678
Joined: 03 Jan 2012, 18:50
Location: UK
Skill: Great creative
Skill: Programmer
Skill: 3D Modeller
Skill: 2D Artist
Skill: Level Designer

Re: Setting Up Scene from XML

Postby Shando » 14 Mar 2013, 01:09

Hi all,

After several attempts at understanding how Lua's file io works, I have come up with the following code for extracting data from csv files:

Code: Select all
-- |------------------------------------------------------------------------------------
-- | .: S K Y L I N E :.    Scripts to read CSV file and extract items of data         
-- |------------------------------------------------------------------------------------

function onKeyDown ( key )
   if ( key == "p" ) then
      loadCSV ( );
   end
end

-- |------------------------------------------------------------------------------------
-- | .: S K Y L I N E :.    loadCSV ( ) function    - main function      
-- |------------------------------------------------------------------------------------
function loadCSV ( )
   local tTab={};
   local aa, bb, cc, dd, ee
   local yy, zz
   
   tTab = readCSV( "H:\\Aurasoft\\MapData.csv" )

-- Get size of tTab table
   yy = table.getn(tTab)

-- Loop through tTab and populate aa, bb etc.
   for zz = 1, yy, 5 do
      aa = tTab[zz]
      bb = tTab[zz + 1]
      cc = tTab[zz + 2]
      dd = tTab[zz + 3]
      ee = tTab[zz + 4]
sky.lprint ( "" .. aa )
sky.lprint ( "" .. bb )
sky.lprint ( "" .. cc )
sky.lprint ( "" .. dd )
sky.lprint ( "" .. ee )
   end
end

-- |------------------------------------------------------------------------------------
-- | .: S K Y L I N E :.    readCSV ( sFile ) function - reads csv file         
-- |------------------------------------------------------------------------------------
function readCSV ( sFile )
   local tCSV = {}
   local tTemp = { }
   
-- Calculate number of lines in CSV file
   local file = io.open( sFile, "r" )
   local length = 0

   while file:read() ~= nil do
      length = length + 1
   end

   io.close ( file )

   file = io.open ( sFile, "r" )
   local sLine = file:read()

-- Ignore CSV header line
   sLine = file:read()

-- Extract all other lines
   while sLine do
      tTemp = fromCSV ( sLine )
      
      for aa = 1, table.getn ( tTemp ) do
         table.insert ( tCSV, tTemp[aa] )
      end
      
      sLine = file:read()
   end

-- Close file and return new table tCSV
   io.close ( file )
   return tCSV
end

-- |------------------------------------------------------------------------------------
-- | .: S K Y L I N E :.    fromCSV ( sLine ) function - extracts data from line         
-- |------------------------------------------------------------------------------------
function fromCSV ( sLine )
-- Add comma to end of line
   sLine = sLine .. ","
   local tTable = {}
   local fieldstart = 1

-- Loop through line checking for commas, and extract subString
   repeat
      local nexti = string.find ( sLine, ",", fieldstart )
      local sSub = string.sub(sLine, fieldstart, nexti - 1)
      
      table.insert ( tTable, sSub )
      
      fieldstart = nexti + 1
   until fieldstart > string.len ( sLine )

-- Return table containing values from line as separate entries
   return tTable
end


The above scripts should be fairly self-explanatory, but feel free to ask questions if there's something you don't understand.

Regards

Shando

PS: these could probably be simplified further with some judicious use of Global Vars etc. 8-)
User avatar
Shando
Skyline Moderator
Skyline Moderator
 
Posts: 560
Joined: 06 Mar 2013, 22:35
Location: Moffat Beach, Queensland
Skill: Programmer
Skill: Scripter
Skill: Level Designer

Re: Setting Up Scene from XML

Postby StarFire » 14 Mar 2013, 09:58

Hey Shando thank you for posting your solution we will have a play with the script later today :D

Very tidy code, + good to see the lua tables are working ;)

This will hopefully be useful to many other users as it solves a common problem for many game styles. Thanks again for posting back :D
User avatar
StarFire
Skyline Founder
Skyline Founder
 
Posts: 1678
Joined: 03 Jan 2012, 18:50
Location: UK
Skill: Great creative
Skill: Programmer
Skill: 3D Modeller
Skill: 2D Artist
Skill: Level Designer

Re: Setting Up Scene from XML

Postby StarFire » 14 Mar 2013, 10:28

Just had a play a play with the code, well done mate =)

We have added it to the lua examples folder for the next release and added your credits to the header ;)

I also noticed you set it to miss the first line in the file, was this by design?
User avatar
StarFire
Skyline Founder
Skyline Founder
 
Posts: 1678
Joined: 03 Jan 2012, 18:50
Location: UK
Skill: Great creative
Skill: Programmer
Skill: 3D Modeller
Skill: 2D Artist
Skill: Level Designer

Re: Setting Up Scene from XML

Postby Shando » 14 Mar 2013, 12:18

Hi NightHawk,

Thanks :)

Yes, I deliberately left out the first line as it's the Header and I didn't need the info for what I wanted.

Regards

Shando
User avatar
Shando
Skyline Moderator
Skyline Moderator
 
Posts: 560
Joined: 06 Mar 2013, 22:35
Location: Moffat Beach, Queensland
Skill: Programmer
Skill: Scripter
Skill: Level Designer


Return to Lua Scripting

Who is online

Users browsing this forum: No registered users and 5 guests

cron