How to Grab and Use Object with Cursor [SOLVED]

Started by Skeller53, Sun 09/01/2005 04:50:16

Previous topic - Next topic

Skeller53

I've searched the manual and the forums and maybe I've missed this, but I'd like to be able to pick up an object (say, with a cursor that looks like a hand) and use it on a character or another object. For example, I have an object that is a hammer. I want to physically grab it with the cursor hand and move it to the character, then initiate an animation that hits the character over the head, or on the foot, etc. Is there a way to do this?

By the way, AGS is wonderful! Thanks to the creator and all the contributors in the forum.

Candle

You could make a icon with the hand holding the hammer and change it to that when you pick it up the hammer  .

..

Do you want the player to like only have the hammer when they are clicking down, and to let go when they let go of the mouse? because that would be a bit more complex, and require a bit more heavy scripting that I can't be bothered to do this early in the morning.

If that's what you need i'll have a go later :P

Ashen

I know what you're thinking ... Don't think that.

Skeler53

That link was useful, Ashen, if only because it gets my brain working in different directions. Here is what I want to do.

The player clicks on the hammer, the hammer can then be dragged anywhere on the screen. The player lets go of the hammer and it stays wherever it was placed. Upon another click (or even a key press) the hammer becomes animated into a swinging motion. If collision or overlapping with a character (or even another object) is detected, the swinging animation is halted, the sound of a thud is heard and a different animation (e.g. the character grabbing his head or little birds flying in a circle over his head) is initiated. The hammer should still be where it was left, where it can be used again, if necessary, or dragged back to its original location.

Am I in way over my head on this one? Thanks for any thoughts.

..

No, that seems perfectly acomplishable.  Youd have to do a bit of experimenting getting the animation to stop exactly right and the 'flying stars' or whatever to go at the same time.

Scorpiorus

#6
Here is a room script demonstrating how you can drag room objects:

at the top of the room script:

// room script file

// holds an object number that's under the cursor
// but only if the mouse button is released. (or -1)

int objectToGrab = -1;

// the object number that's currently being dragged (or -1)
int objectGrabbed = -1;

// mouse button to operate an object with
int mouseButton = LEFT;

room's repeatedly execute:

Ã,  Ã,  if (objectGrabbed == -1)
Ã,  Ã,  {
Ã,  Ã,  Ã,  Ã,  if (IsButtonDown(mouseButton))
Ã,  Ã,  Ã,  Ã,  {
Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  if (GetCursorMode() == MODE_USE)
Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  {
Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  if (objectToGrab == OBJECT_HAMMER)
Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  {
Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  objectGrabbed = objectToGrab;
Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  objectToGrab = -1;
Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  }
Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  }
Ã,  Ã,  Ã,  Ã,  }
Ã,  Ã,  Ã,  Ã,  else
Ã,  Ã,  Ã,  Ã,  {
Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  objectToGrab = GetObjectAt(mouse.x, mouse.y);
Ã,  Ã,  Ã,  Ã,  }
Ã,  Ã,  }
Ã,  Ã,  else
Ã,  Ã,  {
Ã,  Ã,  Ã,  Ã,  if (IsButtonDown(mouseButton))
Ã,  Ã,  Ã,  Ã,  {
Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  int newX = GetViewportX() + mouse.x;
Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  int newY = GetViewportY() + mouse.y;
Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  SetObjectPosition(objectGrabbed, newX, newY);
Ã,  Ã,  Ã,  Ã,  }
Ã,  Ã,  Ã,  Ã,  else
Ã,  Ã,  Ã,  Ã,  {
Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  SetObjectGraphic(objectGrabbed, HAMMER_SPRITE);
Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  objectGrabbed = -1;
Ã,  Ã,  Ã,  Ã,  }
Ã,  Ã,  Ã,  Ã, 
Ã,  Ã,  }

So, when the player's dragging the hammer the objectGrabbed variable holds hammer's object number, otherwise it holds -1. You can use that var to check for various things like collision detection and so on.

Example:

room's repeatedly execute:

if (objectGrabbed != -1)
{
    if (AreCharObjColliding(EGO, objectGrabbed) && IsObjectAnimating(objectGrabbed))
    {
Ã,  Ã,      Display("EGO's hit!");
    }
}

To make that object animating you can have in room's on_key_press function:

function on_key_press(int key) {

Ã,  Ã,  if ((key == 32) && (objectGrabbed != -1)) // if space is pressed
Ã,  Ã,  {
Ã,  Ã,  Ã,  Ã,  if (IsObjectAnimating(objectGrabbed)==0) AnimateObject(objectGrabbed, SWINGING_HAMMER..., ..., repeat=1);
Ã,  Ã,  }
}

Skeller53

#7
Thank you, Scorpiorus. I will try that and let you know how it works out.

Just a couple of basic and I hope not too silly questions. Where it says OBJECT_HAMMER, is the object number expected? I'm trying to figure out if this code will work for all objects in the room, or if separate code is needed for each object.Ã,  I have the same question about HAMMER_SPRITE. Is a specific sprite number expected here?

Thanks a bunch. I'm going to try the code out later tonight and report back.

TerranRich

I'd say yes to both questions, Skeller53, if only because I don't see those variable names set anywhere in that script. Just to be on the safe side, replace those capitalized variable names with actual numbers. :)
Status: Trying to come up with some ideas...

Scorpiorus

Yeah, OBJECT_HAMMER is the object number, HAMMER_SPRITE is original object sprite slot and SWINGING_HAMMER is the swinging animation loop number. By the way, I forgot to set object's view, so you also need to SetObjectView before starting animation.

Quote from: Skeller53 on Wed 12/01/2005 16:29:53I'm trying to figure out if this code will work for all objects in the room, or if separate code is needed for each object.
Yep, the repeatedly executed part of the script is rather generic but I added some specific code to adjust it for the purpose in question.

That's how a more generic code would look like:

at the very top of room script:

// room script file

// holds an object number that's under the cursor
// but only if the mouse button is released. (or -1):
// (***used internally!***)

int objectToGrab = -1;

// the object number that's currently being dragged (or -1).
// Use this variable to find out what object is being dragged:
// (***read-only***)

int objectGrabbed = -1;

// object's sprite slot before it's grabbed:
// useful if you want to set object's graphic back
// when the object is dropped:

int objectOrginalSprite = -1;

// mouse button to grab an object with:

int mouseButton = LEFT;

// grabbing is allowed for this cursor mode:

int grabbingCursorMode = MODE_USE;



// This event occurs when we are about to grab an object (number 'objectNum').
// You can decide here whether it's allowed to grab the object.
// You must return 1 if the object can be dragged.
// Otherwise, return 0.

function on_object_grabbed(int objectNum) {
Ã, 
Ã,  Ã,  if (objectNum == OBJECT_HAMMER)Ã,  return 1; // grab hammer
Ã,  Ã,  if (objectNum == OBJECT_BLUECUP) return 1; // grab blue cup
Ã,  Ã, 
Ã,  Ã,  return 0;Ã, 
}


// This event occurs when we are about to drop an object (number 'objectNum').
// You can, for example, stop animation here or play falling animation
// (see MoveObject command), etc.

function on_object_dropped(int objectNum) {
Ã, 
Ã, 
Ã,  Ã,  // the following code stops the hammer object's animating
Ã,  Ã,  // and set its sprite back to normal:

Ã, 
Ã,  Ã,  if (objectNum == OBJECT_HAMMER)
Ã,  Ã,  {
Ã,  Ã,  Ã,  Ã,  if (IsObjectAnimating(OBJECT_HAMMER))
Ã,  Ã,  Ã,  Ã,  {
Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  SetObjectGraphic(objectNum, objectOrginalSprite);
Ã,  Ã,  Ã,  Ã,  }
Ã,  Ã,  }

Ã,  Ã, 
}

// Calling this function will drop the currently dragged object.
// If there is no one, nothing happens.

function DropObject() {
Ã, 
Ã,  Ã,  if (objectGrabbed != -1)
Ã,  Ã,  {
Ã,  Ã,  Ã,  Ã,  on_object_dropped(objectGrabbed);
Ã,  Ã,  Ã,  Ã,  objectOrginalSprite = -1;
Ã,  Ã,  Ã,  Ã,  objectGrabbed = -1;
Ã,  Ã,  }
}


// Main part of the grabbing object script.
// *** Must be called within repeatedly execute ***

function GrabObject_REP_EXEC() {
Ã, 
Ã,  Ã,  if (objectGrabbed == -1)
Ã,  Ã,  {
Ã,  Ã,  Ã,  Ã,  if (IsButtonDown(mouseButton))
Ã,  Ã,  Ã,  Ã,  {
Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  if (GetCursorMode() == grabbingCursorMode)
Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  {
Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  if (on_object_grabbed(objectToGrab) == 1)
Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  {
Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  objectOrginalSprite = GetObjectGraphic(objectToGrab);
Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  objectGrabbed = objectToGrab;
Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  objectToGrab = -1;
Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  }
Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  }
Ã,  Ã,  Ã,  Ã,  }
Ã,  Ã,  Ã,  Ã,  else
Ã,  Ã,  Ã,  Ã,  {
Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  objectToGrab = GetObjectAt(mouse.x, mouse.y);
Ã,  Ã,  Ã,  Ã,  }
Ã,  Ã,  }
Ã,  Ã,  else
Ã,  Ã,  {
Ã,  Ã,  Ã,  Ã,  if (IsButtonDown(mouseButton))
Ã,  Ã,  Ã,  Ã,  {
Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  int object_sprite = GetObjectGraphic(objectGrabbed);
Ã,  Ã,  Ã,  Ã, 
Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  int sprite_widthÃ,  = GetGameParameter(GP_SPRITEWIDTH,Ã,  object_sprite, 0, 0);
Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  int sprite_height = GetGameParameter(GP_SPRITEHEIGHT, object_sprite, 0, 0);
Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã, 
Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  int newX = GetViewportX() + (mouse.x - sprite_width / 2);
Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  int newY = GetViewportY() + (mouse.y + sprite_height / 2);
Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã, 
Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  SetObjectPosition(objectGrabbed, newX, newY);
Ã,  Ã,  Ã,  Ã,  }
Ã,  Ã,  Ã,  Ã,  else
Ã,  Ã,  Ã,  Ã,  {
Ã,  Ã,  Ã,  Ã,  Ã,  Ã, DropObject();
Ã,  Ã,  Ã,  Ã,  }
Ã,  Ã,  Ã,  Ã, 
Ã,  Ã,  }
Ã, 
}


function on_key_press(int key) {
Ã, 
Ã,  Ã,  // the following code starts object animation if
Ã,  Ã,  // 'space' key is pressed and provided:
Ã,  Ã,  //Ã,  1. grabbed object is the hammer;
Ã,  Ã,  //Ã,  2. object isn't animating.


Ã,  Ã,  if (key == 32)
Ã,  Ã,  {
Ã,  Ã,  Ã,  Ã,  if (objectGrabbed == OBJECT_HAMMER)
Ã,  Ã,  Ã,  Ã,  {
Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  if (IsObjectAnimating(OBJECT_HAMMER)==0)
Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  {
Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  SetObjectView(OBJECT_HAMMER, 1);
Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  AnimateObject(OBJECT_HAMMER, xxx, xxx, 1);
Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  }
Ã,  Ã,  Ã,  Ã,  }
Ã,  Ã,  }

}


room repeatedly execute:
Ã, 
Ã,  Ã,  GrabObject_REP_EXEC(); // <-- must be within repeatedly execute;



There are two event handling functions: on_object_grabbed and on_object_dropped. The functions are to write object-specific code within them.

There is also a DropObject() function. That's useful when you want force the player drop an object. For instance, you should do it if the player character leaves the current room, as objects can't be taken off from their rooms.

Skeller53


I'm not getting any error messages, but I can't grab anything either. Here I'm trying to grab a hammer, and I've also identified another object (whirlblade). I know this is something basic that I have or haven't done. Here is the script for my room:


// room script file

// holds an object number that's under the cursor
// but only if the mouse button is released. (or -1):
// (***used internally!***)
int objectToGrab = -1;

// the object number that's currently being dragged (or -1).
// Use this variable to find out what object is being dragged:
// (***read-only***)
int objectGrabbed = -1;

// object's sprite slot before it's grabbed:
// useful if you want to set object's graphic back
// when the object is dropped:
int objectOrginalSprite = -1;

// mouse button to grab an object with:
int mouseButton = LEFT;

// grabbing is allowed for this cursor mode:
int grabbingCursorMode = MODE_USE;

// This event occurs when we are about to grab an object (number 'objectNum').
// You can decide here whether it's allowed to grab the object.
// You must return 1 if the object can be dragged.
// Otherwise, return 0.
function on_object_grabbed(int objectNum) {
 
    if (objectNum == 4)  return 1; // grab hammer
    if (objectNum == 6) return 1; // grab whirlblade
   
    return 0; 
}

// This event occurs when we are about to drop an object (number 'objectNum').
// You can, for example, stop animation here or play falling animation
// (see MoveObject command), etc.
function on_object_dropped(int objectNum) {
 
 
    // the following code stops the hammer object's animating
    // and set its sprite back to normal:
 
    if (objectNum == 4)
    {
        if (IsObjectAnimating(4))
        {
            SetObjectGraphic(objectNum, objectOrginalSprite);
        }
    }   
}

// Calling this function will drop the currently dragged object.
// If there is no one, nothing happens.
function DropObject() {
 
    if (objectGrabbed != -1)
    {
        on_object_dropped(objectGrabbed);
        objectOrginalSprite = -1;
        objectGrabbed = -1;
    }
}
#sectionstart room_a  // DO NOT EDIT OR REMOVE THIS LINE
function room_a() {
  // script for room: Repeatedly execute
  }
 
  // Main part of the grabbing object script.
// *** Must be called within repeatedly execute ***
function GrabObject_REP_EXEC() {
 
    if (objectGrabbed == -1)
    {
        if (IsButtonDown(mouseButton))
        {
            if (GetCursorMode() == grabbingCursorMode)
            {
                if (on_object_grabbed(objectToGrab) == 1)
                {
                    objectOrginalSprite = GetObjectGraphic(objectToGrab);
                    objectGrabbed = objectToGrab;
                    objectToGrab = -1;
                }
            }
        }
        else
        {
            objectToGrab = GetObjectAt(mouse.x, mouse.y);
        }
    }
    else
    {
        if (IsButtonDown(mouseButton))
        {
            int object_sprite = GetObjectGraphic(objectGrabbed);
       
            int sprite_width  = GetGameParameter(GP_SPRITEWIDTH,  object_sprite, 0, 0);
            int sprite_height = GetGameParameter(GP_SPRITEHEIGHT, object_sprite, 0, 0);
             
            int newX = GetViewportX() + (mouse.x - sprite_width / 2);
            int newY = GetViewportY() + (mouse.y + sprite_height / 2);
               
            SetObjectPosition(objectGrabbed, newX, newY);
        }
        else
        {
           DropObject();
        }     
    }
}


function on_key_press(int key) {
 
    // the following code starts object animation if
    // 'space' key is pressed and provided:
    //  1. grabbed object is the hammer;
    //  2. object isn't animating.

    if (key == 32)
    {
        if (objectGrabbed == 4)
        {
            if (IsObjectAnimating(4)==0)
            {
                SetObjectView(4, 14);
                AnimateObject(4, 2, 0, 0);
            }
        }
   }
}

Scorpiorus

#11
In order for the script to work its GrabObject_REP_EXEC() function must be invoked each game loop. Thus, putting it within the room's repeatedly execute function should make it work.

#sectionstart room_aÃ,  // DO NOT EDIT OR REMOVE THIS LINE
function room_*() {
Ã,  // script for room: Repeatedly execute

Ã,  Ã,  GrabObject_REP_EXEC(); // <-- must be within repeatedly execute;

}

By the way, put room's repeatedly execute function after declaring GrabObject_REP_EXEC, otherwise AGS won't find the last one.

Skeller53

#12
Sorry for being so slow to grasp this, but I do appreciate you for being so patient with me. I'll move that code section around this afternoon and let you know.


OK, I guess I'm blind as well as slow. I get the dreaded "Nested functions not supported you probably forgot to close a brace" error message (at the "functionÃ,  GrabObject_REP_EXEC()" line) when I try to save the following code, which I only changed to include the "functionÃ,  GrabObject_REP_EXEC()" within the repeatedly execute function (I took the ending brace from one place and put it at the end of the section). The braces all match according to the script editor. Where have I gone astray this time, oh wise one . . .


// room script file

// holds an object number that's under the cursor
// but only if the mouse button is released. (or -1):
// (***used internally!***)
int objectToGrab = -1;

// the object number that's currently being dragged (or -1).
// Use this variable to find out what object is being dragged:
// (***read-only***)
int objectGrabbed = -1;

// object's sprite slot before it's grabbed:
// useful if you want to set object's graphic back
// when the object is dropped:
int objectOrginalSprite = -1;

// mouse button to grab an object with:
int mouseButton = LEFT;

// grabbing is allowed for this cursor mode:
int grabbingCursorMode = MODE_USE;

// This event occurs when we are about to grab an object (number 'objectNum').
// You can decide here whether it's allowed to grab the object.
// You must return 1 if the object can be dragged.
// Otherwise, return 0.
function on_object_grabbed(int objectNum) {
Ã, 
Ã,  Ã,  if (objectNum == 4)Ã,  return 1; // grab hammer
Ã,  Ã,  if (objectNum == 6) return 1; // grab whirlblade
Ã,  Ã, 
Ã,  Ã,  return 0;Ã, 
}

// This event occurs when we are about to drop an object (number 'objectNum').
// You can, for example, stop animation here or play falling animation
// (see MoveObject command), etc.
function on_object_dropped(int objectNum) {
Ã, 
Ã, 
Ã,  Ã,  // the following code stops the hammer object's animating
Ã,  Ã,  // and set its sprite back to normal:
Ã, 
Ã,  Ã,  if (objectNum == 4)
Ã,  Ã,  {
Ã,  Ã,  Ã,  Ã,  if (IsObjectAnimating(4))
Ã,  Ã,  Ã,  Ã,  {
Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  SetObjectGraphic(objectNum, objectOrginalSprite);
Ã,  Ã,  Ã,  Ã,  }
Ã,  Ã,  }Ã, 
}

// Calling this function will drop the currently dragged object.
// If there is no one, nothing happens.
function DropObject() {
Ã, 
Ã,  Ã,  if (objectGrabbed != -1)
Ã,  Ã,  {
Ã,  Ã,  Ã,  Ã,  on_object_dropped(objectGrabbed);
Ã,  Ã,  Ã,  Ã,  objectOrginalSprite = -1;
Ã,  Ã,  Ã,  Ã,  objectGrabbed = -1;
Ã,  Ã,  }
}
#sectionstart room_aÃ,  // DO NOT EDIT OR REMOVE THIS LINE
function room_a() {
Ã,  // script for room: Repeatedly execute
Ã,  Ã,  Ã, functionÃ,  GrabObject_REP_EXEC() // <-- must be within repeatedly execute;
Ã,  {
Ã,  // Main part of the grabbing object script.
// *** Must be called within repeatedly execute ***
Ã,  Ã,  if (objectGrabbed == -1)
Ã,  Ã,  {
Ã,  Ã,  Ã,  Ã,  if (IsButtonDown(mouseButton))
Ã,  Ã,  Ã,  Ã,  {
Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  if (GetCursorMode() == grabbingCursorMode)
Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  {
Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  if (on_object_grabbed(objectToGrab) == 1)
Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  {
Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  objectOrginalSprite = GetObjectGraphic(objectToGrab);
Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  objectGrabbed = objectToGrab;
Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  objectToGrab = -1;
Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  }
Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  }
Ã,  Ã,  Ã,  Ã,  }
Ã,  Ã,  Ã,  Ã,  else
Ã,  Ã,  Ã,  Ã,  {
Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  objectToGrab = GetObjectAt(mouse.x, mouse.y);
Ã,  Ã,  Ã,  Ã,  }
Ã,  Ã,  }
Ã,  Ã,  else
Ã,  Ã,  {
Ã,  Ã,  Ã,  Ã,  if (IsButtonDown(mouseButton))
Ã,  Ã,  Ã,  Ã,  {
Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  int object_sprite = GetObjectGraphic(objectGrabbed);
Ã,  Ã,  Ã,  Ã, 
Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  int sprite_widthÃ,  = GetGameParameter(GP_SPRITEWIDTH,Ã,  object_sprite, 0, 0);
Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  int sprite_height = GetGameParameter(GP_SPRITEHEIGHT, object_sprite, 0, 0);
Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã, 
Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  int newX = GetViewportX() + (mouse.x - sprite_width / 2);
Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  int newY = GetViewportY() + (mouse.y + sprite_height / 2);
Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã, 
Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  SetObjectPosition(objectGrabbed, newX, newY);
Ã,  Ã,  Ã,  Ã,  }
Ã,  Ã,  Ã,  Ã,  else
Ã,  Ã,  Ã,  Ã,  {
Ã,  Ã,  Ã,  Ã,  Ã,  Ã, DropObject();
Ã,  Ã,  Ã,  Ã,  }
Ã,  Ã,  }
function on_key_press(int key) {
Ã, 
Ã,  Ã,  // the following code starts object animation if
Ã,  Ã,  // 'space' key is pressed and provided:
Ã,  Ã,  //Ã,  1. grabbed object is the hammer;
Ã,  Ã,  //Ã,  2. object isn't animating.

Ã,  Ã,  if (key == 32)
Ã,  Ã,  {
Ã,  Ã,  Ã,  Ã,  if (objectGrabbed == 4)
Ã,  Ã,  Ã,  Ã,  {
Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  if (IsObjectAnimating(4)==0)
Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  {
Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  SetObjectView(4, 14);
Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  AnimateObject(4, 2, 0, 0);
Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  }
Ã,  Ã,  Ã,  Ã,  }
Ã, Ã,  Ã, }

Ã,  }

}


#sectionend room_aÃ,  // DO NOT EDIT OR REMOVE THIS LINE

Scorpiorus

Quote...which I only changed to include the "functionÃ,  GrabObject_REP_EXEC()" within the repeatedly execute function
No no, you shouldn't move the whole definition of the GrabObject_REP_EXEC() function into repeatedly execute. You just need to invoke GrabObject_REP_EXEC from there. And room repeatedly execute must be placed after the defintion of GrabObject_REP_EXEC:

function GrabObject_REP_EXEC() {

<code here>
...
}

function room_a() {
Ã,  // script for room: Repeatedly execute

GrabObject_REP_EXEC(); // invoke the function

}

Skeller53

Sorry. I got confused regarding being "called within" as opposed to placed within. Still, with this change below, I get no error message but still can't grab anything. Grrrr.

// room script file

// holds an object number that's under the cursor
// but only if the mouse button is released. (or -1):
// (***used internally!***)
int objectToGrab = -1;

// the object number that's currently being dragged (or -1).
// Use this variable to find out what object is being dragged:
// (***read-only***)
int objectGrabbed = -1;

// object's sprite slot before it's grabbed:
// useful if you want to set object's graphic back
// when the object is dropped:
int objectOrginalSprite = -1;

// mouse button to grab an object with:
int mouseButton = LEFT;

// grabbing is allowed for this cursor mode:
int grabbingCursorMode = MODE_USE;

// This event occurs when we are about to grab an object (number 'objectNum').
// You can decide here whether it's allowed to grab the object.
// You must return 1 if the object can be dragged.
// Otherwise, return 0.
function on_object_grabbed(int objectNum) {
Ã, 
Ã,  Ã,  if (objectNum == 4)Ã,  return 1; // grab hammer
Ã,  Ã,  if (objectNum == 6) return 1; // grab whirlblade
Ã,  Ã, 
Ã,  Ã,  return 0;Ã, 
}

// This event occurs when we are about to drop an object (number 'objectNum').
// You can, for example, stop animation here or play falling animation
// (see MoveObject command), etc.
function on_object_dropped(int objectNum) {
Ã, 
Ã, 
Ã,  Ã,  // the following code stops the hammer object's animating
Ã,  Ã,  // and set its sprite back to normal:
Ã, 
Ã,  Ã,  if (objectNum == 4)
Ã,  Ã,  {
Ã,  Ã,  Ã,  Ã,  if (IsObjectAnimating(4))
Ã,  Ã,  Ã,  Ã,  {
Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  SetObjectGraphic(objectNum, objectOrginalSprite);
Ã,  Ã,  Ã,  Ã,  }
Ã,  Ã,  }Ã, 
}

// Calling this function will drop the currently dragged object.
// If there is no one, nothing happens.
function DropObject() {
Ã, 
Ã,  Ã,  if (objectGrabbed != -1)
Ã,  Ã,  {
Ã,  Ã,  Ã,  Ã,  on_object_dropped(objectGrabbed);
Ã,  Ã,  Ã,  Ã,  objectOrginalSprite = -1;
Ã,  Ã,  Ã,  Ã,  objectGrabbed = -1;
Ã,  Ã,  }
}
Ã,  Ã, functionÃ,  GrabObject_REP_EXEC() // <-- must be within repeatedly execute;
Ã,  {
Ã,  // Main part of the grabbing object script.
// *** Must be called within repeatedly execute ***
Ã,  Ã,  if (objectGrabbed == -1)
Ã,  Ã,  {
Ã,  Ã,  Ã,  Ã,  if (IsButtonDown(mouseButton))
Ã,  Ã,  Ã,  Ã,  {
Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  if (GetCursorMode() == grabbingCursorMode)
Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  {
Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  if (on_object_grabbed(objectToGrab) == 1)
Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  {
Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  objectOrginalSprite = GetObjectGraphic(objectToGrab);
Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  objectGrabbed = objectToGrab;
Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  objectToGrab = -1;
Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  }
Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  }
Ã,  Ã,  Ã,  Ã,  }
Ã,  Ã,  Ã,  Ã,  else
Ã,  Ã,  Ã,  Ã,  {
Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  objectToGrab = GetObjectAt(mouse.x, mouse.y);
Ã,  Ã,  Ã,  Ã,  }
Ã,  Ã,  }
Ã,  Ã,  else
Ã,  Ã,  {
Ã,  Ã,  Ã,  Ã,  if (IsButtonDown(mouseButton))
Ã,  Ã,  Ã,  Ã,  {
Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  int object_sprite = GetObjectGraphic(objectGrabbed);
Ã,  Ã,  Ã,  Ã, 
Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  int sprite_widthÃ,  = GetGameParameter(GP_SPRITEWIDTH,Ã,  object_sprite, 0, 0);
Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  int sprite_height = GetGameParameter(GP_SPRITEHEIGHT, object_sprite, 0, 0);
Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã, 
Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  int newX = GetViewportX() + (mouse.x - sprite_width / 2);
Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  int newY = GetViewportY() + (mouse.y + sprite_height / 2);
Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã, 
Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  SetObjectPosition(objectGrabbed, newX, newY);
Ã,  Ã,  Ã,  Ã,  }
Ã,  Ã,  Ã,  Ã,  else
Ã,  Ã,  Ã,  Ã,  {
Ã,  Ã,  Ã,  Ã,  Ã,  Ã, DropObject();
Ã,  Ã,  Ã,  Ã,  }
Ã,  Ã,  }
function on_key_press(int key) {
Ã, 
Ã,  Ã,  // the following code starts object animation if
Ã,  Ã,  // 'space' key is pressed and provided:
Ã,  Ã,  //Ã,  1. grabbed object is the hammer;
Ã,  Ã,  //Ã,  2. object isn't animating.

Ã,  Ã,  if (key == 32)
Ã,  Ã,  {
Ã,  Ã,  Ã,  Ã,  if (objectGrabbed == 4)
Ã,  Ã,  Ã,  Ã,  {
Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  if (IsObjectAnimating(4)==0)
Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  {
Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  SetObjectView(4, 14);
Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  AnimateObject(4, 2, 0, 0);
Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  }
Ã,  Ã,  Ã,  Ã,  }
Ã,  Ã,  }

Ã,  }

}


#sectionstart room_aÃ,  // DO NOT EDIT OR REMOVE THIS LINE
function room_a() {
Ã,  // script for room: Repeatedly execute
GrabObject_REP_EXEC(); // invoke the function

}Ã,  Ã, 

#sectionend room_aÃ,  // DO NOT EDIT OR REMOVE THIS LINE

Scorpiorus

Hmm, strange.

Anyway, here is a test game I've just packed:

http://www.geocities.com/scorpiorus82/obj_drag.zip (copy & paste into the address bar)
AGS 2.62 is required.

Skeller53

And of course your game works perfectly! Thank you so much for all the help you've given me so far. I will use your game and the other advice you've provided to try to get the grabbing function to work in my game. There must be something about my game that won't allow this code to work, and I'll just keep at it and see if I can figure it out. The main thing is that I know it is possible to do.

Thanks again and maybe the next time I post here I'll be announcing success (or more likely asking for help with another problem).

Scorpiorus

You are welcome :)

As to that problem, try removing the Run Script action form the room repeatedly execute interaction via the interaction editor and then adding it again and putting the code to call the GrabObject_REP_EXEC() function. Also, make sure you use the MODE_USE cursor mode, as it's the one that's used to grab objects (you can change that, by the way, by assigning a different mode to the grabbingCursorMode variable).

Skeller53

Success, sort of. Out of sheer frustration, I opened your game and then copied your room as a room into my game. I opened up my game, opened up the new room, changed the background image to mine, added my character in place of roger, changed your objects to objects in my game (changing the room script where necessary to reflect the new objects), and fired it up. Voila! It worked. I now have a room where objects can be grabbed, moved, and otherwise manipulated. I have no idea why this works now and what I had in my other rooms that wouldn't allow your code to work, but there you go. I can now use the room I copied from your game to do what I want. I still have collision issues but I am confident these can be resolved.

I can't thank you enough for your help here. Do you have a problem with me proceeding to create a game basically using a room you created? If so let me know and of course I will stop. If you don't mind, on the other hand, I will certainly give you top credit if and when the game is ever released. Many, many thanks to you Scorpiorus!

Scorpiorus

No problem

And, of course, use the room I've uploaded as you like, because that's what I uploaded it for ;)

SMF spam blocked by CleanTalk