Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: arj0n on Wed 28/07/2010 21:54:31

Title: calling a global function from within a room
Post by: arj0n on Wed 28/07/2010 21:54:31
I have some code for the "function oBlock0_AnyClick()" in my room script.
In the same room I have this same function for "function oBlock1_AnyClick()" untill "function oBlock7_AnyClick()".

If I'm understanding it right I can place this function 1 time in the global script in stead
of several times in the roomscript for all the "oBlock'x'_AnyClick()" functions in that room.

I can't figure out how to do this.

Any help?
Title: Re: calling a global function from within a room
Post by: Ryan Timothy B on Wed 28/07/2010 22:23:56
You should've posted this in your other thread since it's referring to the same code in that thread.

If this one room is the ONLY time you'll need this function, then just leave it in the room script by doing this (http://www.adventuregamestudio.co.uk/yabb/index.php?topic=41502.msg548934#msg548934).

If it will be accessed by multiple rooms, you can add it to the global script by doing this:

Globalscript Header

import function crate_push(Object*crate);


Globalscript

function crate_push(Object*crate) {
 if (crate.X > 100) {
   ...
 }
 else ...
   ...
}


Room script:

function oBlock0_AnyClick() {
 crate_push(oBlock0);
}

function oBlock1_AnyClick() {
 crate_push(oBlock1);
}
//etc



Edit:
Unfortunately in the Room editor you can't run anything in the function call like myFunction(oBlock0). It will crash. You literally have to make the room function for each object, or just have the same function for each and check which one the mouse is on. But I'd prefer the first method because you know it'll work correctly each time.
Title: Re: calling a global function from within a room
Post by: Joe on Thu 29/07/2010 12:16:48
Ok maybe you didn't understand correctly whay Khris said, look:

You must type this function in the objects events:
crate_push

and you must do it like this:

(http://s3.subirimagenes.com:81/imagen/previo/thump_4877067aclar.png)


Then in the room script you'll code this which will be valid for every object

// room script file

function crate_push(){
  Object *crate = Object.GetAtScreenXY(mouse.x, mouse.y);
  if (crate == null) return;
  //Do stuff, example:
  crate.Animate(1, 1, eOnce, eBlock);
}
Title: [Solved]Re: calling a global function from within a room
Post by: arj0n on Thu 29/07/2010 12:53:11
For now I go for the option that the function in the room will be valid for every object.
Hope that will be enough.

Thanx all.