Whats the worst code you have written

F

fxokz

Guest
When i look back at this page i actually cringe a little because of how badly its written.

Code:
///UPGRADES!!!

//turret 1
if player_money >= selected_turret.upgrade_cost && selected_turret = obj_turret1 && current_turret.times_upgraded = 0 //if not upgraded at all
{
    current_turret.fire_rate -= 5;
    current_turret.radius += 5;
    current_turret.damage += 5;
    current_turret.image_index += 1;
    current_turret.times_upgraded +=1;
    player_money -= selected_turret.upgrade_cost;
} else if player_money >= selected_turret.upgrade_cost && selected_turret = obj_turret1 && current_turret.times_upgraded = 1 //if upgraded once
{
    current_turret.fire_rate -= 10;
    current_turret.radius +=5;
    current_turret.damage +=10;
    current_turret.image_index += 1;
    current_turret.times_upgraded +=1;
    player_money -= selected_turret.upgrade_cost;
}

//turret 2
if player_money >= selected_turret.upgrade_cost && selected_turret = obj_turret2 && current_turret.times_upgraded = 0 //if not upgraded at all
{
    current_turret.fire_rate -= 2;
    current_turret.radius += 5;
    current_turret.damage += 3;
    current_turret.image_index += 1;
    current_turret.times_upgraded +=1;
    player_money -= selected_turret.upgrade_cost;
} else if player_money >= selected_turret.upgrade_cost && selected_turret = obj_turret2 && current_turret.times_upgraded = 1 //if upgraded once
{
    current_turret.fire_rate -= 3;
    current_turret.radius +=7;
    current_turret.damage +=6;
    current_turret.image_index += 1;
    current_turret.times_upgraded +=1;
    player_money -= selected_turret.upgrade_cost;
}

//turret 3
if player_money >= selected_turret.upgrade_cost && selected_turret = obj_turret3 && current_turret.times_upgraded = 0 //if not upgraded at all
{
  //current_turret.fire_rate -= .1; TOO OP
    current_turret.radius += 5;
    current_turret.damage += 2;
    current_turret.image_index += 1;
    current_turret.times_upgraded +=1;
    player_money -= selected_turret.upgrade_cost;
} else if player_money >= selected_turret.upgrade_cost && selected_turret = obj_turret3 && current_turret.times_upgraded = 1 //if upgraded once
{
    current_turret.fire_rate -= .1;
    current_turret.radius +=10;
    current_turret.damage +=3;
    current_turret.image_index += 1;
    current_turret.times_upgraded +=1;
    player_money -= selected_turret.upgrade_cost;
}

//turret 4
if player_money >= selected_turret.upgrade_cost && selected_turret = obj_turret4 && current_turret.times_upgraded = 0 //if not upgraded at all
{
    current_turret.fire_rate -= 5;
    current_turret.radius += 2;
    current_turret.damage += 10;
    current_turret.image_index += 1;
    current_turret.times_upgraded +=1;
    player_money -= selected_turret.upgrade_cost;
} else if player_money >= selected_turret.upgrade_cost && selected_turret = obj_turret4 && current_turret.times_upgraded = 1 //if upgraded once
{
    current_turret.fire_rate -= 5;
    current_turret.radius += 3;
    current_turret.damage += 10;
    current_turret.image_index += 1;
    current_turret.times_upgraded +=1;
    player_money -= selected_turret.upgrade_cost;
}

//turret 5
if player_money >= selected_turret.upgrade_cost && selected_turret = obj_turret5 && current_turret.times_upgraded = 0 //if not upgraded at all
{
  //current_turret.fire_rate -= 3;
    current_turret.radius += 3;
    current_turret.damage += 5;
    current_turret.image_index += 1;
    current_turret.times_upgraded +=1;
    player_money -= selected_turret.upgrade_cost;
} else if player_money >= selected_turret.upgrade_cost && selected_turret = obj_turret5 && current_turret.times_upgraded = 1 //if upgraded once
{
    current_turret.fire_rate -= 5;
    current_turret.radius +=4;
    current_turret.damage +=5;
    current_turret.image_index += 1;
    current_turret.times_upgraded +=1;
    player_money -= selected_turret.upgrade_cost;
}

I hope i never go through the pain of that ever again. What about you guys? real curious.
 
S

StuffandThings85

Guest
When I first started with GM, I had no idea how parenting worked. I thought for every object you made (like a ground or wall object), you needed a separate collision code for each one. So I basically had this:
Code:
 //obj_ground
    if (place_meeting(x+hsp_final,y,obj_ground))
{
    while(!place_meeting(x+sign(hsp_final),y,obj_ground))
    {
        x += sign(hsp_final);
    }
        hsp_final=0;
        hsp = 0;
}
over and over for each new ground object I made. So...yeah...
 

RujiK

Member
When I first began modding, I wanted to do use a simple command called "Lock" to lock a car's doors. Correct Usage: MyCar lock true;

But I used "Lock MyCar" because that made more sense grammatically and got this error:
Error and: Type Null, expected bool

This was my journey to success:
Code:
MyCar Lock bool
MyCar Lock expected bool
MyCar Lock TorF (True or false)
Lock MyCar bool
bool Lock MyCar
expected bool Lock MyCar
Lock bool MyCar
etc.
When I finally got it working I felt so smart that I had programmed an entire single command. Ahh, good memories.
 

RujiK

Member
@KingdomOfGamez
That actually works in GMS2, you just need to disable read only varaibles.


Here is an old game I made with some rubbish code that runs really slow:


Now I just put this in the begin-step event every frame: fps += 9999;

And viola!

(I've had this gif's sitting around for over a month)
 

Changgi

Member
10 food_produce()
20 goto (10)

Code:
if(fps < 60) then {
     fps = 60;
}
Thought I would change the future ;-;
I wanted to freeze the room once without using Sleep because it caused problems with other parts of the game so I wanted to make the fps = 0 but GM told me that that wasn't possible. Then I thought it was probably the case because if the fps=0 then it wouldn't actually perform the next step, so the program would be stuck there forever.
 

Hyomoto

Member
The worst code I've written will be the code I write today. All code you write will look idiotic one day, because when you look back on it with all the experience you've gained, you'll wonder why you ever thought it was a good idea to do it that way in the first place. Here's some code I was proud of in the past:
Code:
if transitionSpeed > 0 {
    transition += 1;
  
    transitionProgress = bias( 0.3, transition / transitionSpeed );

    x = lerp( transStartX, transEndX, transitionProgress );
    y = lerp( transStartY, transEndY, transitionProgress );
    width  = lerp( transStartWidth, transEndWidth, transitionProgress );
    height = lerp( transStartHeight, transEndHeight, transitionProgress );
  
    if transition == transitionSpeed { transitionSpeed = 0; transition = 0 }
  
}
Man, I thought it was so smart! Here's what that code looks like today:
Code:
if transition != undefined {
    var _data, _data, _transition;
  
    if is_array( transition ) {
        _transition = transitions[ transition[ 0 ] ];
      
        if array_length_1d( transition ) == 3 { transitionSpeed = [ 0, transition[ 2 ] ] }
        else { transitionSpeed = [ 0, _transition[ 0 ] ] }
      
        transitionWait    = transition[ 1 ];
      
    } else {
        _transition = transitions[ transition ];
        transitionSpeed    = [ 0, _transition[ 0 ] ];
        transitionWait    = 0;
      
    }
    transitionData = [ ];
  
    for ( var _i = 1; _i < array_length_1d( _transition ); _i++ ) {
        var _data = _transition[ _i ], _I = _i - 1;
        switch( _data[ 0 ] ) {
            case transition_x :
                transitionData[ _I, 1 ] = ( _data[ 1 ] == undefined ) ? x : ( _data[ 1 ] );
                transitionData[ _I, 2 ] = ( _data[ 2 ] == undefined ) ? x : ( _data[ 2 ] );
                break;
          
            case transition_y :
                transitionData[ _I, 1 ] = ( _data[ 1 ] == undefined ) ? y : ( _data[ 1 ] );
                transitionData[ _I, 2 ] = ( _data[ 2 ] == undefined ) ? y : ( _data[ 2 ] );
                break;
              
            case transition_width :
                transitionData[ _I, 1 ] = ( _data[ 1 ] == undefined ) ? ( width ) : ( _data[ 1 ] );
                transitionData[ _I, 2 ] = ( _data[ 2 ] == undefined ) ? ( width ) : ( _data[ 2 ] );
                break;
              
            case transition_height :
                transitionData[ _I, 1 ] = ( _data[ 1 ] == undefined ) ? ( height ) : ( _data[ 1 ] );
                transitionData[ _I, 2 ] = ( _data[ 2 ] == undefined ) ? ( height ) : ( _data[ 2 ] );
                break;
              
            case transition_rotate :
                transitionData[ _I, 1 ] = ( _data[ 1 ] == undefined ) ? rotate : _data[ 1 ];
                transitionData[ _I, 2 ] = ( _data[ 2 ] == undefined ) ? rotate : _data[ 2 ];
                break;
              
            case transition_alpha :
                transitionData[ _I, 1 ] = ( _data[ 1 ] == undefined ) ? alpha : _data[ 1 ];
                transitionData[ _I, 2 ] = ( _data[ 2 ] == undefined ) ? alpha : _data[ 2 ];
                break;
              
            case transition_scale :
                transitionData[ _I, 1 ] = ( _data[ 1 ] == undefined ) ? scale : _data[ 1 ];
                transitionData[ _I, 2 ] = ( _data[ 2 ] == undefined ) ? scale : _data[ 2 ];
                break;
      
        }
        transitionData[ _I, 0 ] = _data[ 0 ];
      
    }
    transition = undefined;
  
}
if transitionSpeed != undefined && transitionWait == 0 {
    var _progress = ++transitionSpeed[ 0 ] / transitionSpeed[ 1 ];
  
    for ( var _i = 0; _i < array_height_2d( transitionData ); _i++ ) {
        switch( transitionData[ _i, 0 ] ) {
            case transition_x :
                x = lerp( transitionData[ _i, 1 ], transitionData[ _i, 2 ], _progress );
                break;
          
            case transition_y :
                y = lerp( transitionData[ _i, 1 ], transitionData[ _i, 2 ], _progress );
                break;
              
            case transition_width :
                width = lerp( transitionData[ _i, 1 ], transitionData[ _i, 2 ], _progress );
                break;
              
            case transition_height :
                height = lerp( transitionData[ _i, 1 ], transitionData[ _i, 2 ], _progress );
                break;
              
            case transition_rotate :
                rotate = lerp( transitionData[ _i, 1 ], transitionData[ _i, 2 ], _progress );
                break;
              
            case transition_alpha :
                alpha = lerp( transitionData[ _i, 1 ], transitionData[ _i, 2 ], _progress );
                break;
              
            case transition_scale :
                scale = lerp( transitionData[ _i, 1 ], transitionData[ _i, 2 ], _progress );
                break;
              
        }
      
    }
    transitionProgress    = _progress;
  
    if _progress = 1 { transitionSpeed = undefined }
  
} else { transitionWait -= 1 }
One day I'll look back at this and punch myself. Seemed like a good idea at the time, and it worked: next task!
 
W

Wraithious

Guest
you mean code that worked? haha this was my "global cave system" i made first learning to code in gamemaker 3.3
//straight across//
{if global.cave=0 || global.cave=4 || global.cave=20 || global.cave=40
|| global.cave=67 || global.cave=115 || global.cave=81 || global.cave=86
|| global.cave=1 || global.cave=5 || global.cave=10 || global.cave=11
|| global.cave=14 || global.cave=15 || global.cave=34 || global.cave=37
|| global.cave=70 || global.cave=48 || global.cave=7 || global.cave=36
|| global.cave=9 || global.cave=61 || global.cave=63 || global.cave=74
|| global.cave=29 || global.cave=60 || global.cave=92 || global.cave=100
|| global.cave=9 || global.cave=13 || global.cave=75 || global.cave=95
|| global.cave=26 || global.cave=60 || global.cave=82 || global.cave= 91
|| global.cave=67 || global.cave=91 || global.cave=69 || global.cave=88
|| global.cave=103 || global.cave=118 || global.cave=123 || global.cave=146
|| global.cave=119 || global.cave=121 || global.cave=122 || global.cave=124
|| global.cave=127 || global.cave=128 || global.cave=130 || global.cave=131
|| global.cave=133 || global.cave=135 || global.cave=140 || global.cave=144
|| global.cave=148 || global.cave=154 || global.cave=157 || global.cave=158
|| global.cave=160 || global.cave=161 || global.cave=164 || global.cave=165
|| global.cave=172 || global.cave=178 || global.cave=181 || global.cave=182
|| global.cave=33 || global.cave=97 || global.cave=120 || global.cave=189
|| global.cave=193 || global.cave=194 || global.cave=196 || global.cave=197
|| global.cave=198 || global.cave=200 image_index=1;
//up and down//
if global.cave=79 || global.cave=84 || global.cave=72 || global.cave=102
|| global.cave=2 || global.cave=101 || global.cave=93 || global.cave=89
|| global.cave=12 || global.cave=16 || global.cave=65 || global.cave=87
|| global.cave=106 || global.cave=3 || global.cave=17 || global.cave=99
|| global.cave=39 || global.cave=66 || global.cave=21 || global.cave=30
|| global.cave=58 || global.cave=62 || global.cave=78 || global.cave=105
|| global.cave=73 || global.cave=76 || global.cave=90 || global.cave=6
|| global.cave=8 || global.cave=2 || global.cave=22 || global.cave=23
|| global.cave=24 || global.cave=32 || global.cave=43 || global.cave=44
|| global.cave=45 || global.cave=46 || global.cave=47 || global.cave=50
|| global.cave=52 || global.cave=54 || global.cave=56 || global.cave=76
|| global.cave=83 || global.cave=104 || global.cave=35 || global.cave=38
|| global.cave=107 || global.cave=108 || global.cave=109 || global.cave=111
|| global.cave=112 || global.cave=113 || global.cave=114 || global.cave=94
|| global.cave=125 || global.cave=129 || global.cave=132 || global.cave=176
|| global.cave=134 || global.cave=139 || global.cave=141 || global.cave=163
|| global.cave=162 || global.cave=159 || global.cave=156 || global.cave=170
|| global.cave=185 || global.cave=179 || global.cave=180 || global.cave=183
|| global.cave=188 || global.cave=190 || global.cave=191 || global.cave=192
|| global.cave=195 || global.cave=199 || global.cave=201 image_index=0;
//specials//
//cave53 hidden room special//
if self.y <310 && global.cave=110 && room=53 image_index=0;
if self.y >250 && global.cave=110 && room=53 image_index=1;
//T63 view1//
if global.cave=42 || global.cave=96 || global.cave=49 || global.cave=145 && self.y <310
&& room=63 image_index=0;
if global.cave=42 || global.cave=96 || global.cave=49 || global.cave=145 && self.y >310
&& room=63 image_index=1;
//view2//
if global.cave=166 || global.cave=168 || global.cave=171 || global.cave=174 && self.x <30
&& room=63 image_index=0;
if global.cave=166 || global.cave=168 || global.cave=171 || global.cave=174 && self.x >30
&& room=63 image_index=1;
//special//
if global.cave=171 && self.x>200 image_index=0;
//T64 view2//
if global.cave=25 || global.cave=59 || global.cave=142 || global.cave=150 && self.y < 210
image_index=0;
if global.cave=25 || global.cave=59 || global.cave=142 || global.cave=150 && self.y > 210
image_index=1;
//T65 view1//
if global.cave=18 || global.cave=19 && self.y < 10 image_index=1;
if global.cave=18 || global.cave=19 && self.y > 10 image_index=0;
//T65 view2//
if global.cave=31 || global.cave=51 || global.cave=53 || global.cave=55
|| global.cave=80 && self.y < 10 && room=65 image_index=1;
if global.cave=31 || global.cave=51 || global.cave=53 || global.cave=55
|| global.cave=80 && self.y > 10 && room=65 image_index=0;
//T66 view1//
if global.cave=149 || global.cave=152 || global.cave=68 && self.y < 310 && room=66 image_index=0;
if global.cave=149 || global.cave=152 || global.cave=68 && self.y > 310 && room=66 image_index=1;
//view2//
if global.cave=71 && self.x < 310 && room=66 image_index=1;
if global.cave=71 && self.x > 310 && room=66 image_index=0;


}
}
 
Wow. I literally just did this, like 5 minutes ago.

Code:
if (selected == false)
 {
 draw_text(x+12, y+8, string(value));
 }
else if (selected == true)
 {
 draw_text(x+12, y-8, string(value));
 }
 
T

tserek

Guest
if a !=a
{
game_end();
}

that game would run long time? less serious..
a = a+1; ... a +=1;
a = a+a; ... a +=a;
 
Last edited by a moderator:
G

grixm

Guest
I have some really long and ugly expressions some places:

Code:
if ((mouse_x > 550) or (mouse_y > 550)) and (placing_status != 2) or (!ds_list_empty(semaster_list) and (
((mouse_x == clamp(mouse_x,rectxmin-2,rectxmax+2)) and (mouse_y == clamp(mouse_y,rectymin-2,rectymax+2)))
or ((mouse_x == clamp(mouse_x,anchorx/$ffff*512-10,anchorx/$ffff*512+10)) and (mouse_y == clamp(mouse_y,anchory/$ffff*512-10,anchory/$ffff*512+10)))
or ((mouse_x == clamp(mouse_x,rectxmin-20,rectxmin-2)) and (mouse_y == clamp(mouse_y,rectymax+2,rectymax+20)))
or ((mouse_x == clamp(mouse_x,rectxmax+2,rectxmax+20)) and (mouse_y == clamp(mouse_y,rectymax+2,rectymax+20)))
))
    {
    //do stuff
    }
Or this one before I learned of accessors which would have at least halved its size:
Code:
bezier_coeffs(ds_list_find_value(bez_list,0),ds_list_find_value(bez_list,1),ds_list_find_value(bez_list,2),ds_list_find_value(bez_list,3),ds_list_find_value(bez_list,4),ds_list_find_value(bez_list,5),ds_list_find_value(bez_list,6),ds_list_find_value(bez_list,7));
 
Code:
global.fnt_font_main = font_add_sprite(spr_font_main, ord(" "), true, 1);
In the step event. It leaked memory, but not fast enough for me to catch it. It would take up like 1GB of RAM after an hour or so.
 

Lumenflower

Yellow Dog
When I first downloaded GM I didn't faff around with the manual or anything, I just dived straight in and tried to make a remake of Bowmaster (which was the big thing among my friends at the time). I didn't understand the concept of variables, so instead I would create an object called 'canshoot' or something, and would create it in the corner of the room. When you fired an arrow, the canshoot object would be destroyed, and then created again thirty seconds thereafter. Problem was, you could see the little red square floating in the corner of the screen. I got around this by immediately setting the object's horizontal speed to 1000, so it would fly off the screen before you could see it!
 
D

Dark

Guest
Variable hell.
Code:
///sprite_draw_9patch(9patch list,sprite,subimg,x,y,w,h,color,alpha)
/* Draws a sprite with 9patch, sugin a data returned from sprite_get_9patch.
 * Written by darkwalker247
 */
var sp,sub,col,alp,ow,oh,nl,bx,by,bw,bh,sx,sy,sw,sh,cx,cy,cw,ch,sxs,sys,x1,y1,x2,y2,x3,y3,x4,y4,x5,y5,x6,y6,x7,y7,x8,y8,x9,y9;


sp=argument1
sub=argument2
col=argument7
alp=argument8
ow=sprite_get_width(sp)-2
oh=sprite_get_height(sp)-2
nl=argument0
bx=argument3-(sprite_get_xoffset(argument1))*(argument5/(ow))
by=argument4-(sprite_get_yoffset(argument1))*(argument6/(ow))
bw=argument5
bh=argument6
sx=ds_list_find_value(nl,0)+1
sy=ds_list_find_value(nl,1)+1
sw=ds_list_find_value(nl,2)
sh=ds_list_find_value(nl,3)
cx=ds_list_find_value(nl,4)
cy=ds_list_find_value(nl,5)
cw=ds_list_find_value(nl,6)
ch=ds_list_find_value(nl,7)
sxw=bw-(sx+(ow-(sx+sw)))
syh=bh-(sy+(oh-(sy+sh)))
sxs=sxw/sw
sys=syh/sh
x1=bx
y1=by
x2=bx+sx
y2=by
x3=x2+sxw
y3=by
x4=x3
y4=by+sy
x5=x3
y5=y4+syh
x6=x2
y6=y5
x7=bx
y7=y5
x8=bx
y8=y4
x9=bx+sx
y9=by+sy

draw_sprite_part_ext(sp,sub,1,1,sx,sy,x1,y1,1,1,col,alp) //1
draw_sprite_part_ext(sp,sub,sx,1,sw,sy,x2,y2,sxs,1,col,alp) //2
draw_sprite_part_ext(sp,sub,sx+sw,1,(ow+1)-(sx+sw),sy,x3,y3,1,1,col,alp) //3
draw_sprite_part_ext(sp,sub,sx+sw,sy,(ow+1)-(sx+sw),sh,x4,y4,1,sys,col,alp) //4
draw_sprite_part_ext(sp,sub,sx+sw,sy+sh,(ow+1)-(sx+sw),(oh+1)-(sy+sh),x5,y5,1,1,col,alp) //5
draw_sprite_part_ext(sp,sub,sx,sy+sh,sw,(oh+1)-(sy+sh),x6,y6,sxs,1,col,alp) //6
draw_sprite_part_ext(sp,sub,1,sy+sh,sx,(oh+1)-(sy+sh),x7,y7,1,1,col,alp) //7
draw_sprite_part_ext(sp,sub,1,sy,sx,sh,x8,y8,1,sys,col,alp) //8
draw_sprite_part_ext(sp,sub,sx,sy,sw,sh,x9,y9,sxs,sys,col,alp) //9
 
I didn't understand switch statements, so I did something stupid and dumb...

Code:
///Colouring Alpha

//Blacking

switch(global.hair)or(global.head)or(global.eyes)or(global.arms)or(global.body)or(global.legs)or(global.feet)or(global.skin)
{

    //1's

    case(global.blacking = 0):
    draw_set_alpha(0.0);
    draw_set_colour(c_black);
    break;
    case (global.blacking = 1):
    draw_set_alpha(0.1);
    draw_set_colour(c_black);
    break;
    case (global.blacking = 2):
    draw_set_alpha(0.2);
    draw_set_colour(c_black);
    break;
    case (global.blacking = 3):
    draw_set_alpha(0.3);
    draw_set_colour(c_black);
    break;
    case (global.blacking = 4):
    draw_set_alpha(0.4);
    draw_set_colour(c_black);
    break;
    case (global.blacking = 5):
    draw_set_alpha(0.5);
    draw_set_colour(c_black);
    break;
    case (global.blacking = 6):
    draw_set_alpha(0.6);
    draw_set_colour(c_black);
    break;
    case (global.blacking = 7):
    draw_set_alpha(0.7);
    draw_set_colour(c_black);
    break;
    case (global.blacking = 8):
    draw_set_alpha(0.8);
    draw_set_colour(c_black);
    break;
    case (global.blacking = 9):
    draw_set_alpha(0.9);
    draw_set_colour(c_black);
    break;
    case (global.blacking = 10):
    draw_set_alpha(0.10);
    draw_set_colour(c_black);
    break;
  
    //10's

    case (global.blacking = 11):
    draw_set_alpha(0.11);
    draw_set_colour(c_black);
    break;
    case (global.blacking = 12):
    draw_set_alpha(0.12);
    draw_set_colour(c_black);
    break;
    case (global.blacking = 13):
    draw_set_alpha(0.13);
    draw_set_colour(c_black);
    break;
    case (global.blacking = 14):
    draw_set_alpha(0.14);
    draw_set_colour(c_black);
    break;
    case (global.blacking = 15):
    draw_set_alpha(0.15);
    draw_set_colour(c_black);
    break;
    case (global.blacking = 16):
    draw_set_alpha(0.16);
    draw_set_colour(c_black);
    break;
    case (global.blacking = 17):
    draw_set_alpha(0.17);
    draw_set_colour(c_black);
    break;
    case (global.blacking = 18):
    draw_set_alpha(0.18);
    draw_set_colour(c_black);
    break;
    case (global.blacking = 19):
    draw_set_alpha(0.19);
    draw_set_colour(c_black);
    break;
    case (global.blacking = 20):
    draw_set_alpha(0.20);
    draw_set_colour(c_black);
    break;
  
    //20's
  
    case (global.blacking = 21):
    draw_set_alpha(0.21);
    draw_set_colour(c_black);
    break;
    case (global.blacking = 22):
    draw_set_alpha(0.22);
    draw_set_colour(c_black);
    break;
    case (global.blacking = 23):
    draw_set_alpha(0.23);
    draw_set_colour(c_black);
    break;
    case (global.blacking = 24):
    draw_set_alpha(0.24);
    draw_set_colour(c_black);
    break;
    case (global.blacking = 25):
    draw_set_alpha(0.25);
    draw_set_colour(c_black);
    break;
    case (global.blacking = 26):
    draw_set_alpha(0.26);
    draw_set_colour(c_black);
    break;
    case (global.blacking = 27):
    draw_set_alpha(0.27);
    draw_set_colour(c_black);
    break;
    case (global.blacking = 28):
    draw_set_alpha(0.28);
    draw_set_colour(c_black);
    break;
    case (global.blacking = 29):
    draw_set_alpha(0.29);
    draw_set_colour(c_black);
    break;
    case (global.blacking = 30):
    draw_set_alpha(0.30);
    draw_set_colour(c_black);
    break;
  
    //30's
  
    case (global.blacking = 31):
    draw_set_alpha(0.31);
    draw_set_colour(c_black);
    break;
    case (global.blacking = 32):
    draw_set_alpha(0.32);
    draw_set_colour(c_black);
    break;
    case (global.blacking = 33):
    draw_set_alpha(0.33);
    draw_set_colour(c_black);
    break;
    case (global.blacking = 34):
    draw_set_alpha(0.34);
    draw_set_colour(c_black);
    break;
    case (global.blacking = 35):
    draw_set_alpha(0.35);
    draw_set_colour(c_black);
    break;
    case (global.blacking = 36):
    draw_set_alpha(0.36);
    draw_set_colour(c_black);
    break;
    case (global.blacking = 37):
    draw_set_alpha(0.37);
    draw_set_colour(c_black);
    break;
    case (global.blacking = 38):
    draw_set_alpha(0.38);
    draw_set_colour(c_black);
    break;
    case (global.blacking = 39):
    draw_set_alpha(0.39);
    draw_set_colour(c_black);
    break;
    case (global.blacking = 40):
    draw_set_alpha(0.40);
    draw_set_colour(c_black);
    break;
  
    //40's
  
    case (global.blacking = 41):
    draw_set_alpha(0.41);
    draw_set_colour(c_black);
    break;
    case (global.blacking = 42):
    draw_set_alpha(0.42);
    draw_set_colour(c_black);
    break;
    case (global.blacking = 43):
    draw_set_alpha(0.43);
    draw_set_colour(c_black);
    break;
    case (global.blacking = 44):
    draw_set_alpha(0.44);
    draw_set_colour(c_black);
    break;
    case (global.blacking = 45):
    draw_set_alpha(0.45);
    draw_set_colour(c_black);
    break;
    case (global.blacking = 46):
    draw_set_alpha(0.46);
    draw_set_colour(c_black);
    break;
    case (global.blacking = 47):
    draw_set_alpha(0.47);
    draw_set_colour(c_black);
    break;
    case (global.blacking = 48):
    draw_set_alpha(0.48);
    draw_set_colour(c_black);
    break;
    case (global.blacking = 49):
    draw_set_alpha(0.49);
    draw_set_colour(c_black);
    break;
    case (global.blacking = 50):
    draw_set_alpha(0.50);
    draw_set_colour(c_black);
    break;
}

//Whiteing

switch(global.hair)or(global.head)or(global.eyes)or(global.arms)or(global.body)or(global.legs)or(global.feet)or(global.skin)
{

    //1's

    case (global.whiteing = 0):
    draw_set_alpha(0.0);
    draw_set_colour(c_white);
    break;
    case (global.whiteing = 1):
    draw_set_alpha(0.1);
    draw_set_colour(c_white);
    break;
    case (global.whiteing = 2):
    draw_set_alpha(0.2);
    draw_set_colour(c_white);
    break;
    case (global.whiteing = 3):
    draw_set_alpha(0.3);
    draw_set_colour(c_white);
    break;
    case (global.whiteing = 4):
    draw_set_alpha(0.4);
    draw_set_colour(c_white);
    break;
    case (global.whiteing = 5):
    draw_set_alpha(0.5);
    draw_set_colour(c_white);
    break;
    case (global.whiteing = 6):
    draw_set_alpha(0.6);
    draw_set_colour(c_white);
    break;
    case (global.whiteing = 7):
    draw_set_alpha(0.7);
    draw_set_colour(c_white);
    break;
    case (global.whiteing = 8):
    draw_set_alpha(0.8);
    draw_set_colour(c_white);
    break;
    case (global.whiteing = 9):
    draw_set_alpha(0.9);
    draw_set_colour(c_white);
    break;
    case (global.whiteing = 10):
    draw_set_alpha(0.10);
    draw_set_colour(c_white);
    break;
  
    //10's

    case (global.whiteing = 11):
    draw_set_alpha(0.11);
    draw_set_colour(c_white);
    break;
    case (global.whiteing = 12):
    draw_set_alpha(0.12);
    draw_set_colour(c_white);
    break;
    case (global.whiteing = 13):
    draw_set_alpha(0.13);
    draw_set_colour(c_white);
    break;
    case (global.whiteing = 14):
    draw_set_alpha(0.14);
    draw_set_colour(c_white);
    break;
    case (global.whiteing = 15):
    draw_set_alpha(0.15);
    draw_set_colour(c_white);
    break;
    case (global.whiteing = 16):
    draw_set_alpha(0.16);
    draw_set_colour(c_white);
    break;
    case (global.whiteing = 17):
    draw_set_alpha(0.17);
    draw_set_colour(c_white);
    break;
    case (global.whiteing = 18):
    draw_set_alpha(0.18);
    draw_set_colour(c_white);
    break;
    case (global.whiteing = 19):
    draw_set_alpha(0.19);
    draw_set_colour(c_white);
    break;
    case (global.whiteing = 20):
    draw_set_alpha(0.20);
    draw_set_colour(c_white);
    break;
  
    //20's
  
    case (global.whiteing = 21):
    draw_set_alpha(0.21);
    draw_set_colour(c_white);
    break;
    case (global.whiteing = 22):
    draw_set_alpha(0.22);
    draw_set_colour(c_white);
    break;
    case (global.whiteing = 23):
    draw_set_alpha(0.23);
    draw_set_colour(c_white);
    break;
    case (global.whiteing = 24):
    draw_set_alpha(0.24);
    draw_set_colour(c_white);
    break;
    case (global.whiteing = 25):
    draw_set_alpha(0.25);
    draw_set_colour(c_white);
    break;
    case (global.whiteing = 26):
    draw_set_alpha(0.26);
    draw_set_colour(c_white);
    break;
    case (global.whiteing = 27):
    draw_set_alpha(0.27);
    draw_set_colour(c_white);
    break;
    case (global.whiteing = 28):
    draw_set_alpha(0.28);
    draw_set_colour(c_white);
    break;
    case (global.whiteing = 29):
    draw_set_alpha(0.29);
    draw_set_colour(c_white);
    break;
    case (global.whiteing = 30):
    draw_set_alpha(0.30);
    draw_set_colour(c_white);
    break;
  
    //30's
  
    case (global.whiteing = 31):
    draw_set_alpha(0.31);
    draw_set_colour(c_white);
    break;
    case (global.whiteing = 32):
    draw_set_alpha(0.32);
    draw_set_colour(c_white);
    break;
    case (global.whiteing = 33):
    draw_set_alpha(0.33);
    draw_set_colour(c_white);
    break;
    case (global.whiteing = 34):
    draw_set_alpha(0.34);
    draw_set_colour(c_white);
    break;
    case (global.whiteing = 35):
    draw_set_alpha(0.35);
    draw_set_colour(c_white);
    break;
    case (global.whiteing = 36):
    draw_set_alpha(0.36);
    draw_set_colour(c_white);
    break;
    case (global.whiteing = 37):
    draw_set_alpha(0.37);
    draw_set_colour(c_white);
    break;
    case (global.whiteing = 38):
    draw_set_alpha(0.38);
    draw_set_colour(c_white);
    break;
    case (global.whiteing = 39):
    draw_set_alpha(0.39);
    draw_set_colour(c_white);
    break;
    case (global.whiteing = 40):
    draw_set_alpha(0.40);
    draw_set_colour(c_white);
    break;
  
    //40's
  
    case (global.whiteing = 41):
    draw_set_alpha(0.41);
    draw_set_colour(c_white);
    break;
    case (global.whiteing = 42):
    draw_set_alpha(0.42);
    draw_set_colour(c_white);
    break;
    case (global.whiteing = 43):
    draw_set_alpha(0.43);
    draw_set_colour(c_white);
    break;
    case (global.whiteing = 44):
    draw_set_alpha(0.44);
    draw_set_colour(c_white);
    break;
    case (global.whiteing = 45):
    draw_set_alpha(0.45);
    draw_set_colour(c_white);
    break;
    case (global.whiteing = 46):
    draw_set_alpha(0.46);
    draw_set_colour(c_white);
    break;
    case (global.whiteing = 47):
    draw_set_alpha(0.47);
    draw_set_colour(c_white);
    break;
    case (global.whiteing = 48):
    draw_set_alpha(0.48);
    draw_set_colour(c_white);
    break;
    case (global.whiteing = 49):
    draw_set_alpha(0.49);
    draw_set_colour(c_white);
    break;
    case (global.whiteing = 50):
    draw_set_alpha(0.50);
    draw_set_colour(c_white);
    break;
}
You just made me cry, I'm not even kidding. This is sickening.
 
I didn't understand switch statements, so I did something stupid and dumb...

Code:
///Colouring Alpha

//Blacking

switch(global.hair)or(global.head)or(global.eyes)or(global.arms)or(global.body)or(global.legs)or(global.feet)or(global.skin)
{

    //1's

    case(global.blacking = 0):
    draw_set_alpha(0.0);
    draw_set_colour(c_black);
    break;
    case (global.blacking = 1):
    draw_set_alpha(0.1);
    draw_set_colour(c_black);
    break;
    case (global.blacking = 2):
    draw_set_alpha(0.2);
    draw_set_colour(c_black);
    break;
    case (global.blacking = 3):
    draw_set_alpha(0.3);
    draw_set_colour(c_black);
    break;
    case (global.blacking = 4):
    draw_set_alpha(0.4);
    draw_set_colour(c_black);
    break;
    case (global.blacking = 5):
    draw_set_alpha(0.5);
    draw_set_colour(c_black);
    break;
    case (global.blacking = 6):
    draw_set_alpha(0.6);
    draw_set_colour(c_black);
    break;
    case (global.blacking = 7):
    draw_set_alpha(0.7);
    draw_set_colour(c_black);
    break;
    case (global.blacking = 8):
    draw_set_alpha(0.8);
    draw_set_colour(c_black);
    break;
    case (global.blacking = 9):
    draw_set_alpha(0.9);
    draw_set_colour(c_black);
    break;
    case (global.blacking = 10):
    draw_set_alpha(0.10);
    draw_set_colour(c_black);
    break;
  
    [...]
}
Wow, it physically pained me to read that. I don't know whether to congratulate you or pray for your soul.
 

csanyk

Member
When I was working on my game Alamogordo for Ludum Dare 29, I wanted to replicate the font used by the Atari 2600 in the game E.T.

I had not heard of sprite fonts before, and had never used them. So I improvised my own system. First, I fired up E.T. in an emulator and took screen captures of the score, taking care to get all 10 digits that I would need. I then cut out the numbers and put them into a sprite resource. As I recall, I had problems with the selection size not always being the same, or being exactly centered, so it took quite a bit of time just to get the sprite set up so that it was the right size for all digits, and all the digits were positioned the same relative to the sprite origin.

Next, I wrote a script that took a number that I passed into it, converted that number into a string, then took each digit in the string and ran it through a switch statement, matching the value in the substring against a case that drew the right sub-image of my sprite that held the images of all the digits, and correctly offset them from one another, to draw the score on the screen. Only... no, I didn't even do it like that! I actually used div to get the ones, tens, hundreds, and thousands digit, and then used that value as the subimage index to draw the sprite.

It worked beautifully, and at the time I thought it was brilliantly clever, but that there had to be a better way if only I had the time to RTFM.

I was using the variable name "finds" for storing the score because the player's score goes up whenever they uncover something as they dig through the Alomogordo landfill, finding it, and you got a certain amount of points for each find you discovered.

Code:
Create Event:
finds = 0;
image_index = 0;
image_speed = 0;

ones_digit = 0;
tens_digit = 0;
hundreds_digit = 0;
thousands_digit = 0;

Draw Event: 
ones_digit = finds mod 10;
tens_digit = finds div 10;
hundreds_digit = finds div 100;
thousands_digit = finds div 1000;

draw_sprite(sprite_index, ones_digit, x, y);
draw_sprite(sprite_index, tens_digit, x-sprite_width, y);
draw_sprite(sprite_index, hundreds_digit, x-(2*sprite_width), y);
draw_sprite(sprite_index, thousands_digit, x-(3*sprite_width), y);
At the time, my inspiration for the game was very last minute, I hadn't even intended to produce a project that weekend, but the idea came to me Sunday and I ended up putting the game together in literally about 10 hours altogether. So I was just pleased that it worked on the first try. I had literally no time to experiment or read through the manual in the hopes that I would find a proper way to do it!
 

Hyomoto

Member
Nah, I did that recently for the GM2 beta simply because it didn't have the sprite add font availability. It's nice that it's there, preferred even, but what's important is it worked for you and did what you wanted. That's good code.
 

Schwee

Member
I won't post the code but I had a whole stair system setup using multiple bools for different inclines, along with dozens of conditionals checking different speeds and slopes. Reduces over 100 lines by just using a while loop to auto adjust height if the difference is within 1 pixel.
 

csanyk

Member
Nah, I did that recently for the GM2 beta simply because it didn't have the sprite add font availability. It's nice that it's there, preferred even, but what's important is it worked for you and did what you wanted. That's good code.
Hmm, I wasn't aware that sprite fonts aren't in GMS2... (yet, I hope?) They are useful.
 
F

fxokz

Guest
Variable hell.
Code:
///sprite_draw_9patch(9patch list,sprite,subimg,x,y,w,h,color,alpha)
/* Draws a sprite with 9patch, sugin a data returned from sprite_get_9patch.
 * Written by darkwalker247
 */
var sp,sub,col,alp,ow,oh,nl,bx,by,bw,bh,sx,sy,sw,sh,cx,cy,cw,ch,sxs,sys,x1,y1,x2,y2,x3,y3,x4,y4,x5,y5,x6,y6,x7,y7,x8,y8,x9,y9;


sp=argument1
sub=argument2
col=argument7
alp=argument8
ow=sprite_get_width(sp)-2
oh=sprite_get_height(sp)-2
nl=argument0
bx=argument3-(sprite_get_xoffset(argument1))*(argument5/(ow))
by=argument4-(sprite_get_yoffset(argument1))*(argument6/(ow))
bw=argument5
bh=argument6
sx=ds_list_find_value(nl,0)+1
sy=ds_list_find_value(nl,1)+1
sw=ds_list_find_value(nl,2)
sh=ds_list_find_value(nl,3)
cx=ds_list_find_value(nl,4)
cy=ds_list_find_value(nl,5)
cw=ds_list_find_value(nl,6)
ch=ds_list_find_value(nl,7)
sxw=bw-(sx+(ow-(sx+sw)))
syh=bh-(sy+(oh-(sy+sh)))
sxs=sxw/sw
sys=syh/sh
x1=bx
y1=by
x2=bx+sx
y2=by
x3=x2+sxw
y3=by
x4=x3
y4=by+sy
x5=x3
y5=y4+syh
x6=x2
y6=y5
x7=bx
y7=y5
x8=bx
y8=y4
x9=bx+sx
y9=by+sy

draw_sprite_part_ext(sp,sub,1,1,sx,sy,x1,y1,1,1,col,alp) //1
draw_sprite_part_ext(sp,sub,sx,1,sw,sy,x2,y2,sxs,1,col,alp) //2
draw_sprite_part_ext(sp,sub,sx+sw,1,(ow+1)-(sx+sw),sy,x3,y3,1,1,col,alp) //3
draw_sprite_part_ext(sp,sub,sx+sw,sy,(ow+1)-(sx+sw),sh,x4,y4,1,sys,col,alp) //4
draw_sprite_part_ext(sp,sub,sx+sw,sy+sh,(ow+1)-(sx+sw),(oh+1)-(sy+sh),x5,y5,1,1,col,alp) //5
draw_sprite_part_ext(sp,sub,sx,sy+sh,sw,(oh+1)-(sy+sh),x6,y6,sxs,1,col,alp) //6
draw_sprite_part_ext(sp,sub,1,sy+sh,sx,(oh+1)-(sy+sh),x7,y7,1,1,col,alp) //7
draw_sprite_part_ext(sp,sub,1,sy,sx,sh,x8,y8,1,sys,col,alp) //8
draw_sprite_part_ext(sp,sub,sx,sy,sw,sh,x9,y9,sxs,sys,col,alp) //9
How the absolute F can you keep up with all of those vars ahahah
 

Hyomoto

Member
Hmm, I wasn't aware that sprite fonts aren't in GMS2... (yet, I hope?) They are useful.
They are now as part of the desktop beta, but the trial beta did not. However, my way of dealing with it was a bit different. I just stored strings as numbers in an array. Basically I did the conversion when the string was set, rather than when it was drawn, so I only had to do it once and I could just call draw_sprite directly. Generally calculations are, for whatever reason, slower during the draw event so it's often faster to cache your results during a step event.
 

RujiK

Member
@Ninety @Joe

Actually, saving the game every frame isn't that weird.

How do you think overwatch, COD, and CS:GO have a replay function? They literally make a mini-savegame every frame.
 
D

Dark

Guest
Considering that GM saves are almost like a savestate that's a terrible idea with a big game.
 

Changgi

Member
Considering that GM saves are almost like a savestate that's a terrible idea with a big game.
I found out that GM8 at least requires the object holding the variable to be present in the room during the save in order for a local variable to be saved.
 

Ihato

Member
Then that's no code, thus it isn't a bad code.
I'd like to put this quote right here:
"A program that produces incorrect results twice as fast is infinitely slower." - John Ousterhot
Similarly a program that produces no results at all is infinitely slower than a badly written program which does.
 

Tsa05

Member
Back in my chatbot-programming days I used to use the following code pretty often:
execute_string()

shudder
 

JackTurbo

Member
Been working on a turn based strategy on and off for a while and recently took another look at my prototype GUI code.

Dont have the code to hand but basically when it was a unit's turn it would display a portrait of the character in the bottom corner of the screen.

What made the code so bad is that it would just keep drawing the image every frame and when it was the next units turn it would just draw them on top too, without ever destroying any thing.

So by like 2 mins or so into a battle there would be a stack of around 10,000 of the same 3 or 4 images drawn on top of each other.
 

gitwalrus

Member
Not GML but I work as a C programmer for embedded devices. I can't remember exactly what I did but I messed up the code and any control panel that had the new code downloaded to it would immediately become a brick and cease to start or do anything at all. Locked up like three control panels trying to fix this.
 
My very first Visual Basic 6 project. to be sure to catch any errors in a function, I had put an "on error resume next" before every single line of code inside the same function. Horrible, HORRIBLE piece of code, sigh....:confused:
 
E

Ethanicus

Guest
I would show you some code on my character in Dreamer, but I don't think I can pinpoint ONE awful bit. I'd have to upload the whole thing. I started years ago and never realized how atrocious it all is until recently.
 
Top