Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411621 Posts in 69391 Topics- by 58447 Members - Latest Member: sinsofsven

May 11, 2024, 03:21:42 AM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)Saving level data in a string... What am I doing wrong?
Pages: [1]
Print
Author Topic: Saving level data in a string... What am I doing wrong?  (Read 1338 times)
pgil
Guest
« on: January 09, 2010, 09:45:10 AM »

SO I'm making a level editor. My rooms are 640x480, and every object snaps to an 8x8 grid. Placing objects in the room works fine, but I want to make it so you can copy the entire map to the clipboard.

Here's my code:

Code:
if keyboard_check(vk_control){
   if keyboard_check_pressed(ord("C")){
      c=0 //the column to start at
      r=0 //the row to start at
      str_level = "Level"
      while(r<room_height){
         while(c<room_width){
            if position_meeting(c,r,ob_fence) sub="A"
            else sub = "0"
            string_insert(sub,str_level,string_length(str_level)) //add object to the end of the string
            c+=8  //move right one column
         }
         r+=8   //move down one row..
         c=0    //.. and reset column to 0
      }
      clipboard_set_text(str_level)
   }
}

It's supposed to create a string with the word "level" followed by letters or numbers representing each tile, starting at the top-left and working to the right each row until it gets to the bottom.

When I test it out, it only copies the initial string - the word "level". I've also tried saving a text file, and that does the same thing. So it looks like I'm not writing to the string correctly. I can't figure out what I'm doing wrong, though, so maybe somebody here could look at my code and see if there are any obvious mistakes  Beer!
Logged
Theotherguy
Level 1
*



View Profile
« Reply #1 on: January 09, 2010, 10:17:31 AM »

Why not use two for loops instead of two while loops?

I haven't ever used GML, but my guess is based on the behaviour that string_insert is supposed to return a brand new string, and not modify a string in place.

The second thing that could be happening is room_height and room_width are smaller than or equal to zero.

Try printing sub inside the nested while loop, and see what it says.
Logged

pgil
Guest
« Reply #2 on: January 09, 2010, 10:26:29 AM »

I haven't ever used GML, but my guess is based on the behaviour that string_insert is supposed to return a brand new string, and not modify a string in place.

Facepalm That's it! I knew it would be something obvious like that.

Thank you   Gentleman

So why for loops? Are they faster?
Logged
Theotherguy
Level 1
*



View Profile
« Reply #3 on: January 09, 2010, 11:57:24 AM »

No, I just find them easier to read. When compiled, a while loop and a for loop come out to exactly the same thing. A for loop is just a convenient short-hand for a specific kind of while loop.
Logged

JMickle
Level 10
*****



View Profile
« Reply #4 on: January 09, 2010, 12:10:39 PM »

I haven't ever used GML, but my guess is based on the behaviour that string_insert is supposed to return a brand new string, and not modify a string in place.

Facepalm That's it! I knew it would be something obvious like that.

i've made that mistake so many time you wouldn't believe.

Code:
number=random(5);
round(number);
WHY IS IT STILL A DECIMAL?? lol this got me for hours.
Logged

JMickle
Level 10
*****



View Profile
« Reply #5 on: January 09, 2010, 12:13:11 PM »

Oh, by the way, you can add strings together, which could make you code simpler to read:

input-
Code:
str1= 'Hello';
str2= 'frg';
draw_text(str1+str2);

output-
Quote
Hellofrg
Logged

Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic