×

Langue

Fermer
Atelier 801
  • Forums
  • Dev Tracker
  • Connexion
    • English Français
      Português do Brasil Español
      Türkçe Polski
      Magyar Română
      العربية Skandinavisk
      Nederlands Deutsch
      Bahasa Indonesia Русский
      中文 Filipino
      Lietuvių kalba 日本語
      Suomi עברית
      Italiano Česky
      Hrvatski Slovensky
      Български Latviešu
      Estonian
  • Langue
  • Forums
  • /
  • Transformice
  • /
  • Modules
  • /
  • Reading XML in Lua
Reading XML in Lua
Pagoda
« Citoyen »
1676454000000
    • Pagoda#4165
    • Profil
    • Derniers messages
    • Tribu
#1
  0
Hi All

So I have been helping my friend fixing his module, as it stands the problem is fixed but it makes it very inconvenient for him when loading maps.

Basically, he is loading Defilante objects into the map via Lua, but doing it one-by-one is difficult. He still has the defilante objects in map XML so I just want to hear from people's experience messing with XML in Lua.

After loading the map into a variable, what is the best way to parse that XML, so that each Defilante object can be placed into the map by Lua?

Thanks!
Pagoda
« Citoyen »
1676456760000
    • Pagoda#4165
    • Profil
    • Derniers messages
    • Tribu
#2
  0
To make my idea clearer, this is the sort of end result I would want:

1
2
3
4
5
6
7
8
9
10
XMLmap = "<xml/><blah><blaha/>...."

function parseDefilanteObjects()
.....
return objectList[]
end

for i in parseDefilanteObjects(XMLmap) as object
tfm.exec.addBonus(object[i])
end

And it would place all of the Defilante objects into the map when a new round starts, or when the mouse dies.

P.S. Forgive me if the Lua code is wrong, this is just an example of how it would work
Wizardamani0
« Censeur »
1676461740000
    • Wizardamani0#6277
    • Profil
    • Derniers messages
#3
  0
Give an example please.
Pagoda
« Citoyen »
1676475960000
    • Pagoda#4165
    • Profil
    • Derniers messages
    • Tribu
#4
  0
Wizardamani0 a dit :
Give an example please.

Hi Wizardman I just want anybody's tips about parsing XML. I imagine it would be like string split as seen in this tutorial:

https://www.tutorialspoint.com/how-to-split-a-string-in-lua-programming

Is it possible to use that to parse the XML? I just want some tips to deal with it
Pagoda
« Citoyen »
1676477400000
    • Pagoda#4165
    • Profil
    • Derniers messages
    • Tribu
#5
  0
Hey I found some people online have written about it in the past so I think I'll follow their advice. I just checked string.gmatch() works within Transformice.
Bomboffkiler
« Citoyen »
1676559360000
    • Bomboffkiler#0000
    • Profil
    • Derniers messages
    • Tribu
#6
  0
Considering the XML isn't complex, simple matching with regular expressions works really. Note that Lua's regular expressions don't look like JavaScript ones (JavaScript RegExp is based in Perl). You can parse the XML string more in depth (like, Code Point by Code Point or match by match).
Deletedaccount
« Citoyen »
1676744100000
    • Deletedaccount#6153
    • Profil
    • Derniers messages
    • Tribu
#7
  0
https://atelier801.com/topic?f=6&t=383230&p=16#m307
Laiba
« Sénateur »
1676748120000
    • Laiba#5965
    • Profil
    • Derniers messages
#8
  1
This>>>>https://atelier801.com/topic?f=6&t=383230&p=16#m307


Indexinel gave

Dernière modification le 1676748180000
Pagoda
« Citoyen »
1677085680000
    • Pagoda#4165
    • Profil
    • Derniers messages
    • Tribu
#9
  0
Hey guys, thanks for your help.

This is the code I created which will parse the Defilante objects.

I had trouble with converting it from Regex to the Lua patterns but this works fine

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
xml = ""

function eventNewGame()
-- Load map XML
xml = tfm.get.room.xmlMapInfo.xml

-- Remove bonuses from previous round
tfm.exec.removeBonus()

-- Go through each object in the XML file and add the Defilante bonus
for object in xml:gmatch("<O(.-)%/>") do
type = object:match('C="(.-)"')
x = object:match('X="(.-)"')
y = object:match('Y="(.-)"')

-- If ID of object is 6 (Ball) then add a spring bonus (ID 3)
if type == "6" then
tfm.exec.addBonus(3, x, y)
end
end
end

tfm.exec.newGame("@7927594", true)
  • Forums
  • /
  • Transformice
  • /
  • Modules
  • /
  • Reading XML in Lua
© Atelier801 2018

Equipe Conditions Générales d'Utilisation Politique de Confidentialité Contact

Version 1.27