SOLVED Code not working HELP

M

MasterOfTheYeetz

Guest
so basically my code doesn't work and it doesn't check if obstacles are repeating. WHY IS THIS CODE NO WORK??!?!?!?!??!?!?!?!?!?
GML:
        if (global.speedmodifier > 1)
        {
            if (count == 1)
            {
                var j = instance_create_layer(room_width +100, room_height - 75, "Instances", oObstacle);
                j.sprite_index = choose(Sprite5, Sprite6, Sprite7, Sprite8, Sprite9 );
                j.image_speed = 0;
                j.image_index = 1;
              
        if (!j.sprite_index = (Sprite5)) {
            var j = instance_create_layer(room_width +100, room_height - 75, "Instances", oObstacle);
            j.sprite_index = choose(Sprite6, Sprite7, Sprite8, Sprite9); 
            j.image_speed = 0;
            j.image_index = 1;
}
    if (!j.sprite_index = (Sprite6)) {
            var j = instance_create_layer(room_width +100, room_height - 75, "Instances", oObstacle);
            j.sprite_index = choose(Sprite5, Sprite7, Sprite8, Sprite9); 
            j.image_speed = 0;
            j.image_index = 1;
}
    if (!j.sprite_index = (Sprite7)) {
            var j = instance_create_layer(room_width +100, room_height - 75, "Instances", oObstacle);
            j.sprite_index = choose(Sprite6, Sprite5, Sprite8, Sprite9); 
            j.image_speed = 0;
            j.image_index = 1;
}
    if (!j.sprite_index = (Sprite8)) {
            var j = instance_create_layer(room_width +100, room_height - 75, "Instances", oObstacle);
            j.sprite_index = choose(Sprite6, Sprite7, Sprite5, Sprite9); 
            j.image_speed = 0;
            j.image_index = 1;
}
    if (!j.sprite_index = (Sprite9)) {
            var j = instance_create_layer(room_width +100, room_height - 75, "Instances", oObstacle);
            j.sprite_index = choose(Sprite6, Sprite7, Sprite8, Sprite5); 
            j.image_speed = 0;
            j.image_index = 1;
}
 
Last edited by a moderator:
Strange structure aside, the checks "(!j.sprite_index = (Sprite5))", etc. are probably not doing what you want. But it's hard to say, since you haven't clarified what this is supposed to do in general, and how it's failing for you.
Anyhow, maybe you want one of these two?
  • (j.sprite_index != Sprite5) -- "j's sprite_index is not equal to Sprite5"
  • (j.sprite_index == Sprite5) -- "j's sprite_index is equal to Sprite5". Note the double-equals. This checks for equality instead of trying to assign the variable right then and there.
    • Actually I think GM detects if a single "=" is in a conditional and assumes it's an equality check anyway, but I'm not sure. Best practice is to use "==" when you want to check if two things are equal, "=" when you are assigning something.
 
M

MasterOfTheYeetz

Guest
You seem to be missing some } there
where?
I made some edits to the code
edit: that } I just forgot to copy
GML:
        if (global.speedmodifier > 1)
        {
            if (count == 1)
            {
                var j = instance_create_layer(room_width +100, room_height - 75, "Instances", oObstacle);
                j.sprite_index = choose(Sprite5, Sprite6, Sprite7, Sprite8, Sprite9 );
                j.image_speed = 0;
                j.image_index = 1;
            if (global.speedmodifier > 1)
        {
            if (count == 1)
            {  
   
        if (j.sprite_index = (Sprite5))
        {
            var j = instance_create_layer(room_width +100, room_height - 75, "Instances", oObstacle);
            j.sprite_index = choose(Sprite6, Sprite7, Sprite8, Sprite9);  
            j.image_speed = 0;
            j.image_index = 1;

}
            }
        }
                if (global.speedmodifier > 1)
        {
            if (count == 1)
            {
    if (j.sprite_index = (Sprite6)) {
            var j = instance_create_layer(room_width +100, room_height - 75, "Instances", oObstacle);
            j.sprite_index = choose(Sprite5, Sprite7, Sprite8, Sprite9);  
            j.image_speed = 0;
            j.image_index = 1;
}
            }
        }
                if (global.speedmodifier > 1)
        {
            if (count == 1)
            {
    if (j.sprite_index = (Sprite7)) {
            var j = instance_create_layer(room_width +100, room_height - 75, "Instances", oObstacle);
            j.sprite_index = choose(Sprite6, Sprite5, Sprite8, Sprite9);  
            j.image_speed = 0;
            j.image_index = 1;
}
            }
        }
                if (global.speedmodifier > 1)
        {
            if (count == 1)
            {
    if (j.sprite_index = (Sprite8)) {
            var j = instance_create_layer(room_width +100, room_height - 75, "Instances", oObstacle);
            j.sprite_index = choose(Sprite6, Sprite7, Sprite5, Sprite9);  
            j.image_speed = 0;
            j.image_index = 1;
}
            }
        }
                if (global.speedmodifier > 1)
        {
            if (count == 1)
            {
    if (j.sprite_index = (Sprite9)) {
            var j = instance_create_layer(room_width +100, room_height - 75, "Instances", oObstacle);
            j.sprite_index = choose(Sprite6, Sprite7, Sprite8, Sprite5);  
            j.image_speed = 0;
            j.image_index = 1;
}
            }
        }
            }
        }
 
You and those you ask for help will have a much better time following the code if it is indented properly.

When indented, it looks like there are some very strange choices like checking if (global.speedmodifier > 1) several times in a row. subsequent ifs for the same condition do nothing, assuming the condition didn't change (which it seems not to in this code).

I'd recommend explaining what the code is supposed to do and how it is failing.

GML:
if (global.speedmodifier > 1)
{
   if (count == 1)
   {
      var j = instance_create_layer(room_width +100, room_height - 75, "Instances", oObstacle);
      j.sprite_index = choose(Sprite5, Sprite6, Sprite7, Sprite8, Sprite9 );
      j.image_speed = 0;
      j.image_index = 1;
      if (global.speedmodifier > 1)
      {
         if (count == 1)
         {
            
            if (j.sprite_index = (Sprite5))
            {
               var j = instance_create_layer(room_width +100, room_height - 75, "Instances", oObstacle);
               j.sprite_index = choose(Sprite6, Sprite7, Sprite8, Sprite9);
               j.image_speed = 0;
               j.image_index = 1;
              
            }
         }
      }
      if (global.speedmodifier > 1)
      {
         if (count == 1)
         {
            if (j.sprite_index = (Sprite6)) {
               var j = instance_create_layer(room_width +100, room_height - 75, "Instances", oObstacle);
               j.sprite_index = choose(Sprite5, Sprite7, Sprite8, Sprite9);
               j.image_speed = 0;
               j.image_index = 1;
            }
         }
      }
      if (global.speedmodifier > 1)
      {
         if (count == 1)
         {
            if (j.sprite_index = (Sprite7)) {
               var j = instance_create_layer(room_width +100, room_height - 75, "Instances", oObstacle);
               j.sprite_index = choose(Sprite6, Sprite5, Sprite8, Sprite9);
               j.image_speed = 0;
               j.image_index = 1;
            }
         }
      }
      if (global.speedmodifier > 1)
      {
         if (count == 1)
         {
            if (j.sprite_index = (Sprite8)) {
               var j = instance_create_layer(room_width +100, room_height - 75, "Instances", oObstacle);
               j.sprite_index = choose(Sprite6, Sprite7, Sprite5, Sprite9);
               j.image_speed = 0;
               j.image_index = 1;
            }
         }
      }
      if (global.speedmodifier > 1)
      {
         if (count == 1)
         {
            if (j.sprite_index = (Sprite9)) {
               var j = instance_create_layer(room_width +100, room_height - 75, "Instances", oObstacle);
               j.sprite_index = choose(Sprite6, Sprite7, Sprite8, Sprite5);
               j.image_speed = 0;
               j.image_index = 1;
            }
         }
      }
   }
}
 
M

MasterOfTheYeetz

Guest
Strange structure aside, the checks "(!j.sprite_index = (Sprite5))", etc. are probably not doing what you want. But it's hard to say, since you haven't clarified what this is supposed to do in general, and how it's failing for you.
Anyhow, maybe you want one of these two?
  • (j.sprite_index != Sprite5) -- "j's sprite_index is not equal to Sprite5"
  • (j.sprite_index == Sprite5) -- "j's sprite_index is equal to Sprite5". Note the double-equals. This checks for equality instead of trying to assign the variable right then and there.
    • Actually I think GM detects if a single "=" is in a conditional and assumes it's an equality check anyway, but I'm not sure. Best practice is to use "==" when you want to check if two things are equal, "=" when you are assigning something.
I want it to check is Sprite (something) is displayed and not choose that sprite but a different sprite.
 

Nidoking

Member
You'll need to use else, at the very least. And you're creating a lot of extra instances, which doesn't seem to be what you want.
 
M

MasterOfTheYeetz

Guest
ok my code so far for my controller:
GML:
randomize();
var count = irandom_range(1, 2);

var i = instance_create_layer(room_width +100, room_height - 75, "Instances", oObstacle);
i.sprite_index = choose(Sprite5, Sprite6, Sprite7, Sprite8, Sprite9);
switch (i.sprite_index)
{
    case Sprite5:
    case Sprite6:
    case Sprite7:
    case Sprite8:
    case Sprite9:
        i.image_speed = 0;
        i.image_index = irandom_range(0, sprite_get_number(i.sprite_index) - 9)
        
        
        
        if (global.speedmodifier > 1)
        {
            if (count == 1)
            {
                var j = instance_create_layer(room_width +100, room_height - 75, "Instances", oObstacle);
                j.sprite_index = choose(Sprite5, Sprite6, Sprite7, Sprite8, Sprite9 );
                j.image_speed = 0;
                j.image_index = 1;
                
        if (j.sprite_index == (Sprite5)) {
            var j = instance_create_layer(room_width +100, room_height - 75, "Instances", oObstacle);
            j.sprite_index = choose(Sprite6, Sprite7, Sprite8, Sprite9);   
            j.image_speed = 0;
            j.image_index = 1;
}
    if (j.sprite_index == (Sprite6)) {
            var j = instance_create_layer(room_width +100, room_height - 75, "Instances", oObstacle);
            j.sprite_index = choose(Sprite5, Sprite7, Sprite8, Sprite9);   
            j.image_speed = 0;
            j.image_index = 1;
}
    if (j.sprite_index == (Sprite7)) {
            var j = instance_create_layer(room_width +100, room_height - 75, "Instances", oObstacle);
            j.sprite_index = choose(Sprite6, Sprite5, Sprite8, Sprite9);   
            j.image_speed = 0;
            j.image_index = 1;
}
    if (j.sprite_index == (Sprite8)) {
            var j = instance_create_layer(room_width +100, room_height - 75, "Instances", oObstacle);
            j.sprite_index = choose(Sprite6, Sprite7, Sprite5, Sprite9);   
            j.image_speed = 0;
            j.image_index = 1;
}
    if (j.sprite_index == (Sprite9)) {
            var j = instance_create_layer(room_width +100, room_height - 75, "Instances", oObstacle);
            j.sprite_index = choose(Sprite6, Sprite7, Sprite8, Sprite5);   
            j.image_speed = 0;
            j.image_index = 1;
}
            }
        }
        
    break;
    
    default:
    i.image_speed = 1;
    i.y = choose(room_height - 85, room_height -145, room_height - 180);   
    
}

alarm[0] = room_speed * (irandom_range(3,4))
 

Nidoking

Member
how would I stop it?
You'll need to use else, at the very least.
If you're familiar with else, then I'll point out that you're doing five sequential checks for sprite_index, and based on that, you're creating anywhere from four to six instances of oObstacle.

If you're not familiar with else, then I think a good tutorial in basic GML will do you much more good than continuing to work on this problem at present. Save your game, do the tutorial, then come back to this and see if you can figure out what you've done wrong.
 
M

MasterOfTheYeetz

Guest
If you're familiar with else, then I'll point out that you're doing five sequential checks for sprite_index, and based on that, you're creating anywhere from four to six instances of oObstacle.

If you're not familiar with else, then I think a good tutorial in basic GML will do you much more good than continuing to work on this problem at present. Save your game, do the tutorial, then come back to this and see if you can figure out what you've done wrong.
the thing is, I have a deadline of the 15th for this game but I'm supposed to have a test by 7:30 m est
 

Nidoking

Member
Does the school you're applying to expect you to have basic competence in programming prior to applying? If the answer is yes, then my professional opinion is that you should save yourself the application fee, take the year off, and spend the time doing tutorials. If your school expects you to be able to turn what you've posted here into a working game before enrolling, then you're not ready, and I don't think having this forum do your homework for you is in conformance with the school's ethics policy.
 
M

MasterOfTheYeetz

Guest
Does the school you're applying to expect you to have basic competence in programming prior to applying? If the answer is yes, then my professional opinion is that you should save yourself the application fee, take the year off, and spend the time doing tutorials. If your school expects you to be able to turn what you've posted here into a working game before enrolling, then you're not ready, and I don't think having this forum do your homework for you is in conformance with the school's ethics policy.
No its an extra thing.
 
Top