problem with script - active inventory.

Started by marcusbugger, Thu 31/12/2009 01:27:26

Previous topic - Next topic

marcusbugger

Code: ags

function hHotspot19_UseInv()
{
   if (call == 0){
if (player.ActiveInventory == iYkey){
cJason.Walk(420, 333, eBlock, eWalkableAreas);
cJason.LockView(25);
cJason.Animate(0, 2, eOnce);
PlaySound(11);
Display("Unlocked.");
dialog[0].SetOptionState(4, eOptionOffForever);
dialog[7].SetOptionState(4, eOptionOffForever);
SetGlobalInt(min, 0);



}
}





if (call == 1){
if (player.ActiveInventory == iYkey)
{
cJason.Walk(420, 333, eBlock, eWalkableAreas);
cJason.LockView(25);
cJason.Animate(0, 2, eOnce);
PlaySound(11);
Display("Locked."); 
cJason.UnlockView();
SetGlobalInt(min, 1);

}}
if (call < 2) {
  call += 1; 
 }
  if (call == 2) {
    call -= 2;
    
 
  }
}

function hHotspot19_Interact()
{

if (call == 0){
cJason.Walk(420, 333, eBlock, eWalkableAreas);
cJason.LockView(25);
cJason.Animate(2, 2, eOnce);
PlaySound(1);
Display("It 's locked.");
dialog[0].SetOptionState(4, eOptionOn);
dialog[7].SetOptionState(4, eOptionOn);
cJason.UnlockView();
}


if (call == 1)
{
cJason.Walk(420, 333, eBlock, eWalkableAreas);
cJason.LockView(25);
cJason.Animate(0, 2, eOnce);
player.ChangeRoom(6, 0, 0);
PlaySound(0);

}

}


- It all works, but if I use another inventory item on hotspot 19, the game takes the command changeroom and goes to room 6, if  I use a cup, lamp wick or something else(because only key must work on door and not the other inventory items). Can anyone say what this is or what I did wrong.? I made a door you must unlock(you can lock it and unlock it) with inventory(yellow key) and then use the interaction(hand) to open door and go to room 6, but you can use any type of inventory now on hotpsot and you will find yourself in room 6 and that 's  not what I want.

monkey0506

#1
For everyone's sake...please learn to properly indent your code. This is an unreadable mess. Indentation lets us see at a glance at any point in the code exactly what function and/or if branch we may be in...

Note: I'm not trying to be rude here, but until I posted this and corrected the indentation, I honestly couldn't make sense of the code. Essentially I would have had to mentally perform the same task...so it's just easier to visualize when the code is indented.

Also you can use the [code] and [/code] tags to encapsulate your code, like this:

Code: ags
function hHotspot19_UseInv()
{
  if (call == 0){ // where is CALL being defined at anyway...?
    if (player.ActiveInventory == iYkey){
      cJason.Walk(420, 333, eBlock, eWalkableAreas);
      cJason.LockView(25);
      cJason.Animate(0, 2, eOnce);
      PlaySound(11);
      Display("Unlocked.");
      dialog[0].SetOptionState(4, eOptionOffForever);
      dialog[7].SetOptionState(4, eOptionOffForever);
      SetGlobalInt(min, 0);
      cJason.ChangeRoom(6, 0, 0);
    }
  }
  if (call == 1){
    if (player.ActiveInventory == iYkey)
    {
      cJason.Walk(420, 333, eBlock, eWalkableAreas);
      cJason.LockView(25);
      cJason.Animate(0, 2, eOnce);
      PlaySound(11);
      Display("Locked.");
      cJason.UnlockView();
      SetGlobalInt(min, 1);
    }
  }
  if (call < 2) { // if call is 0 or 1
    call += 1;
  }
  if (call == 2) { // note that if call was 1 this will change it back to 0 since there is no else, call will never be left at 2
    // if this was intentional, consider just using "if (call == 0) call = 1;" and vice versa instead...
    call -= 2;
  }
}

function hHotspot19_Interact()
{
  if (call == 0){
    cJason.Walk(420, 333, eBlock, eWalkableAreas);
    cJason.LockView(25);
    cJason.Animate(2, 2, eOnce);
    PlaySound(1);
    Display("It 's locked.");
    dialog[0].SetOptionState(4, eOptionOn);
    dialog[7].SetOptionState(4, eOptionOn);
    cJason.UnlockView();
  }
  if (call == 1)
  {
    cJason.Walk(420, 333, eBlock, eWalkableAreas);
    cJason.LockView(25);
    cJason.Animate(0, 2, eOnce);
    player.ChangeRoom(6, 0, 0);
    PlaySound(0);
  }
}


There...that's better. Now with the preview function let me actually look at your code.

Okay, I don't see how using an inventory item other than iYkey would produce this call to ChangeRoom based on the code given. However, I just wanted to point out that your logic is not sound at all here. You're using the variable call (which we never see (or get any indication) where that is defined at...) as a flag to indicate whether the door is locked or unlocked. Yet you're toggling that flag every time ANY inventory item is used on the door. So if I were to use iSausage on the door and then eModeInteract with the door, I'd be let through as though I'd used the key on it.

Based on the code you've given though, I don't see how using the sausage on the door would let me go through without an additional interaction. That makes me suspicious that either your functions are improperly linked in the editor or you have some bad logic going on in your on_mouse_click function as well. Would you mind posting your on_mouse_click from your global script? Because it appears to me that at some point you're calling ProcessClick with eModeInteract where you should be using eModeUseInv instead...but I can't tell that without seeing the function.

marcusbugger

#2
Code: ags

// Automatically converted interaction variables
int IntVar_Global_1 = 0;
export IntVar_Global_1;
int brick;
int small;
int seck;
int dor;
int second = 5;
int dorp;
int min;
// main global script file

 
#sectionstart game_start  // DO NOT EDIT OR REMOVE THIS LINE
function game_start() {
  // called when the game starts, before the first room is loaded
}
#sectionend game_start  // DO NOT EDIT OR REMOVE THIS LINE


#sectionstart repeatedly_execute  // DO NOT EDIT OR REMOVE THIS LINE
function repeatedly_execute() {
  // put anything you want to happen every game cycle here
}
#sectionend repeatedly_execute  // DO NOT EDIT OR REMOVE THIS LINE


function show_inventory_window () {
  // This demonstrates both types of inventory window - the first part is how to
  // show the built-in inventory window, the second part uses the custom one.
  // Un-comment one section or the other below.
  
  // ** DEFAULT INVENTORY WINDOW
//  InventoryScreen();

  // ** CUSTOM INVENTORY WINDOW
  gInventory.Visible = true;
  // switch to the Use cursor (to select items with)
  mouse.Mode = eModeInteract;
  // But, override the appearance to look like the arrow
  mouse.UseModeGraphic(eModePointer);

}

#sectionstart on_key_press  // DO NOT EDIT OR REMOVE THIS LINE
function on_key_press(int keycode) {
  // called when a key is pressed. keycode holds the key's ASCII code
  if (IsGamePaused() == 1) keycode=0;  // game paused, so don't react to keypresses
  if (keycode==17)  QuitGame(1);   // Ctrl-Q
  if (keycode==363) SaveGameDialog();   // F5
  if (keycode==365) RestoreGameDialog();  // F7
  if (keycode==367) RestartGame();  // F9
  if (keycode==434) SaveScreenShot("scrnshot.bmp");  // F12
  if (keycode==9)   show_inventory_window();  // Tab, show inventory

  if (keycode==19)  Debug(0,0);  // Ctrl-S, give all inventory
  if (keycode==22)  Debug(1,0);  // Ctrl-V, version
  if (keycode==1)   Debug(2,0);  // Ctrl-A, show walkable areas
  if (keycode==24)  Debug(3,0);  // Ctrl-X, teleport to room
}
#sectionend on_key_press  // DO NOT EDIT OR REMOVE THIS LINE


#sectionstart on_mouse_click  // DO NOT EDIT OR REMOVE THIS LINE
function on_mouse_click(MouseButton button) {
  // called when a mouse button is clicked. button is either LEFT or RIGHT
  if (IsGamePaused() == 1) {
    // Game is paused, so do nothing (ie. don't allow mouse click)
  }
  else if (button == eMouseLeft) {
    ProcessClick(mouse.x, mouse.y, mouse.Mode );
  }
  else {   // right-click, so cycle cursor
    mouse.SelectNextMode();
  }
}
#sectionend on_mouse_click  // DO NOT EDIT OR REMOVE THIS LINE


#sectionstart interface_click  // DO NOT EDIT OR REMOVE THIS LINE
function interface_click(int interface, int button) {
  // This function is obsolete, from 2.62 and earlier versions.
}
#sectionend interface_click  // DO NOT EDIT OR REMOVE THIS LINE


#sectionstart btnInvUp_Click  // DO NOT EDIT OR REMOVE THIS LINE
function btnInvUp_Click(GUIControl *control, MouseButton button) {
  InventoryWindow1.ScrollUp();
}
#sectionend btnInvUp_Click  // DO NOT EDIT OR REMOVE THIS LINE

#sectionstart btnInvDown_Click  // DO NOT EDIT OR REMOVE THIS LINE
function btnInvDown_Click(GUIControl *control, MouseButton button) {
  InventoryWindow1.ScrollDown();
}
#sectionend btnInvDown_Click  // DO NOT EDIT OR REMOVE THIS LINE

#sectionstart btnInvOK_Click  // DO NOT EDIT OR REMOVE THIS LINE
function btnInvOK_Click(GUIControl *control, MouseButton button) {
  
	// They pressed the OK button, close the GUI
	gInventory.Visible = false;
	mouse.UseDefaultGraphic();
}
#sectionend btnInvOK_Click  // DO NOT EDIT OR REMOVE THIS LINE

#sectionstart btnInvSelect_Click  // DO NOT EDIT OR REMOVE THIS LINE
function btnInvSelect_Click(GUIControl *control, MouseButton button) {
  
	// They pressed SELECT, so switch to the Get cursor
	mouse.Mode = eModeInteract;
	// But, override the appearance to look like the arrow
	mouse.UseModeGraphic(eModePointer);
}
#sectionend btnInvSelect_Click  // DO NOT EDIT OR REMOVE THIS LINE

#sectionstart btnIconInv_Click  // DO NOT EDIT OR REMOVE THIS LINE
function btnIconInv_Click(GUIControl *control, MouseButton button) {
  
  show_inventory_window();
}
#sectionend btnIconInv_Click  // DO NOT EDIT OR REMOVE THIS LINE

#sectionstart btnIconCurInv_Click  // DO NOT EDIT OR REMOVE THIS LINE
function btnIconCurInv_Click(GUIControl *control, MouseButton button) {
  
  if (player.ActiveInventory != null)
    mouse.Mode = eModeUseinv;
}
#sectionend btnIconCurInv_Click  // DO NOT EDIT OR REMOVE THIS LINE

#sectionstart btnIconSave_Click  // DO NOT EDIT OR REMOVE THIS LINE
function btnIconSave_Click(GUIControl *control, MouseButton button) {
  
  SaveGameDialog();
}
#sectionend btnIconSave_Click  // DO NOT EDIT OR REMOVE THIS LINE

#sectionstart btnIconLoad_Click  // DO NOT EDIT OR REMOVE THIS LINE
function btnIconLoad_Click(GUIControl *control, MouseButton button) {
  
  RestoreGameDialog();
}
#sectionend btnIconLoad_Click  // DO NOT EDIT OR REMOVE THIS LINE

#sectionstart btnIconExit_Click  // DO NOT EDIT OR REMOVE THIS LINE
function btnIconExit_Click(GUIControl *control, MouseButton button) {
  
  QuitGame(1);
}
#sectionend btnIconExit_Click  // DO NOT EDIT OR REMOVE THIS LINE

#sectionstart btnIconAbout_Click  // DO NOT EDIT OR REMOVE THIS LINE
function btnIconAbout_Click(GUIControl *control, MouseButton button) {
  
  Display("Adventure Game Studio v2 run-time engine[[Copyright (c) 1999-2005 Chris Jones");
}
#sectionend btnIconAbout_Click  // DO NOT EDIT OR REMOVE THIS LINE


- This is what stands in my global script. A happy new year monkey. If I use inventory items on hotspots to go through a door nothing happens, and I must use the hand(interaction code).
It has to do with a puzzle I made to return a key only when you lock it, you get from another character some stuff(item).



ZH

Quote from: marcusbuggerSorry, I don 't know how 2 use the /code function what you mean.

“[code]
// Automatically converted interaction variables
int IntVar_Global_1 = 0;
export IntVar_Global_1;
int brick;
int small;
int seck;
int dor;
int second = 5;
int dorp;
int min;
// main global script file
[/code]”

is rendered as:

Code: ags

// Automatically converted interaction variables
int IntVar_Global_1 = 0;
export IntVar_Global_1;
int brick;
int small;
int seck;
int dor;
int second = 5;
int dorp;
int min;
// main global script file

marcusbugger

#4
"Would you mind posting your on_mouse_click from your global script? Because it appears to me that at some point you're calling ProcessClick with eModeInteract where you should be using eModeUseInv instead...but I can't tell that without seeing the function."

I have paste the global script code here, Have you checked it already monkey or pumaman?
This is a reminder, thank you and have a nice 2010. It 's some topics above this one.

monkey0506

Sorry I didn't respond sooner Marcus (and Happy New Year to you too). I did read this but got caught up in other things and forgot... ::)

Based on your code it seems that the inventory clicks are being handled automatically (unless you have some other script lying around somewhere that's got an on_mouse_click in it as well).

Quote from: marcusbugger on Fri 01/01/2010 00:49:14- This is what stands in my global script. A happy new year monkey. If I use inventory items on hotspots to go through a door nothing happens, and I must use the hand(interaction code).
It has to do with a puzzle I made to return a key only when you lock it, you get from another character some stuff(item).

Based on what you're saying here, if you use an inventory item on the door, "nothing happens" but if you "use the hand" to interact with the door (eModeInteract) then you're allowed through even if you didn't use the key yet. Is that correct?

Because if I'm understanding you correctly then it's the exact problem I described in my first post:

Quote from: monkey_05_06 on Thu 31/12/2009 01:47:45However, I just wanted to point out that your logic is not sound at all here. You're using the variable call (which we never see (or get any indication) where that is defined at...) as a flag to indicate whether the door is locked or unlocked. Yet you're toggling that flag every time ANY inventory item is used on the door. So if I were to use iSausage on the door and then eModeInteract with the door, I'd be let through as though I'd used the key on it.

The way you're using the call variable means that using any inventory item on the door will toggle whether it's locked or unlocked. Regardless of whether it's actually your problem (as I'm still not clear what you're saying the problem is), try replacing your hHotspot19_UseInv function with this:

Code: ags
function hHotspot19_UseInv() {
  if (call == 0) { // if the door is LOCKED
    if (player.ActiveInventory == iYkey) { // if the player used the key, UNLOCK the door
      cJason.Walk(420, 333, eBlock, eWalkableAreas);
      cJason.LockView(25);
      cJason.Animate(0, 2, eOnce);
      PlaySound(11);
      Display("Unlocked.");
      dialog[0].SetOptionState(4, eOptionOffForever);
      dialog[7].SetOptionState(4, eOptionOffForever);
      SetGlobalInt(min, 0);
      cJason.ChangeRoom(6, 0, 0);
      call = 1; // set the flag to show the door as UNLOCKED
    }
  }
  if (call == 1) { // if the door is UNLOCKED
    if (player.ActiveInventory == iYkey) { // if the player used the key, LOCK the door
      cJason.Walk(420, 333, eBlock, eWalkableAreas);
      cJason.LockView(25);
      cJason.Animate(0, 2, eOnce);
      PlaySound(11);
      Display("Locked.");
      cJason.UnlockView();
      SetGlobalInt(min, 1);
      call = 0; // set the flag to show the door as LOCKED
    }
  }
}


See if that fixes your problem. All I've done here is corrected the logic so that the CALL variable only gets changed if the iYkey inventory item gets used on the door as opposed to any inventory item.

marcusbugger

#6
If I use your code, I see - "unlocked" - (you know what I mean) and then I click I get that other message - "Locked"- (call = 1) and then I changeroom, also not what I want? Other inventory doesn 't work anymore that 's fixed in your code.

Can I sent my game or a certain file to you, so you can check. Sorry if I didn 't flag my problem earlier, if I have a problem I will do that, but I must say that I didn 't understand what you ment with flag and now I do.

monkey0506

If you're using the code that I posted then if call is set to 1 then the door will be unlocked and you should change rooms. Otherwise the door is locked and ChangeRoom never gets called.

If you want to PM me a link to download your game and see for myself I wouldn't mind taking a look.

marcusbugger

Quote from: monkey_05_06 on Thu 14/01/2010 22:14:10
If you're using the code that I posted then if call is set to 1 then the door will be unlocked and you should change rooms. Otherwise the door is locked and ChangeRoom never gets called.

If you want to PM me a link to download your game and see for myself I wouldn't mind taking a look.


Link sent to your e-mail, hope you get.

marcusbugger

Reminder: If you found my game monkey or someone else of the ags devellopers and helpers?(Pumaman, monker or..). Monkey I 've sent you an email 2 download my game from internet, do you have or got it??

Greetings marcus.

SMF spam blocked by CleanTalk