Jump to content


Photo

Help with loading a world


  • Please log in to reply
1 reply to this topic

#1 KOOPA

KOOPA

    GMC Member

  • GMC Member
  • 463 posts
  • Version:GM8

Posted 21 May 2012 - 05:47 AM

Hi im still trying to get my head around saving and loading worlds with ds_grids Ive managed to make it save to a text document
but I can not figure out to make it load then create the grid its loaded into the room

so far here is the code (along with the world generation scripts im using also)


World saving script
global.worldname = get_string("Name your world!",working_directory)
directory_create(global.worldname)
global.world_p1 = ds_grid_write(global.dsGridObjects)
global.world_p2 = ds_grid_write(global.dsGridObjects2)
global.world_p3 = ds_grid_write(global.dsGridObjects3)
file = file_text_open_write(global.worldname + "\worldsave.w")
file_text_write_string(file,global.world_p1)
file_text_close(file)
file2 = file_text_open_write(global.worldname + "\worldsave_p2.w")
file_text_write_string(file2,global.world_p2)
file_text_close(file2)
file3 = file_text_open_write(global.worldname + "\worldsave_p3.w")
file_text_write_string(file3,global.world_p3)
file_text_close(file3)



Stone layer generation script
w = floor(room_width/16);
h = floor(room_height/16);
global.dsGrid_objects = ds_grid_create(w,h);
ds_grid_set_region(global.dsGrid_objects,0,0,w,h,1);
for (grid_x=0;grid_x<=w;grid_x+=1)
{
    for (grid_y=21;grid_y<=h;grid_y+=1)
    {
    if (ds_grid_get(global.dsGrid_objects,grid_x,grid_y)==1)
    {
        global.grid_choice = choose(stone_block)
        instance_create(grid_x*16,grid_y*16, global.grid_choice);
        };
    };
};
instance_destroy()


Dirt and sand layer generation scripts
w = floor(room_width/16);
h = floor(336/16);
global.dsGrid_objects3 = ds_grid_create(w,h);
ds_grid_set_region(global.dsGrid_objects3,0,0,w,h,1);
for (grid_x=0;grid_x<=w;grid_x+=1)
{
    for (grid_y=13;grid_y<=h;grid_y+=1)
    {
    if (ds_grid_get(global.dsGrid_objects3,grid_x,grid_y)==1)
    {
        global.grid_choice = choose(dirt_block, sand_block)
        instance_create(grid_x*16,grid_y*16, global.grid_choice);
        };
    };
};
instance_destroy()



Grass layer generation script
w = floor(room_width/16);
h = floor(208/16);
global.dsGrid_objects2 = ds_grid_create(w,h);
ds_grid_set_region(global.dsGrid_objects2,0,0,w,h,1);
for (grid_x=0;grid_x<=w;grid_x+=1)
{
    for (grid_y=12;grid_y<=h;grid_y+=1)
    {
    if (ds_grid_get(global.dsGrid_objects2,grid_x,grid_y)==1)
    {
        global.grid_choice = choose(grass_block)
        instance_create(grid_x*16,grid_y*16, global.grid_choice);
        };
    };
};
instance_destroy()


Please help ive been trying for quite a while to do this please
  • 0

#2 C_Pike

C_Pike

    GMC Member

  • GMC Member
  • 565 posts
  • Version:GM8.1

Posted 21 May 2012 - 12:13 PM

First you generate your world:
0000000
0000000
0000000
0000000

It is empty...

-

Now you populate it
0 = air (nothing)
1 = stone
2 = dirt
3 = grass

1111111
1000001
1333331
1222221
1111111

-


You give the option to save the file (or just save the file, regardless):
1) Write the data structure to a string
2) get_save_filename(filter,fname) Asks for a filename to save with the given filter. If the user presses Cancel an empy string is returned.

To load an old level:
get_open_filename(filter,fname) Asks the player for a filename to open with the given filter. The filter has the form 'name1|mask1|name2|mask2|...'. A mask contains the different options with a semicolon between them. * means any string. For example: 'bitmaps|*.bmp;*.wmf'. If the user presses Cancel an empy string is returned.
-Write the string in the file to the main data structure using ds_grid_read

Now you run the code that is in charge of the object creation, and it populates the room.

Google search

Writing and reading data:

From an old FoxInABox post
Posted 24 August 2010 - 01:22 PM
the first part is not saving anything in a file .. just filling in 3 of the 6 places in the grid..

var f;
f=file_text_open_write("filename.sav")

file_text_write_string(f,ds_grid_write(test))
file_text_writeln(f) // go to next line (not realy nessesary here tho')

file_text_close(f)
restoring the ds_grid:
var f;
f=file_text_open_read("filename.sav")

test = ds_grid_read(file_text_read_string(f))
file_text_readln(f) // go to next line (not realy nessesary here tho')

file_text_close(f)


Edited by C_Pike, 21 May 2012 - 12:21 PM.

  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users