Jump to content


Photo

Need help using 1 object for multiple things


  • Please log in to reply
11 replies to this topic

#1 Astal

Astal

    GMC Member

  • GMC Member
  • 247 posts

Posted 06 August 2012 - 08:35 PM

Ok this is what im trying to do.

I have 1 object, i want to be able to place it 1 or several times in different rooms throughout the game and have it doing different things each time its ran in to.

I tried doing it like below, the problem is the first one triggers fine, then the next one i try to trigger sets them all off, if i dont have the global.triggered and try to use the array only they dont trigger at all after the first one


/////////////////////////////////////////Trigger outside mansion/////////////////////////////////////////////
if(global.Trigger_Triggered[0] == 0 && global.triggered == 0)
 {
    Message("Hmmmm a cabin, I better go inside and get out of these woods these zombies freak me out!");
    global.Trigger_Triggered[0] = 1;
    global.triggered = 1;

    with(106456)
    {
    instance_destroy();
    }
    //Alarm[3] = 30;
 }

    
/////////////////////////////////////////Trigger outside mansion/////////////////////////////////////////////
    
/////////////////////////////////////////Trigger inside mansion, room to left of main room/////////////////////////////////////////////
    else if (global.Trigger_Triggered[1] == 0 && global.triggered == 1) //top left
    {    
        instance_create(192,208,obj_Zombie);
        instance_create(544,208,obj_Zombie); 
        global.Trigger_Triggered[1] = 1;   
        global.triggered = 2;   
         
        with(107200)
        {
        instance_destroy();
        }
        
        //Alarm[3] = 30;
    }

    else if (global.Trigger_Triggered[2] == 0 && global.triggered == 2) //top right
    {    

        instance_create(880,176,obj_Zombie);
        instance_create(1056,304,obj_Zombie);   
        global.Trigger_Triggered[2] = 1;
        global.triggered = 3;

        with(107202)
        {
        instance_destroy();
        }
        
        //Alarm[3] = 30;
    }

    
    else if (global.Trigger_Triggered[3] == 0 && global.triggered == 3) //bottom left
    {    

        instance_create(224,480,obj_Zombie);
        instance_create(544,576,obj_Zombie); 
        global.Trigger_Triggered[3] = 1;
        global.triggered = 4;
                
        with(107205)
        {
        instance_destroy();
        }

        //Alarm[3] = 30;        
    }


    
    else if (global.Trigger_Triggered[4] == 0 && global.triggered == 4)
    {    
        instance_create(640,544,obj_Zombie);
        instance_create(1088,560,obj_Zombie); 
        global.Trigger_Triggered[4] = 1;
        global.triggered = 5;               
        with(107203)
        {
        instance_destroy();
        }

        //Alarm[3] = 30;
    }

/////////////////////////////////////////Trigger inside mansion, room to left of main room/////////////////////////////////////////////    

/////////////////////////////////////////Trigger inside mansion, 2nd room to the left of the main room/////////////////////////////////////////////    

    else if (global.Trigger_Triggered[5] == 0 && global.triggered == 5)
    {    

        instance_create(944,192,obj_Zombie);
        instance_create(672,352,obj_Zombie); 
        instance_create(944,544,obj_Zombie); 
        global.Trigger_Triggered[5] = 1;
        global.triggered = 6; 
             
        with(107197)
        {
        instance_destroy();
        }
        
        //Alarm[3] = 30; 
    }
    
    else if (global.Trigger_Triggered[6] == 0 && global.triggered == 6)
    {    

        instance_create(272,496,obj_Zombie);
        instance_create(608,608,obj_Zombie); 
        global.Trigger_Triggered[6] = 1;
        global.triggered = 6;   
             
        with(107198)
        {
        instance_destroy();
        }
        
        //Alarm[3] = 30;   
    }

    
/////////////////////////////////////////Trigger inside mansion, 2nd room to the left of the main room/////////////////////////////////////////////

  • 0

#2 Splaff

Splaff

    GMC Member

  • GMC Member
  • 71 posts
  • Version:GM8

Posted 06 August 2012 - 09:12 PM

One of your issues is that you're using "else if" 6 times in series: you're wiring all your checks as interdependent, which is most likely what you aren't meaning to do.

Using a switch statement instead might help your code be a little cleaner and reliable.

Edited by Splaff, 06 August 2012 - 09:15 PM.

  • 0

#3 Astal

Astal

    GMC Member

  • GMC Member
  • 247 posts

Posted 06 August 2012 - 09:25 PM

One of your issues is that you're using "else if" 6 times in series: you're wiring all your checks as interdependent, which is most likely what you aren't meaning to do.

Using a switch statement instead might help your code be a little cleaner and reliable.


how would I use a switch with that code the same way as any other huh?

yeah i dunno how to use a switch with more than one variable

Edited by Astal, 06 August 2012 - 09:40 PM.

  • 0

#4 Wraithcutter

Wraithcutter

    GMC Member

  • New Member
  • 286 posts
  • Version:GM8

Posted 06 August 2012 - 09:49 PM

I agree with Spalff about the switch. It would make things easier on you anyway. If each of these areas is seperate room in your game, you can use room as your case, otherwise maybe location.

So something like...

switch (room)
     {
     case room_outside:
              {
              //do stuff
              }
      case room_inside1:
              {
              //do other stuff
              }
      }

If these aren't seperate rooms you're using, you could probably use X as the switch, and each objects x location as the cases.

*Edit: For using checking multiple variables - You can then use an if statement inside of your case's codes.

I do suggest that you use break(); at the end of your 'If' if you don't need it to continue checking for other stuff. Helps solve the problem of triggering other peices of code on accident.

Edited by Wraithcutter, 06 August 2012 - 09:56 PM.

  • 0

#5 Splaff

Splaff

    GMC Member

  • GMC Member
  • 71 posts
  • Version:GM8

Posted 06 August 2012 - 09:57 PM

how would I use a switch with that code the same way as any other huh?

yeah i dunno how to use a switch with more than one variable


Judging from your code, I would say to do something like this:

switch (global.triggered)
{
     case 0:
     {
          if global.Trigger_Triggered[0] == 0
          {
               global.Trigger_Triggered[0] = 1;
               //do your action
          }

          break;
     }

     case 1:
     {
          if global.Trigger_Triggered[1] == 0
          {
               global.Trigger_Triggered[1] = 1;
               //do your action
          }

          break;
     }

     case 2:
     {
          if global.Trigger_Triggered[2] == 0
          {
               global.Trigger_Triggered[2] = 1;
               //do your action
          }

          break;
     }

     case 3:
     {
          if global.Trigger_Triggered[3] == 0
          {
               global.Trigger_Triggered[3] = 1;
               //do your action
          }

          break;
     }

     case 4:
     {
          if global.Trigger_Triggered[4] == 0
          {
               global.Trigger_Triggered[4] = 1;
               //do your action
          }

          break;
     }

     case 5:
     {
          if global.Trigger_Triggered[5] == 0
          {
               global.Trigger_Triggered[5] = 1;
               //do your action
          }

          break;
     }

     case 6:
     {
          if global.Trigger_Triggered[6] == 0
          {
               global.Trigger_Triggered[6] = 1;
               //do your action
          }

          break;
     }
}

You don't need to use two variables: "global.Trigger_Triggered[x]" doesn't need to be part of the switch for it to work, it's just an extra check.

Edit: Got ninja'd from above! I wrote out what he was talking about, though, so it should be fine.

Edited by Splaff, 06 August 2012 - 09:58 PM.

  • 1

#6 Astal

Astal

    GMC Member

  • GMC Member
  • 247 posts

Posted 06 August 2012 - 10:21 PM


how would I use a switch with that code the same way as any other huh?

yeah i dunno how to use a switch with more than one variable


Judging from your code, I would say to do something like this:

switch (global.triggered)
{
     case 0:
     {
          if global.Trigger_Triggered[0] == 0
          {
               global.Trigger_Triggered[0] = 1;
               //do your action
          }

          break;
     }

     case 1:
     {
          if global.Trigger_Triggered[1] == 0
          {
               global.Trigger_Triggered[1] = 1;
               //do your action
          }

          break;
     }

     case 2:
     {
          if global.Trigger_Triggered[2] == 0
          {
               global.Trigger_Triggered[2] = 1;
               //do your action
          }

          break;
     }

     case 3:
     {
          if global.Trigger_Triggered[3] == 0
          {
               global.Trigger_Triggered[3] = 1;
               //do your action
          }

          break;
     }

     case 4:
     {
          if global.Trigger_Triggered[4] == 0
          {
               global.Trigger_Triggered[4] = 1;
               //do your action
          }

          break;
     }

     case 5:
     {
          if global.Trigger_Triggered[5] == 0
          {
               global.Trigger_Triggered[5] = 1;
               //do your action
          }

          break;
     }

     case 6:
     {
          if global.Trigger_Triggered[6] == 0
          {
               global.Trigger_Triggered[6] = 1;
               //do your action
          }

          break;
     }
}

You don't need to use two variables: "global.Trigger_Triggered[x]" doesn't need to be part of the switch for it to work, it's just an extra check.

Edit: Got ninja'd from above! I wrote out what he was talking about, though, so it should be fine.



k guys thank you im gonna try that


it works, thank you so much. i just have em in the wrong order in my script haha. ahh im gonna have to use the x of the object also

Edited by Astal, 06 August 2012 - 10:35 PM.

  • 0

#7 crashhelper

crashhelper

    GMC Member

  • GMC Member
  • 867 posts

Posted 06 August 2012 - 10:33 PM


if (global.Trigger_Triggered[global.triggered] == 0)

{

    switch (global.triggered)

    {

        // stuff

    }

}


  • 1

#8 Splaff

Splaff

    GMC Member

  • GMC Member
  • 71 posts
  • Version:GM8

Posted 06 August 2012 - 10:40 PM

if (global.Trigger_Triggered[global.triggered] == 0)
{
    switch (global.triggered)
    {
        // stuff
    }
}


Use this one instead, it's more optimized. Kudos.
  • 0

#9 Astal

Astal

    GMC Member

  • GMC Member
  • 247 posts

Posted 06 August 2012 - 10:52 PM


if (global.Trigger_Triggered[global.triggered] == 0)
{
    switch (global.triggered)
    {
        // stuff
    }
}


Use this one instead, it's more optimized. Kudos.



mine works it just removes the first object i run in to no matter which one it is in the code, so if i run in to one on the right it will destroy the left on if its in the code first, i cant figure out how to check for x correctly

I tried if(global.Trigger_Triggered[1] == 0 && obj_Trigger.x == 336)
  • 0

#10 Astal

Astal

    GMC Member

  • GMC Member
  • 247 posts

Posted 06 August 2012 - 11:27 PM



if (global.Trigger_Triggered[global.triggered] == 0)
{
    switch (global.triggered)
    {
        // stuff
    }
}


Use this one instead, it's more optimized. Kudos.



mine works it just removes the first object i run in to no matter which one it is in the code, so if i run in to one on the right it will destroy the left on if its in the code first, i cant figure out how to check for x correctly

I tried if(global.Trigger_Triggered[1] == 0 && obj_Trigger.x == 336)



yeah just doesnt work you know of an example to use the x and the y in an if
  • 0

#11 Astal

Astal

    GMC Member

  • GMC Member
  • 247 posts

Posted 07 August 2012 - 06:00 AM

Am i using instance position right? trying to make sure the object im touching is the right 1

switch (global.triggered)   
    {
          case 0: 
          {
          
            if(global.Trigger_Triggered[0] == 0)
            {
                Message("Hmmmm a cabin, I better go inside and get out of these woods these zombies freak me out!");
                global.Trigger_Triggered[0] = 1;
                with(106456)
                {
                    instance_destroy();
                }
            
            global.triggered = 1;
            
            }
            break;
          }
          
          case 1: 
          {
            if(global.Trigger_Triggered[1] == 0 && instance_position(336,192, obj_Trigger))
            {    
                instance_create(192,208,obj_Zombie); 
                instance_create(544,208,obj_Zombie);
                global.Trigger_Triggered[1] = 1;    
            
                with(107200)
                {
                        instance_destroy();                     
                }
                
            global.triggered =+ 1;    
            
            }            
            break;
          }
          
          case 2: 
          {
            if(global.Trigger_Triggered[2] == 0 && instance_position(384,464,obj_Trigger))
            {
                instance_create(880,176,obj_Zombie); 
                instance_create(1056,304,obj_Zombie); 
                global.Trigger_Triggered[2] = 1; 
                
                with(107202)
                {
                    instance_destroy();
                }
                
            global.triggered =+ 1;
                
            }            
            break;
          }
          
          case 3: 
          {
            if(global.Trigger_Triggered[3] == 0 && instance_position(1056,160,obj_Trigger))
            {
                instance_create(224,480,obj_Zombie); 
                instance_create(544,576,obj_Zombie);
                global.Trigger_Triggered[3] = 1;
                
                with(107205)
                {
                    instance_destroy();
                }
                
            global.triggered =+ 1;
          
            }            
            break;
          }
          
          case 4: 
          {
            if(global.Trigger_Triggered[4] == 0 && instance_position(865,560,obj_Trigger))
            {
                instance_create(640,544,obj_Zombie); 
                instance_create(1088,560,obj_Zombie); 
                global.Trigger_Triggered[4] = 1; 
                with(107203)
                {
                    instance_destroy();
                }

            global.triggered =+ 1;
                
            }            
            break;
          }

  • 0

#12 Astal

Astal

    GMC Member

  • GMC Member
  • 247 posts

Posted 07 August 2012 - 09:53 PM

here is the final working code if anyone else needs it

// Check if an instance/object is touching me
    var inst;
    inst = instance_place(x, y, obj_Trigger);
    // If it is then we can check its position

switch(room)
{
    case rm_Forest_7:
    {
           switch (inst.x)   
           {
                case 464: 
                {
          
                    if(global.Trigger_Triggered[0] == 0 && instance_exists(inst))
                    {
                        Message("Hmmmm a cabin, I better go inside and get out of these woods these zombies freak me out!");
                        global.Trigger_Triggered[0] = 1;
                        
                        with(106456)
                        {
                            instance_destroy();
                        }
                    }
                break;
                }
          }
    break;
    }
    
    case rm_Cabin_Main_to_Left:
    {
           switch (inst.x)   
           {
                case 336: 
                {
          
                    if(global.Trigger_Triggered[1] == 0 && instance_exists(inst))
                    {
                        instance_create(192,208,obj_Zombie); 
                        instance_create(544,208,obj_Zombie);
                        global.Trigger_Triggered[1] = 1; 
                        with(107200)
                        {
                            instance_destroy();
                        }
            
            
                    }
                break;
                }
          
                case 384: 
                {
                    if(global.Trigger_Triggered[2] == 0 && instance_exists(inst))
                    {
                        instance_create(224,480,obj_Zombie); 
                        instance_create(544,576,obj_Zombie);
                        global.Trigger_Triggered[2] = 1; 
                
                        with(107202)
                        {
                            instance_destroy();
                        }                             
                    }            
                break;
                }
          
                case 1056: 
                {
                    if(global.Trigger_Triggered[3] == 0 && instance_exists(inst))
                    {
                        instance_create(880,176,obj_Zombie); 
                        instance_create(1056,304,obj_Zombie); 
                        global.Trigger_Triggered[3] = 1;
                
                        with(107205)
                        {
                            instance_destroy();
                        }
          
                    }            
                break;
                }
          
                case 864: 
                {
                    if(global.Trigger_Triggered[4] == 0 && instance_exists(inst))
                    {
                        instance_create(640,544,obj_Zombie); 
                        instance_create(1088,560,obj_Zombie); 
                        global.Trigger_Triggered[4] = 1; 
                        
                        with(107203)
                        {
                            instance_destroy();
                        }
                    }            
                break;
                }   
        }
    break;
    }
    
    case rm_Cabin_Main_Room_Left_To_Left:
    {
           switch (inst.x)
           {
    
                case 432: 
                {
                    if(global.Trigger_Triggered[5] == 0 && instance_exists(inst))
                    {
                        instance_create(272,496,obj_Zombie); 
                        instance_create(608,608,obj_Zombie); 
                        global.Trigger_Triggered[5] = 1; 
                
                        with(107197)
                        {
                            instance_destroy();
                        }
                    }            
                    break;
                }
          
                case 944: 
                {
                    if(global.Trigger_Triggered[6] == 0 && instance_exists(inst))
                    {
                        instance_create(944,192,obj_Zombie);
                        instance_create(672,352,obj_Zombie); 
                        instance_create(944,544,obj_Zombie); 
                        global.Trigger_Triggered[6] = 1; 
                
                        with(107198)
                        {
                        instance_destroy();
                        }      
                    }            
                break;
                }      
        }
    break;
    }

}

  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users