• 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!

GML Logical Error, please help

S

Sroft

Guest
I am creating this piece of code that should let you switch weapons by right clicking your mouse, but it doesn't work, please help. I have a variable (soulswap) the number increases when you pick up a certain weapon
-PLAYERS CODE
switch (weapons)
{
case WEAPONS_MODE.HANDS:
{
Unarmed();
break;
}
case WEAPONS_MODE.PLASMA:
{
Plasmagun();
break;
}
}
-UNARMED
if (!place_meeting(x,y+1,WObject))
{
sprite_index = SHeroA;
image_speed = 20;
if (sign(vsp) > 20) image_index = 1; else image_index = 1;
}
else
{
image_speed = 1;
if (hsp == 0)
{
sprite_index = SHero;
}
else
{
sprite_index = SHeroR;
}
}
if (hsp != 0) image_xscale = sign(hsp);

//soul switch
if (mouse_check_button_pressed(vk_right)) && (Soulswap >= 1) && (weapons != WEAPONS_MODE.PLASMA)
{
weapons = WEAPONS_MODE.PLASMA
}
else
{
weapons = WEAPONS_MODE.HANDS
}
-PLASMA
if (!place_meeting(x,y+1,WObject))
{
sprite_index = sPGunPlasmaA;
image_speed = 20;
if (sign(vsp) > 20) image_index = 1; else image_index = 1;
}
else
{
image_speed = 1;
if (hsp == 0)
{
sprite_index = sPGunPlasma;

}
else
{
sprite_index = sPGunPlasmaR;

}
}
if (hsp != 0) image_xscale = sign(hsp);

//Shooting
firing = firing -0.25;
if (ShootCont)
{
if (mouse_check_button(mb_left)) and (firing <0)
{
firing = 5;
with (instance_create_layer(x,y,"Guns",GObject))
{

}
}

// bullet animations
if (sprite_index == sPGunPlasma) and (mouse_check_button(mb_left))
{
sprite_index = sPGunPlasmaSattack;
}

if (sprite_index == sPGunPlasmaR) and (mouse_check_button(mb_left))
{
sprite_index = sPGunPlasmaRattack;
}

if ( sprite_index == sPGunPlasmaA)
{
firing = 1
}
}

//switching souls
if (mouse_check_button_pressed(vk_right)) && (Soulswap >= 2)
{
weapons = WEAPONS_MODE.ROBOT;
}
else
{
weapons = WEAPONS_MODE.HANDS;
}
if this is not enough information of the code to go by, please just let me know
Thank you
 

samspade

Member
In addition to what IndianaBones just said, please remember to use code brackets and proper indentation. Your code is very hard to read otherwise.

Initially I see a few possible problems.

First, I don't see anything which increases the variable soulswap. Second, while I can't really tell what code is in which sections, something about your soulswap sections looks wrong.
 

SeraphSword

Member
First of all, use the code formatting to make your code readable on here. It's just [ code ] at the beginning and [ / code ] at the end (but no spaces).

As for your problem, one thing that immediately jumped out was that you are using brackets after every case in the switch statement. The only brackets you need are after the switch and after all cases.
So like this:
Code:
switch (weapons)
{
   case WEAPONS_MODE.HANDS:
       Unarmed();
       break;
   case WEAPONS_MODE.PLASMA:
       Plasmagun();
       break;
}
You also have multiple extraneous brackets in your if statements. For instance after if(ShootCont) and after if ( sprite_index == sPGunPlasmaA).

Also, the code after //soul switch and //switching souls is probably conflicting. You say for the Robot mode that if the value is greater than or equal to 2, but the earlier one about Plasma says greater than or equal to 1. So if the value is 2, both would be true (since two is greater than the 1 needed for Plasma, and equal to the 2 needed for Robot).
And Robot isn't in the initial switch talking about weapons. If you have a switch like that it should cover all cases, with any stray issues going under default: .

Because I'm a saint I formatted your original code in case anyone can find things I missed:
Code:
-PLAYERS CODE
switch (weapons)
{
   case WEAPONS_MODE.HANDS:
   {
       Unarmed();
       break;
   }
       case WEAPONS_MODE.PLASMA:
   {
       Plasmagun();
       break;
   }
}
-UNARMED
if (!place_meeting(x,y+1,WObject))
{
   sprite_index = SHeroA;
   image_speed = 20;
   if (sign(vsp) > 20) image_index = 1; else image_index = 1;
}
else
{
   image_speed = 1;
   if (hsp == 0)
   {
       sprite_index = SHero;
   }
   else
   {
       sprite_index = SHeroR;
   }
}
if (hsp != 0) image_xscale = sign(hsp);

//soul switch
if (mouse_check_button_pressed(vk_right)) && (Soulswap >= 1) && (weapons != WEAPONS_MODE.PLASMA)
{
   weapons = WEAPONS_MODE.PLASMA
}
else
{
   weapons = WEAPONS_MODE.HANDS
}
-PLASMA
if (!place_meeting(x,y+1,WObject))
{
   sprite_index = sPGunPlasmaA;
   image_speed = 20;
   if (sign(vsp) > 20) image_index = 1; else image_index = 1;
}
else
{
   image_speed = 1;
   if (hsp == 0)
   {
   sprite_index = sPGunPlasma;

   }
   else
   {
       sprite_index = sPGunPlasmaR;

   }
}
if (hsp != 0) image_xscale = sign(hsp);

//Shooting
firing = firing -0.25;
if (ShootCont)
{
   if (mouse_check_button(mb_left)) and (firing <0)
   {
       firing = 5;
       with (instance_create_layer(x,y,"Guns",GObject))
   {

}
}

// bullet animations
if (sprite_index == sPGunPlasma) and (mouse_check_button(mb_left))
{
   sprite_index = sPGunPlasmaSattack;
}

if (sprite_index == sPGunPlasmaR) and (mouse_check_button(mb_left))
{
   sprite_index = sPGunPlasmaRattack;
}

if ( sprite_index == sPGunPlasmaA)
{
   firing = 1
}
}

//switching souls
if (mouse_check_button_pressed(vk_right)) && (Soulswap >= 2)
{
   weapons = WEAPONS_MODE.ROBOT;
}
else
{
   weapons = WEAPONS_MODE.HANDS;
}
 
Last edited:
S

Sroft

Guest
First of all, use the code formatting to make your code readable on here. It's just [ code ] at the beginning and [ / code ] at the end (but no spaces).

As for your problem, one thing that immediately jumped out was that you are using brackets after every case in the switch statement. The only brackets you need are after the switch and after all cases.
So like this:
Code:
switch (weapons)
{
   case WEAPONS_MODE.HANDS:
       Unarmed();
       break;
   case WEAPONS_MODE.PLASMA:
       Plasmagun();
       break;
}
You also have multiple extraneous brackets in your if statements. For instance after if(ShootCont) and after if ( sprite_index == sPGunPlasmaA).

Also, the code after //soul switch and //switching souls is probably conflicting. You say for the Robot mode that if the value is greater than or equal to 2, but the earlier one about Plasma says greater than or equal to 1. So if the value is 2, both would be true (since two is greater than the 1 needed for Plasma, and equal to the 2 needed for Robot).
And Robot isn't in the initial switch talking about weapons. If you have a switch like that it should cover all cases, with any stray issues going under default: .

Because I'm a saint I formatted your original code in case anyone can find things I missed:
Code:
-PLAYERS CODE
switch (weapons)
{
   case WEAPONS_MODE.HANDS:
   {
       Unarmed();
       break;
   }
       case WEAPONS_MODE.PLASMA:
   {
       Plasmagun();
       break;
   }
}
-UNARMED
if (!place_meeting(x,y+1,WObject))
{
   sprite_index = SHeroA;
   image_speed = 20;
   if (sign(vsp) > 20) image_index = 1; else image_index = 1;
}
else
{
   image_speed = 1;
   if (hsp == 0)
   {
       sprite_index = SHero;
   }
   else
   {
       sprite_index = SHeroR;
   }
}
if (hsp != 0) image_xscale = sign(hsp);

//soul switch
if (mouse_check_button_pressed(vk_right)) && (Soulswap >= 1) && (weapons != WEAPONS_MODE.PLASMA)
{
   weapons = WEAPONS_MODE.PLASMA
}
else
{
   weapons = WEAPONS_MODE.HANDS
}
-PLASMA
if (!place_meeting(x,y+1,WObject))
{
   sprite_index = sPGunPlasmaA;
   image_speed = 20;
   if (sign(vsp) > 20) image_index = 1; else image_index = 1;
}
else
{
   image_speed = 1;
   if (hsp == 0)
   {
   sprite_index = sPGunPlasma;

   }
   else
   {
       sprite_index = sPGunPlasmaR;

   }
}
if (hsp != 0) image_xscale = sign(hsp);

//Shooting
firing = firing -0.25;
if (ShootCont)
{
   if (mouse_check_button(mb_left)) and (firing <0)
   {
       firing = 5;
       with (instance_create_layer(x,y,"Guns",GObject))
   {

}
}

// bullet animations
if (sprite_index == sPGunPlasma) and (mouse_check_button(mb_left))
{
   sprite_index = sPGunPlasmaSattack;
}

if (sprite_index == sPGunPlasmaR) and (mouse_check_button(mb_left))
{
   sprite_index = sPGunPlasmaRattack;
}

if ( sprite_index == sPGunPlasmaA)
{
   firing = 1
}
}

//switching souls
if (mouse_check_button_pressed(vk_right)) && (Soulswap >= 2)
{
   weapons = WEAPONS_MODE.ROBOT;
}
else
{
   weapons = WEAPONS_MODE.HANDS;
}
Thank you so much! Sorry for the late response, just keeping up with other things in life, but thank you so much!
 
Top