calling a global function from within a room

Started by arj0n, Wed 28/07/2010 21:54:31

Previous topic - Next topic

arj0n

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?

Ryan Timothy B

#1
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.

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

Globalscript Header
Code: ags

import function crate_push(Object*crate);


Globalscript
Code: ags

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


Room script:
Code: ags

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.

Joe

#2
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:




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

// 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);
}
Copinstar © Oficial Site

arj0n

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.

SMF spam blocked by CleanTalk