How to call a function in the global script from a module?

Started by RickJ, Wed 15/03/2006 21:30:43

Previous topic - Next topic

RickJ

I guess what I want to do would be something like having a virtual method defined in the module that could be redefined in the global script.    I don't think virtual methods are supported but is there another way of calling a function in the global script from a module?  Perhaps trigger an event or something?   


Radiant

To my knowledge, no. You cannot even call the "standard" functions (such as on_interface_click) unless they appear earlier in the script.

So, simply, move the function to the module instead, or to a higher module.

Kweepa

Still waiting for Purity of the Surf II

RickJ

Thanks Steve, I hadn't thought of that.   Although it won't help my current situation, I'll have to remember this technique for future reference.

For the GUI/module I'm currently working on I have generic click handler functions (i.e. one for buttons, one for listbox, etc) that recieve the clicks and then call a module function to process them.   The module function maps the click to an event which is returned and used to call a single event handler function similar to interface_click().   I was thinking to encapsulate the generic click handler's in the module (i.e. one for buttons, one for listbox, etc),  and then call a single event function in the global or room script that could be customized by module users.   

In the example below WinBlu.Click() maps the control's click to a previously programatically defined event which is returned.  WinBlu_Event is the event handler that gets customized to do  whatever the user wants.

// Event Handler
function WinBlu_Event(int event) {
   if (event==THIS) {
   }
   else if (event==THAT) {
   }
   else {
      Display("Error");
   }
}

// Generic button click handler
function WinBlu_ButtonClick(GUIControl *control, MouseButton button) {
   WinBlu_Event(WinBlu.Click(control, button));
}

There is one generic click handler for each type of control that can generate clicks or events.   I thought it would make it easier to use the module if these were included in the module instead of requing that they be pasted into the global script.   Using a character that way you suggest would work just fine but I don't think it would makes things any clearer or easer to understand. 

In case anyone wonders what I am doing, I am attempting to create a dynamically configured GUI system for module testing.  The idea is to throw down a few lines of code to easily create a module testbed. 
=====

Anyway Steve's "cDummy" technique gave me an idea.   What if "on_event()" could be triggered from a script command something like this "TriggerEvent(int data);".  You could catch it with on_event() something like this: 

on_event(event, data) {
   if (event==eEventScriptTriggered) {
   }
}

Would anyone else find this useful?  Of course having some kind of "virtual" module functions would also be sweet.   I've already made too many suggestions in recent days so I'll let some else  make it so ...  ::)


Code: ags
 
if (RickJ > AGS_MAX_SUGGESTION_QUOTA) {
   ShutUp(RickJ);
}
else {
   PostSuggestion("SUGGESTION:.....");
}

Pumaman

Some sort of virtual function or function pointer support would be quite handy for this, but it's not one of my top priorities at the moment.

RickJ

I just had another thought about this.  By experiment I found that  CallRoomScript() can be called from a module script to activate the on_call() event handler in the current room.

Anyway this got me to wondering if something like a CallGlobalScript() thing could be easily done, that would allow modules to trigger events in the global script?  Perhaps this is a more practical sapproach and a bit easier to implement than virtual functions, function pointers, etc.

Radiant

Note that CallRoomScript is one of those functions that only gets called when the script you're running is done, and only returns the next cycle. Makes it kind of impractical to get any kind of return value.

RickJ

Perhaps not but it would accomplish what I would like to do and would allow more stuff to be done in the module itself rather than requiring additional code outside the module.   

Pumaman

Hmm, possibly. I don't really like CallRoomScript, I think a neater solution is needed. I'll have a think about it.

HeirOfNorton

I hope a month isn't too long to dig up this thread, but this is one bit of functionality I'd love to see most in 2.73  :).

Anyway, I had another thought. If CallGlobalScript is too messy, what about some sort of forward declaration? I know full forward declaration has probably been on the tracker for ages, but even a simplified version would work. I was thinking a version of "import" for script modules, so you could do something like:

Code: ags

//in a module

import global function foo(int bar);


Anyway, what do you think?

SMF spam blocked by CleanTalk