GML Need help with some code!

J

Joshua Hotchin

Guest
Hey,
the code is here:

(In "press <Space>" Event)
Code:
/// Spawn the bullets
if playerdir = 0 {
    if powerup_bulletsize = 4 instance_create(x+2, y+4, obj_bullet4x4_up) else
    if powerup_bulletsize = 8 instance_create(x+2, y+4, obj_bullet8x8_up) else
    if powerup_bulletsize = 16 instance_create(x+2, y+4, obj_bullet16x16_up) else

if playerdir = 1 {
    if powerup_bulletsize = 4 instance_create(x+2, y+4, obj_bullet4x4_right) else
    if powerup_bulletsize = 8 instance_create(x+2, y+4, obj_bullet8x8_right) else
    if powerup_bulletsize = 16 instance_create(x+2, y+4, obj_bullet16x16_right) else

if playerdir = 2 {
    if powerup_bulletsize = 4 instance_create(x+2, y+4, obj_bullet4x4_down) else 
    if powerup_bulletsize = 8 instance_create(x+2, y+4, obj_bullet8x8_down) else 
    if powerup_bulletsize = 16 instance_create(x+2, y+4, obj_bullet16x16_down) else 

if playerdir = 3 {
    if powerup_bulletsize = 4 instance_create(x+2, y+4, obj_bullet4x4_left) else 
    if powerup_bulletsize = 8 instance_create(x+2, y+4, obj_bullet8x8_left) else 
    if powerup_bulletsize = 16 instance_create(x+2, y+4, obj_bullet16x16_left)
}}}}
When I start the game and press space, nothing happens even though playerdir is set to 1 (right). If I press "W" and then space it shoots upwards (which it is meant to do) but when I press "D" (right) and then press space no bullets come out.

BTW WASD are movement keys. Just in case.
 

NightFrost

Member
You aren't showing how WASD keys are translated into playerdir values. Check that you've written that bit correctly, so that pressing "D" actually sets the variable to 1, etc.
 
Z

Zghaun

Guest
if playerdir = 0
{
switch(powerup_bulletsize)
{
case 4:
instance_create(x+2, y+4, obj_bullet4x4_up)
break;

case 8:
instance_create(x+2, y+4, obj_bullet8x8_up)
break;

case 16:
instance_create(x+2, y+4, obj_bullet16x16_up)
break;
}
}

if playerdir = 1
{
switch(powerup_bulletsize)
{
case 4:
instance_create(x+2, y+4, obj_bullet4x4_right)
break;

case 8:
instance_create(x+2, y+4, obj_bullet8x8_right)
break;

case 16:
instance_create(x+2, y+4, obj_bullet16x16_right)
break;
}
}
you get the idea, there is a more optimized and faster version though
 

NightFrost

Member
Acutally, your code seems messed up a bit as those else if commands nest it incorectly. Straightening up the line feeds makes it look like this:
Code:
/// Spawn the bullets
if playerdir = 0 {
    if powerup_bulletsize = 4 instance_create(x+2, y+4, obj_bullet4x4_up)
    else if powerup_bulletsize = 8 instance_create(x+2, y+4, obj_bullet8x8_up)
    else if powerup_bulletsize = 16 instance_create(x+2, y+4, obj_bullet16x16_up)
    else if playerdir = 1 {
        if powerup_bulletsize = 4 instance_create(x+2, y+4, obj_bullet4x4_right)
        else if powerup_bulletsize = 8 instance_create(x+2, y+4, obj_bullet8x8_right)
        else if powerup_bulletsize = 16 instance_create(x+2, y+4, obj_bullet16x16_right)
        else if playerdir = 2 {
            if powerup_bulletsize = 4 instance_create(x+2, y+4, obj_bullet4x4_down)
            else if powerup_bulletsize = 8 instance_create(x+2, y+4, obj_bullet8x8_down)
            else if powerup_bulletsize = 16 instance_create(x+2, y+4, obj_bullet16x16_down)
            else if playerdir = 3 {
                if powerup_bulletsize = 4 instance_create(x+2, y+4, obj_bullet4x4_left)
                else if powerup_bulletsize = 8 instance_create(x+2, y+4, obj_bullet8x8_left)
                else if powerup_bulletsize = 16 instance_create(x+2, y+4, obj_bullet16x16_left)
            }
        }
    }
}
See the problem?
 
J

Joshua Hotchin

Guest
I switched to NightFrost's code but it still isn't working, I set playerdir in WASD pressed events:

Code:
In event: Press A key
playerdir = 3
Code:
In event: Press W key
playerdir = 0
Code:
In event: Press S key
playerdir = 2
Code:
In event: Press D key
playerdir = 1
Still isn't working!
 

YoSniper

Member
Here's my take on it, combining the two parts of code:

Step Event
Code:
///Control input part
if keyboard_check_pressed(ord("W")) {
    playerdir = 0;
} else if keyboard_check_pressed(ord("D")) {
    playerdir = 1;
} else if keyboard_check_pressed(ord("S")) {
    playerdir = 2;
} else if keyboard_check_pressed(ord("A")) {
    playerdir = 3;
}

///Spawning bullets
var bullet_index;
bullet_index = noone;  //Default - also used for debugging later
if keyboard_check_pressed(vk_space) {
    switch(playerdir) {
        case 0: //Up
            switch(powerup_bulletsize) {
                case 4:
                    bullet_index = obj_bullet4x4_up;
                    break;
               case 8:
                   bullet_index = obj_bullet8x8_up;
                   break;
               case 16:
                   bullet_index = obj_bullet16x16_up;
                   break;
            }
            break;
        
        case 1: //Right
            switch(powerup_bulletsize) {
                case 4:
                    bullet_index = obj_bullet4x4_right;
                    break;
               case 8:
                   bullet_index = obj_bullet8x8_right;
                   break;
               case 16:
                   bullet_index = obj_bullet16x16_right;
                   break;
            }
            break;
       
       case 2: //Down
            switch(powerup_bulletsize) {
                case 4:
                    bullet_index = obj_bullet4x4_down;
                    break;
               case 8:
                   bullet_index = obj_bullet8x8_down;
                   break;
               case 16:
                   bullet_index = obj_bullet16x16_down;
                   break;
            }
            break;
       
        case 3: //Left
            switch(powerup_bulletsize) {
                case 4:
                    bullet_index = obj_bullet4x4_left;
                    break;
               case 8:
                   bullet_index = obj_bullet8x8_left;
                   break;
               case 16:
                   bullet_index = obj_bullet16x16_left;
                   break;
            }
            break;
    }
    
    if bullet_index == noone {    //Something is wrong with the values if this happens
        show_message("ERROR: Could not spawn a bullet with playerdir = " + string(playerdir) + ", powerup_bulletsize = " + string(powerup_bulletsize));
    } else {
        instance_create(x + 2, y + 4, bullet_index);
    }
}
 
J

Joshua Hotchin

Guest
Here's my take on it, combining the two parts of code:

Step Event
Code:
///Control input part
if keyboard_check_pressed(ord("W")) {
    playerdir = 0;
} else if keyboard_check_pressed(ord("D")) {
    playerdir = 1;
} else if keyboard_check_pressed(ord("S")) {
    playerdir = 2;
} else if keyboard_check_pressed(ord("A")) {
    playerdir = 3;
}

///Spawning bullets
var bullet_index;
bullet_index = noone;  //Default - also used for debugging later
if keyboard_check_pressed(vk_space) {
    switch(playerdir) {
        case 0: //Up
            switch(powerup_bulletsize) {
                case 4:
                    bullet_index = obj_bullet4x4_up;
                    break;
               case 8:
                   bullet_index = obj_bullet8x8_up;
                   break;
               case 16:
                   bullet_index = obj_bullet16x16_up;
                   break;
            }
            break;
       
        case 1: //Right
            switch(powerup_bulletsize) {
                case 4:
                    bullet_index = obj_bullet4x4_right;
                    break;
               case 8:
                   bullet_index = obj_bullet8x8_right;
                   break;
               case 16:
                   bullet_index = obj_bullet16x16_right;
                   break;
            }
            break;
      
       case 2: //Down
            switch(powerup_bulletsize) {
                case 4:
                    bullet_index = obj_bullet4x4_down;
                    break;
               case 8:
                   bullet_index = obj_bullet8x8_down;
                   break;
               case 16:
                   bullet_index = obj_bullet16x16_down;
                   break;
            }
            break;
      
        case 3: //Left
            switch(powerup_bulletsize) {
                case 4:
                    bullet_index = obj_bullet4x4_left;
                    break;
               case 8:
                   bullet_index = obj_bullet8x8_left;
                   break;
               case 16:
                   bullet_index = obj_bullet16x16_left;
                   break;
            }
            break;
    }
   
    if bullet_index == noone {    //Something is wrong with the values if this happens
        show_message("ERROR: Could not spawn a bullet with playerdir = " + string(playerdir) + ", powerup_bulletsize = " + string(powerup_bulletsize));
    } else {
        instance_create(x + 2, y + 4, bullet_index);
    }
}
Thanks, this fixed in completely!
 

NightFrost

Member
I switched to NightFrost's code but it still isn't working
Um, I didn't make any changes, I just reformatted the lines so it would be easier for you to see where things were going wrong. If you look through the code, you'll notice that all the ifs are nested inside each other. The entire code block will only run if playerdir is zero, so checking it for any other value inside it is obviously going to return a false. To work, the ifs would need to be de-nested in the same manner YoSniper turned them into switch-statements above.
 
J

Joshua Hotchin

Guest
Um, I didn't make any changes, I just reformatted the lines so it would be easier for you to see where things were going wrong. If you look through the code, you'll notice that all the ifs are nested inside each other. The entire code block will only run if playerdir is zero, so checking it for any other value inside it is obviously going to return a false. To work, the ifs would need to be de-nested in the same manner YoSniper turned them into switch-statements above.
Sorry, I meant to say it is working now after switching to you code you put in, thanks!
 
Top