Author Topic: XML in QB64?  (Read 2259 times)

0 Members and 1 Guest are viewing this topic.

Offline Nate5700

  • Newbie
  • Posts: 10
    • View Profile
XML in QB64?
« on: May 26, 2020, 09:51:24 pm »
Hello again all,

I am thinking about the game I've started and how to design levels for it. One thing I've done that seems to work well in other languages is to use XML to describe a level to a program, and have the program "build" the level from the XML file. Is a QB64 XML library a thing? Or would I need to use a C library for this purpose?

I looked at the page on the wiki about using C libraries and I started getting a little dizzy trying to understand it. But the way I learn is by trying and doing, so I can probably figure that out. But if there's already a QB64 library I can use, let's go with that then.

Thanks in advance for any help you can provide!

Offline Nate5700

  • Newbie
  • Posts: 10
    • View Profile
Re: XML in QB64?
« Reply #1 on: May 26, 2020, 11:55:03 pm »
OK so maybe I'm misunderstanding the use of C libraries in QB64. Is it that QB64 has support for certain C libraries, or they are already built in?

I found an XML library consisting of a ".c" and a ".h" file. I tried putting "DECLARE LIBRARY ".\xml_parse_lib"" into a test program and QB64 actually crashed on me every time I tried to do it. Is this a bug, something I did wrong, or is it just that this is something QB64 isn't supposed to do? You would think the program would just flag it and say "nope, can't do that" instead of crashing though.

Guess I need to find another method. XML is convenient but it certainly isn't the only way to do things. I was just hoping not to have to write something to parse strings myself, so that I can actually worry about programming a game. Oh well.

EDIT: So I did some more reading and discovered the use of OPEN and INPUT for reading files, so I can do basically what I wanted to do with the XML as comma-separated files instead. Gosh, I keep answering my own questions.

XML would still be nice as an intuitive way for a human to read the file. But not critical for what I'm doing right now.
« Last Edit: May 27, 2020, 12:51:53 am by Nate5700 »

FellippeHeitor

  • Guest
Re: XML in QB64?
« Reply #2 on: May 27, 2020, 01:07:55 am »
Quote
Gosh, I keep answering my own questions.

  [ You are not allowed to view this attachment ]  

Ok, inspirational BS aside, glad to hear you figured it out before we had time to come to the rescue. Let us know how we can assist you with any part of OPEN/INPUT stuff.

Offline Nate5700

  • Newbie
  • Posts: 10
    • View Profile
Re: XML in QB64?
« Reply #3 on: May 27, 2020, 12:37:14 pm »
Thanks Fellippe, I'll take you up on your offer to help actually.

Let us know how we can assist you with any part of OPEN/INPUT stuff.

Is there any way that INPUT can detect the end of a line in a file? I didn't see anything in the wiki on that and playing with it, it looks like it just moves to the first value on the next line when it encounters the end of a line.

What I'm trying to do is write a sub that takes a line from the file that describes an object or obstacle in the game. The artwork I'm using from OpenGameArt doesn't seem to lend itself well to tiling in a logical order, so for each object I'd like to have values that describe which image tiles to use when displaying the object. Because of that, the lines for each object might not be a fixed length.

The workaround I have right now is that in the file, the last field for each line is a string, and then you put the tile numbers in that string, which keeps the line with a fixed number of values. I wrote a sub to parse the string to figure out what tiles to use, but it would seem more efficient just to grab the values from the file instead of having to parse a string.

FellippeHeitor

  • Guest
Re: XML in QB64?
« Reply #4 on: May 27, 2020, 12:46:22 pm »
With LINE INPUT you'll read a full line at once.

Offline Nate5700

  • Newbie
  • Posts: 10
    • View Profile
Re: XML in QB64?
« Reply #5 on: May 27, 2020, 01:29:54 pm »
With LINE INPUT you'll read a full line at once.

Looking at it, it looks like LINE INPUT just reads the file line to a string variable, so to get the values out of it I'm back to trying to parse a string. I guess I could read the line from the file, PUT that line to a temporary file, and then read that with INPUT? That doesn't seem much more efficient than what I'm already doing. But maybe I'm missing something.

FellippeHeitor

  • Guest
Re: XML in QB64?
« Reply #6 on: May 27, 2020, 01:31:47 pm »
Yeah, that would not be productive. Reading the line then parsing it would, although it is what you're trying to avoid.

Offline Nate5700

  • Newbie
  • Posts: 10
    • View Profile
Re: XML in QB64?
« Reply #7 on: May 27, 2020, 01:45:04 pm »
Yeah it's what I was hoping to avoid, anyway. Probably what I already have is good enough, as long as I remember that I have to enclose the tile numbers in quotes when reading from the CSV file so that it interprets it as a single string variable. Which is easy for me to do, but I'm wanting to make the file format somewhat intuitive for others as well, there may be someone who plays my game that wants to make their own custom levels.

Here's another question while we're on the topic of parsing strings: It's something I've never really done before in BASIC. So, I'm not sure if the way I've come up with to parse a string is the best way. Here's what I've got:

Code: QB64: [Select]
  1.             WHILE MID$(tiles$, strindex%, 1) <> "," AND MID$(tiles$, strindex%, 1) <> ""
  2.                 strread$ = strread$ + MID$(tiles$, strindex%, 1)
  3.                 strindex% = strindex% + 1
  4.             WEND
  5.             strindex% = strindex% + 1
  6.             tile% = VAL(strread$)

Where "tiles$" is the string from the CSV that lists the numbers of the image tiles you want to use, and "tile%" is the extracted value that you use to actually find the tile. This bit of code definitely works, I just have this feeling that there's a better way to do it.

EDIT: Modified the code as follows so that I'm not calling MID$ three times every loop:

Code: QB64: [Select]
  1.             nextchar$ = MID$(tiles$, strindex%, 1)
  2.             WHILE nextchar$ <> "," AND nextchar$ <> ""
  3.                 strread$ = strread$ + nextchar$
  4.                 strindex% = strindex% + 1
  5.                 nextchar$ = MID$(tiles$, strindex%, 1)
  6.             WEND
  7.             strindex% = strindex% + 1
  8.             tile% = VAL(strread$)
  9.  
« Last Edit: May 27, 2020, 05:02:35 pm by Nate5700 »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: XML in QB64?
« Reply #8 on: May 27, 2020, 09:18:59 pm »
Here is a wonderful parser whether comma delimited or space delimited (or both even) you can chop a string into an array in no time. You can load a file into a long string, chop it by end of line markers CHR$(13) + CHR$(10) or just CHR$(10)'s depending on data file and then chop each line delimited by commas or spaces into another array...

https://www.qb64.org/forum/index.php?topic=1607.0

If you have a sample data file and you can describe it's structure, I can show you how it works.

Oh, one caveat, the numbers have to be written as strings, ie you can edit them in a text editor.
« Last Edit: May 27, 2020, 09:25:56 pm by bplus »