Variable checking problem

Started by Duckbutcher, Wed 01/02/2017 15:16:12

Previous topic - Next topic

Duckbutcher

Hey all hope you're well.

I've done a bit of script for a hotspot. Actually it`s some cooker controls and I want them to turn off and on a hotplate. I want the computer to check a variable (cookervar) to tell whether to turn the hotplate on and off. When cookervar is at 0, the cooker will turn on on when the "use" command is used, and the cold hotplate hotspot will turn off and be replaced by an object representing the hot turned-on hotplate. This part of the script also changes the cookervar to 1 so that the next time the player uses "use", the hotplate object will be removed and the cold hotplate hotspot will come back on.

I fear I'm missing something obvious but I can' find what it is. The hotplate object switches on fine but when I turn it off it disappears for a second and then comes back on. Here's my script, please put me out of my misery.

Code: ags

function hocont_AnyClick()
{
  
// LOOK AT
    if(UsedAction(eGA_LookAt)) {
      player.Say("Stupid electric cooker.");
    }
    // USE
    else if(UsedAction(eGA_Use)) {
      if (cookervar == 1)
      cEgo.Walk(96, 132, eBlock);
      cEgo.FaceDirection(eDirectionUp);
      Wait(10);
      hhplat.Enabled=true;
      oHot.Visible=false;
      cookervar = 0;  
    }
    
      if (cookervar == 0){
      cEgo.Walk(96, 132, eBlock);
      cEgo.FaceDirection(eDirectionUp);
      Wait(10);
      hhplat.Enabled=false;
      oHot.Visible=true;
      cookervar = 1;
    }
    

    // OPEN
      else if(UsedAction(eGA_Open)) {
      player.Say("It doesn't move.");
          }
    // Pull
    else if(UsedAction(eGA_Pull)) {
      Unhandled();
    }  
    // PICKUP
    else if(UsedAction(eGA_PickUp)) {
      Unhandled();
          }
    //USE INV
    else if(UsedAction(eGA_UseInv)) {
      Unhandled();
    }
    
    else Unhandled();
}

Danvzare

I could be wrong, since I just gave this a quick glance, but I'm fairly sure you're missing an else just before "if (cookervar == 0)"
so it should look like this:

Code: ags

function hocont_AnyClick()
{
  
// LOOK AT
    if(UsedAction(eGA_LookAt)) {
      player.Say("Stupid electric cooker.");
    }
    // USE
    else if(UsedAction(eGA_Use)) {
      if (cookervar == 1)
      cEgo.Walk(96, 132, eBlock);
      cEgo.FaceDirection(eDirectionUp);
      Wait(10);
      hhplat.Enabled=true;
      oHot.Visible=false;
      cookervar = 0;  
    }
    
      else if (cookervar == 0){
      cEgo.Walk(96, 132, eBlock);
      cEgo.FaceDirection(eDirectionUp);
      Wait(10);
      hhplat.Enabled=false;
      oHot.Visible=true;
      cookervar = 1;
    }
    

    // OPEN
      else if(UsedAction(eGA_Open)) {
      player.Say("It doesn't move.");
          }
    // Pull
    else if(UsedAction(eGA_Pull)) {
      Unhandled();
    }  
    // PICKUP
    else if(UsedAction(eGA_PickUp)) {
      Unhandled();
          }
    //USE INV
    else if(UsedAction(eGA_UseInv)) {
      Unhandled();
    }
    
    else Unhandled();
}

Duckbutcher

#2
Tried that, and now the action just doesn't happen at all. The command highlights in the action bar but the character doesn't move and nothing else happens!

I think it's getting itself into a loop where I change the variable at the end of each section, and it's just going ahead and reading the next bit after the variable has been changed. I also tried putting in a return; command to try and get it to stop after the first action, but that didn't work either.

Danvzare

Alright, let's take a closer look at this code then.

Code: ags

function hocont_AnyClick()
{
  
// LOOK AT
    if(UsedAction(eGA_LookAt)) {
      player.Say("Stupid electric cooker.");
    }
    // USE
    else if(UsedAction(eGA_Use)) {
      if (cookervar == 1) {
      cEgo.Walk(96, 132, eBlock);
      cEgo.FaceDirection(eDirectionUp);
      Wait(10);
      hhplat.Enabled=true;
      oHot.Visible=false;
      cookervar = 0;  
      }
      else if (cookervar == 0) {
      cEgo.Walk(96, 132, eBlock);
      cEgo.FaceDirection(eDirectionUp);
      Wait(10);
      hhplat.Enabled=false;
      oHot.Visible=true;
      cookervar = 1;
      }
    }
    

    // OPEN
      else if(UsedAction(eGA_Open)) {
      player.Say("It doesn't move.");
          }
    // Pull
    else if(UsedAction(eGA_Pull)) {
      Unhandled();
    }  
    // PICKUP
    else if(UsedAction(eGA_PickUp)) {
      Unhandled();
          }
    //USE INV
    else if(UsedAction(eGA_UseInv)) {
      Unhandled();
    }
    
    else Unhandled();
}


Alright try that.

Duckbutcher

Haha that one missing {, typical! Thanks! Solved.

Khris

#5
Use a bool instead of an int, call it "cooker_on", initial value "false", then use:
Code: ags
  else if (UsedAction(eGA_Use)) {
    cEgo.Walk(96, 132, eBlock);
    cEgo.FaceDirection(eDirectionUp);
    Wait(10);
    cooker_on = !cooker_on; // switch state
    hhplat.Enabled = !cooker_on;
    oHot.Visible = cooker_on;
  }


Even with the code you have now you can make it much shorter by moving the first three commands out of the conditional.

SMF spam blocked by CleanTalk