Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: mode7 on Fri 12/11/2010 09:42:12

Title: Move object from module script
Post by: mode7 on Fri 12/11/2010 09:42:12
I have this in my module script:

// new module script
function repeatedly_execute_always() {
overhotspot.X = omarker.X+13-GetViewportX();
overhotspot.Y = omarker.Y-15-GetViewportY();
cmarker.x = omarker.X;
cmarker.y = omarker.Y+50;
}

When I try to run the game I get an undefined object error. How do I reference objects from a module? Or isn't it possible?
Title: Re: Move object from module script
Post by: Khris on Fri 12/11/2010 10:27:40
You can reference them using the object[] array.
Title: Re: Move object from module script
Post by: monkey0506 on Fri 12/11/2010 16:21:10
To further elaborate, Hotspots and Objects technically only exist within the room which they are created. None of their data is loaded into memory until that room is loaded. For this reason, the script names of hotspots and objects are only available within their respective rooms. It is actually even possible to have, for example, an object with the script name of "oDoor" in every room in your game! So, there would be no way for other scripts to know which oDoor object you are referencing.

As Khris said, you can use the global object array from other scripts, but bear in mind that since you have to reference the object by its ID instead of by its name, it could have undesired results if it's not in the room you expect. Be sure you are also checking the room as well.

Based on your code given, I would actually recommend just putting this function in the room script. However, if you really want to put it in a script file entirely separate from the room script, you could do something like:

function repeatedly_execute_always() {
  if (player.Room == PUT_ROOM_NUMBER_HERE) {
    object[PUT_OVERHOTSPOT_ID_HERE].X = object[PUT_OMARKER_ID_HERE].X + 13 - GetViewportX();
    object[PUT_OVERHOTSPOT_ID_HERE].X = object[PUT_OMARKER_ID_HERE].Y - 15 - GetViewportY();
    cmarker.x = object[PUT_OMARKER_ID_HERE].X;
    cmarker.y = object[PUT_OMARKER_ID_HERE].Y + 50;
  }
}


I actually had to guess as to what overhotspot was, but I'm guessing it's also an object.

If you want to use these overhotspot and omarker objects in every room (or even more than one room), you might consider simply using a Character instead, since they are global and you wouldn't have to worry about recreating them for each room.
Title: Re: Move object from module script
Post by: mode7 on Fri 12/11/2010 17:17:37
Thanks for the explanation monkey. I'm moving a marker around when the player is standing on a specific hotspot, so you can always see what you're looking it (its a keyboard controlled game). Well I think an object isn't very suitable for the job then. I'll replace it with a gui then.