GML Snippets

Handy GML Snippets for you to use

Pixel Perfect Movement

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
for (i=0; i<abs(move_xx); i++){
    if place_meeting(x+sign(move_xx),y,obj_wall_parent)=false{
        x+=sign(move_xx)
    }else{
        move_xx=0
    }
}

for (i=0; i<abs(move_yy); i++){
    if place_meeting(x,y+sign(move_yy),obj_wall_parent)=false{
        y+=sign(move_yy)
    }else{
        move_yy=0
    }
}

Save / Load system

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
//////////////////////////////////
//scr_save_example("saveFileName")
//
//by: WubsGames
//
//////////////////////////////////
if file_exists(argument0){
    file_delete(argument0)// we check if our file already exists, if it does we delete it.
}

var save_map=ds_map_create()// we are going to create a DS map to save our info into.

ds_map_add(save_map,"variableName",variableValue)//store a variable in our new DS map
//you can store as many variables in this map as you want.

var f=file_text_open_write(argument0)//open our file (this creates it if it does not exists)
file_text_write_string(f,ds_map_write(save_map)) // write the entire DS map as a string, into the file.
file_text_close(f)//close the file
ds_map_destroy(save_map)//destroy the DS map

//////////////////////////////////
//scr_load_example("loadFileName")
//
//by: WubsGames
//
//////////////////////////////////
if !file_exists(argument0){
    exit//if the file we want to load does not exist, we just exit.
    //you could also show the player a message here "oops! no save file found" or something
}

var load_map=ds_map_create()// create an empty map
var f=file_text_open_read(argument0) // open our file for reading
ds_map_read(load_map,file_text_read_string(f))//read the string from the file, into the DS map we just created
file_text_close(f)//close the file, we dont need it any more

variableValue=real(ds_map_find_value(load_map,"variableName"))//now we can pull variables from the DS map
//load as many vars out of the map as you want

ds_map_destroy(load_map)//destroy the DS map