Automated object usage script

Started by Peegee, Fri 19/01/2024 17:57:14

Previous topic - Next topic

Peegee

Hello, I would like to know if it would be possible to program this kind of thing (without it being too complex):

When a character uses an inventory object on an area (object in the room, hotspot or character), and there is nothing planned for that, he launches an animation, on the spot, of use of the object, like he takes the object out of his pocket and puts it back.
I tried this code, in "function unhandled_event(int what, int type)", in GlobalScript.

Code: ags
   if ((what == 1 && type == 3)||(what == 2 && type == 3)||(what == 3 && type == 3)||(what == 4 && type == 3)||(what == 5 && type == 3))
{
if (cGob1.ActiveInventory == iLezard)
{  
 cGob1.FaceDirection(eDirectionDown);
cGob1.ChangeView (122);
cGob1.Animate (16, 3,eOnce, eBlock);
cGob1.ChangeView (54);
cGob1.Animate (4, 3, eOnce, eBlock);
cGob1.ChangeView (122);
cGob1.Animate (16, 3,eOnce, eBlock);
cGob1.ChangeView (128);
cGob1.FaceDirection(eDirectionDown);
}
}

It works more or less, but it does not do the action when an object use (even another object) is programmed on the area. It would be too complicated to add variables for each area.

Thank you in advance.

Khris

Generic behavior can be added to the global on_mouse_click:

Code: ags
  // inside on_mouse_click
  if (mouse.Mode == eModeUseinv) {
    int x = Mouse.x, y = Mouse.y;
    PlayInteractAnimation(player.ActiveInventory); // call custom function
    Room.ProcessClick(x, y, eModeUseinv);
  }

This code will not work as-is, it requires a function to actually animate the character further up in the script, like this:

Code: ags
function PlayInteractAnimation(InventoryItem* item) {
  player.FaceDirection(eDirectionDown);
  player.ChangeView(...);
  // etc..
}

Peegee

Hello. Sorry, I only see your message now. With a guy from the French forum, we finally found a solution, by creating a function:

import function JSP(InventoryItem* iObjet); in the EditHeader

I will see with him if your solution can be simpler. If you are interested, I can explain more precisely how we did it. Thank you PG

Khris

Importing the function in the header is only necessary if you want to be able to call it from a room script. Which suggests you're putting this function call into every single interaction.

My approach will call the function whenever you click somewhere with an active inventory item, then run the actual interaction, if one exists, which means you don't have to put the same line a dozen times into each room script.

Also note that the (largely unrelated) code from your first post can be shortened considerably by simply doing
Code: ags
  if (type == 3) {
    // ...
  }
There's no need to test the value of what, and even if there were, you could do what >= 1 && what <= 5 instead.

SMF spam blocked by CleanTalk