Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an event called when a function is executed #2944

Closed
Romitou opened this issue Apr 23, 2020 · 10 comments
Closed

Add an event called when a function is executed #2944

Romitou opened this issue Apr 23, 2020 · 10 comments
Labels
enhancement Feature request, an issue about something that could be improved, or a PR improving something. priority: lowest "Nice to have" updates that are not required (tiny low impact bug fixes or QoL enhancements).

Comments

@Romitou
Copy link
Sponsor Contributor

Romitou commented Apr 23, 2020

Description

Hello!

I want to know if it is technically possible to add an event that would be called when a function of a script is executed.

Since I don't know if my English is correct, here is an example of what I mean by that:

main.sk

function test():
  broadcast "This is a test function."

command test:
  trigger:
    test()

test.sk

on function: # Or an another name :p
  send "%event-function% has been executed."
  event-function is "test"
  broadcast "This is a second text."

This would be extremely handy for adding scripts based on others, for example adding functionality to the base script.

If you think this is not possible or if you have any questions or an alternative to this, please let me know. As usual, thank you very much for your work on Skript. Thanks for reading, have a nice day!

@Pikachu920
Copy link
Member

where would this be useful?

@Romitou
Copy link
Sponsor Contributor Author

Romitou commented Apr 24, 2020

where would this be useful?

The suggestion may sound odd, but let's imagine a mini-game script.

What I was looking for and I didn't find, is a way to add functionality without modifying the main script, so an event which would be called when the main script functions are executed would allow this.

Useful for doing some kind of add-on but in Skript. Of course, my suggestion does not apply if something like this already exists.

@BaeFell
Copy link
Member

BaeFell commented Apr 25, 2020

It's possible to overwrite functions by having your script be parsed after the first script. However, I'm unsure if this is intended behaviour or just a bug that I'm abusing.

@Romitou
Copy link
Sponsor Contributor Author

Romitou commented Apr 25, 2020

It's possible to overwrite functions by having your script be parsed after the first script. However, I'm unsure if this is intended behaviour or just a bug that I'm abusing.

Very interesting, it seems indeed to be a good way which I had not thought of, thank you very much! I'll take a look at it. Unfortunately it does not seem to work since a function has already been loaded and it cannot load a second one with the same name.

@Pikachu920
Copy link
Member

so more generally what you're looking for is a way to create custom events?

@Romitou
Copy link
Sponsor Contributor Author

Romitou commented Apr 30, 2020

I don't know if we can call it custom events, but just a way to detect that something has been executed in another code, so I thought of an event that detects when a function is called. I edited the example of my first comment.

@ShaneBeee
Copy link
Contributor

I'm not 100% sure how I feel about this.
On one hand, I can see this useful, but on the other, I feel this would be useful only for a very VERY small percentage of Skripters.

Ill mark it with low priority and leave it open for discussion.

@ShaneBeee ShaneBeee added enhancement Feature request, an issue about something that could be improved, or a PR improving something. priority: lowest "Nice to have" updates that are not required (tiny low impact bug fixes or QoL enhancements). labels May 12, 2020
@Wealthyturtle
Copy link
Member

Personally, I find this feature redundant... You can technically already do this. In my scripts when I want to trigger a "custom" event, I just use functions. For example:

on damage:
    # ...
    handle_a(victim)
    # ...
function handle_a(e: entity): # You can treat this as an "event"
    # ...

Where handle_a() can be implemented in a secondary script. The only issue would be ensuring that it gets loaded before the primary script, but that's another issue.

@Romitou
Copy link
Sponsor Contributor Author

Romitou commented May 25, 2020

Hello, thank you for your response.
I tried to use this system again just now in 2.5-alpha3, but the second function overwrites the first one.

I don't think I explained my suggestion properly.
In the case of your code, I will try to recover the execution of the function in an event. Not being sure to have explained well, here are other examples with the desired behavior:

game.sk:

function game_example():
    game_start()
    # </> ...

function game_start():
    send "The game has been started!" to {game::players::*}
    # </> ...

modifier.sk:

on function:
    event-function is "game_start"
    # </> other code that would allow to add other effects to the main script

Of course, this is a suggestion for a very specific use. 😅

@TPGamesNL
Copy link
Member

Just like #4360, I think this feature would be fine as a Bukkit event if addons may need this but not as a Skript event.

I don't think we should create a Skript event for this since most functions don't need event calls, and for those few that do it could be coded into the event as mentioned above (custom events are suggested in #121).

Bukkit event are actually already called for functions if an addon requests it (see #816 and 0ccbcd8) (if you want to use it with skript-reflect you can use the instance from Skript.getAddonInstance()).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Feature request, an issue about something that could be improved, or a PR improving something. priority: lowest "Nice to have" updates that are not required (tiny low impact bug fixes or QoL enhancements).
Projects
None yet
Development

No branches or pull requests

6 participants