Hello Guest, please login or register.
Did you miss your activation email?
Login with username, password and session length.

Pages: [1]   Go Down

Author Topic: Loading maps with text files in GM7  (Read 1274 times)

0 Members and 1 Guest are viewing this topic.

noseblunt3

Woodman is awesome
Loading maps with text files in GM7
« on: July 03, 2009, 05:35:42 am »
  • *
  • Reputation: +2/-0
  • Offline Offline
  • Gender: Male
  • Posts: 132
I've been trying to make it work for the entire day,but I can't seem to find the problem.
Basically, I want to load all objects, backgrounds, etc. from a text file.It work pretty well... for the background and music.
It's probably a logic error, but I can't figure it out. The script replace all objects with the last one:

The thing is; the position of each object is correct, but it's the wrong one

I tried 2 version, both version fail the same way.
General part of the code:
Code: [Select]
// Load level through text files
// only one argument--> argument0 == name of the level/text file

var level, L, line, music, i, obj;   

if !file_exists(working_directory+"\levels\"+argument0+".txt") return 0
level = file_text_open_read(working_directory+"\levels\"+argument0+".txt");
       
// Store each line of the file in one array entry

L=0;
while !file_text_eof(level) {
                               line[L]=file_text_read_string(level); // copy the line into the array
                               file_text_readln(level); // skip to next line when done
                               L += 1;
                            }
file_text_close(level);

text file for version 1
Code: [Select]
1240                  line[0]          room width
480                   line[1]          room height
TestBackground.bmp    line[2]          background
secretinvasion.mp3    line[3]          song
3                     line[4]          number of object to create
320                   line[5]          x position of object1
360                   line[6]          y position of object1
obj_ground1           line[7]          name of object1
895                   line[8]          x position of object2
357                   line[9]          y position of object2
obj_ground2           line[10]         name of object3
400                   line[11]         x position of object3
380                   line[12]         y position of object3
obj_hero_R            line[13]         name of object3

Version 1
Code: [Select]
// object

if ( real(line[4]) > 0 ) // if need to create object
   {
      i = 5; // start at line 5
      while ( i < 5 + ( real(line[4]) * 3) ) // ( 5 + real(line[4]) * 3 ) == max number of lines + 1
         {
            instance_create(real(line[i]), real(line[i+1]), line[i+2]);
            i += 3;
         }   
   }

text file for version 2
Code: [Select]
1240                  line[0]          room width
480                   line[1]          room height
TestBackground.bmp    line[2]          background
secretinvasion.mp3    line[3]          song
3                     line[4]          number of object to create
320,360|obj_ground1   line[5]          object1
895,357|obj_ground2   line[6]          object2
400,380|obj_hero_R    line[7]          object3

Version 2
Code: [Select]
// object

if ( real(line[4]) > 0 ) // line[4] == number of object to create
   {
      i = 5;
      while ( i < 5 + real(line[4]) )
         {
          // find the position of the separators
          oba = string_pos("," , line[i]);
          obb = string_pos("|" , line[i]);
          // use that info to split the string and store the part into an array
          obj[0] = string_copy( line[i], 1, real(oba)-1); // copy the Xpos of the object
          obj[1] = string_copy( line[i], real(oba) + 1, real(obb) - (real(oba) + 1 )); // copy the Ypos of the object
          obj[2] = string_copy( line[i], real(obb) + 1, real(string_length(line[i])) - real(obb)); // copy the name of the object
          // create the instance
          instance_create(real(obj[0]), real(obj[1]), obj[2]);
          i += 1;
         }   
   }
And that's pretty much it!
Anyway, if someone could help me, I would be really grateful...
Logged
Rome wasn't built in a day, but they didn't waste time by sitting around doing nothing either!

noseblunt3

Woodman is awesome
Re: Loading maps with text files in GM7
« Reply #1 on: July 06, 2009, 06:37:40 pm »
  • *
  • Reputation: +2/-0
  • Offline Offline
  • Gender: Male
  • Posts: 132
Sorry for the double post, but I think I found my problem (but not the solution...).
It turns out that the argument obj of the function instance_create(x, y, obj) need to be an integer or a real.
So by using a string, it always result in 0, which mean that it create the first object I created (the player)

So, how could I work my way around this?  :huh:

Logged
Rome wasn't built in a day, but they didn't waste time by sitting around doing nothing either!
Re: Loading maps with text files in GM7
« Reply #2 on: July 06, 2009, 07:48:03 pm »
  • *
  • Reputation: +16/-0
  • Offline Offline
  • Gender: Male
  • Posts: 1633
I've been looking through the various functions of GML and I'm sorry to say I'm not convinced you can convert text to an object name. You can however use strings in combination with the switch command. Not a pretty sollution but it'll work. Or just use the index numbers for referencing. Sorry, that probably isn't a lot of help.
Logged
Re: Loading maps with text files in GM7
« Reply #3 on: July 06, 2009, 07:59:21 pm »
  • *
  • Reputation: +9/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3725
If you define constants for every object the game is capable of creating and give these unique integer numbers. These numbers can be 1, 2, 3, 4, 5, 6, 7, 8.... and so on, or some code like 1xx for playable characters (swap xx wit 01 or something) and 2xx for NPC's, 3xx for walkable surfaces and so on. If you use codes, you probably be able to replace the names in the text and don't need a conversion.

Well at least create constants. At the place where you are parsing the string look at the string name and replace it with the appropriate constant. I can't give you example code, but you should be able to figure it out.
Logged
Re: Loading maps with text files in GM7
« Reply #4 on: July 06, 2009, 09:03:59 pm »
  • *
  • Reputation: +9/-0
  • Offline Offline
  • Posts: 728
GM uses numbers as an id for the main objects you create in a project. I do have a tool you could use to see what the id of any object in your project is. This could be an option.

http://www.pyxosoft.com/downloads/gmlib_example.zip

This GMLib example uses the id property to showcase this value. As well as the object's name and other properties. You just need .net 2.0 to use it.
Logged
  • Pyxosoft
Pages: [1]   Go Up

 


Contact Us | Legal | Advertise Here
2013 © ZFGC, All Rights Reserved



Page created in 0.592 seconds with 45 queries.

anything