(Feature request)Be able to declare events through script.

Started by Pajama Sam, 12 Feb 2022, 22:29

« previous - next »

Pajama Sam

I know right know there is already a lot going into ags 3.6.0 but I was thinking maybe later add this feature.I'm not sure if it's really that useful to allow declaring events using script but someone said about it to me as being annoying to have to un-declare deleted functions.Some people don't like certain guis that much.What do you think? Is allowing declaring events through script worth it or is this already possible?

Crimson Wizard

#1
Quote from: Pajama Sam on 12 Feb 2022, 22:29someone said about it to me as being annoying to have to un-declare deleted functions.Some people don't like certain guis that much.

I was puzzled by the request at first, but judging by this phrase, I guess you mean an ability to link functions to object events in script, instead of the properties/events panel?
Yes, such ability would be very nice to have.

For a proper feature we would likely require a "function pointer" support in AGS script, that is being able to store a function reference in a variable. It's been discussed before, and we have a proposal open in our issue tracker; but that's a serious task, unlikely done too soon.

In theory there may also be workarounds, like linking function by passing only its name as a string. But such approach may be unsafe, as there will be no way to prevent connecting wrong kind of function (with wrong number of arguments, etc).

Pajama Sam


fernewelten

The delegate objects that I'm currently implementing might help. You can load a delegate object with lots of function calls at runtime. Whenever the delegate object is invoked, all its function calls are executed in turn.

So you'd have, e.g., a function
Code: ags

function oBlueCup.Interact()
{
    MyDelegateObject.Invoke();
}


and see to it that MyDelegateObject is loaded with the function calls you need.

Don't hold your breath, mind you, progress is slow, but I am working on the feature.  :)

Pajama Sam

I assume your saying that your working on being able to have functions like beforefadein without using the event handler GUI and have that the event handler can be activated by script.Because what Im talking about is allowing the event handler be activated using script as in a second control instead of clicking the GUI buttons.It seems like it should be possible to  type a keyword to run the programming that event handler GUI buttons have .I'm no programmer though so I don't know.

Crimson Wizard

#5
Quote from: Pajama Sam on 14 Feb 2022, 05:22
I assume your saying that your working on being able to have functions like beforefadein without using the event handler GUI and have that the event handler can be activated by script.Because what Im talking about is allowing the event handler be activated using script as in a second control instead of clicking the GUI buttons.It seems like it should be possible to  type a keyword to run the programming that event handler GUI buttons have .I'm no programmer though so I don't know.

At first I thought i understand what you mean, but now I am in doubts again...
Your feature request sais "be able to declare events through script", here you say "activate handler using script".
Maybe what you mean is -- call existing button handler from another place? If not, could you give an elaborate example of what you are trying to do?

Hide: ShowHide

As per fernewelten's work, the code example above was perhaps not making things fully clear, so I'll amend.

The idea is to be able to connect a function to variable which may be then used to call this function. If that variable is a part of a button, that button may automatically call it when pressed.

For example,
Code: ags

function game_start()
{
     // connect functions to button events
     bButton1.OnClick += MyEventHandler;
     bButton2.OnClick += MyEventHandler;
     bButton3.OnClick += MyAnotherEventHandler;
}

function MyEventHandler()
{
}

function MyAnotherEventHandler()
{
}


fernewelten

Quote from: Pajama Sam on 14 Feb 2022, 05:22
Because what I'm talking about is allowing the event handler be activated using script as in a second control instead of clicking the GUI buttons.

Where's the difficulty in that? You only need to code oBlueCup_Interact();, i.e., to simply call  in code the function that comprises the event handler. (If you use the current compiler, this function call must be anywhere below the definition of the function that is called.)

It's not quite that easy if you're in GlobalScript and need to call an event function that resides in a room script file. In that case, you'd call `CallRoomScript(...);` in the GlobalScript file and have an `on_call()` function in the room script file that calls oBlueCup_Interact(); etc. in its turn.

As concerns the GUI controls, note that they are all handled with globals. So it's entirely possible in code to have the handler for one GUI control modify the states of another GUI control. For instance, let's say that you want to do: Whenever the user clicks on Button A in GUI G, then GUI H should close. Behind-the-scenes, Button A has an event handler A_OnClick. This is a function, and in this function, you can go
Code: ags

function A_OnClick(GUIControl *control, MouseButton button)
{
    H.Visible = false;
}

Pajama Sam

Sorry I don't comprehend what your saying exactly.Just to be sure were talking about the same thing let me explain again what I mean.I want to be able to type a room function in a room script and not have to put it in the event panel.If you don't put it in the event panel you get a null pointer referenced error.You probably do know what im asking and I probably just don't understand your explanation.

Crimson Wizard

#8
Quote from: Pajama Sam on 14 Feb 2022, 18:05
Sorry I don't comprehend what your saying exactly.Just to be sure were talking about the same thing let me explain again what I mean.I want to be able to type a room function in a room script and not have to put it in the event panel.

By "room function" do you mean like, "room after fade in" kind of function? You do not want to link them on the event panel all the time, and only type in script?

If so, this is not possible at the moment, and this is what may become possible either after fernewelten's function pointer feature is completed.

In case of room events this may also be made possible by changing engine to look for a fixed function names, just like it does with "game_start", "on_mouse_click" and so on.

Pajama Sam

Yes that's exactly what I mean't.I understand now.

SMF spam blocked by CleanTalk