• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Reading String in ForLoop using two values as an X & Y position

  • Thread starter TRANEPRODUCTIONS
  • Start date
T

TRANEPRODUCTIONS

Guest
Hey im trying to create a rougelike game but i cannot for the life of me figure out whats going wrong in the process I've ran into this issue more then once so i decided to post it here.

I used to times the J position by the width to get sorta kinda were it should be on the string but it's not exactly correct can someone help please.? P.s The prefab's are stored in strings using a 8x5 box size for now.

dX & dY are the drill integers used to select what position it is in on the map the rest of the coding is finished but not relevant to this specific error.

Code:
roomPrefab[0] = "000000000s0000s0000000000s0000s000000000"

//Setup Prefab
                for(j=0;j<roomPrefabHeight[roomType];j++){
                    for(o=0;o<roomPrefabWidth[roomType];o++){
                   
                        var xx, yy;
                        xx = dX + (o*16);
                        yy = dY + (j*16);
                        var str = string_char_at(roomPrefab[roomType], (o+j*8));
                        show_debug_message(str);
                       
                        if (str == "s"){
                            instance_create_depth(xx, yy, 0, ospawner);
                        }
                        //ADD Floor's
                        ds_grid_set_region(global.grid, xx, yy,xx+16, yy+16, 2);
                    }
                }
 

Sam04

Member
Hey im trying to create a rougelike game but i cannot for the life of me figure out whats going wrong in the process I've ran into this issue more then once so i decided to post it here.

I used to times the J position by the width to get sorta kinda were it should be on the string but it's not exactly correct can someone help please.? P.s The prefab's are stored in strings using a 8x5 box size for now.

dX & dY are the drill integers used to select what position it is in on the map the rest of the coding is finished but not relevant to this specific error.

Code:
roomPrefab[0] = "000000000s0000s0000000000s0000s000000000"

//Setup Prefab
                for(j=0;j<roomPrefabHeight[roomType];j++){
                    for(o=0;o<roomPrefabWidth[roomType];o++){
                 
                        var xx, yy;
                        xx = dX + (o*16);
                        yy = dY + (j*16);
                        var str = string_char_at(roomPrefab[roomType], (o+j*8));
                        show_debug_message(str);
                     
                        if (str == "s"){
                            instance_create_depth(xx, yy, 0, ospawner);
                        }
                        //ADD Floor's
                        ds_grid_set_region(global.grid, xx, yy,xx+16, yy+16, 2);
                    }
                }
A couple of observations.

First, you said the box size is 8x5. Make sure the "roomPrefabHeight[roomType]" variable really returns 8 and also make sure that the "roomPrefabWidth[roomType]" variable really returns 5.

Second, you have to keep in mind that the "string_char_at()" function starts counting characters from 1 (you can check more about that function here). Which means that the "o+j*8" is offset by one. You can solve this by changing it to "(o+j*8)+1". But in any case, it would be good to make your code work with other box sizes so you should replace it directly to "(o+j*roomPrefabWidth[roomType])+1".

Thirdly, this one might not be related to the problem at all but it is still something goog to note. The dX and dY variables, make sure they are set to the right place beforehand.

Hope, that helps.
 
Last edited:
Top