OXP Event handlers
Moderators: winston, another_commander
- hiran
- Theorethicist
- Posts: 2403
- Joined: Fri Mar 26, 2021 1:39 pm
- Location: a parallel world I created for myself. Some call it a singularity...
OXP Event handlers
So there are numerous event handling functions for world scripts (see World Script Event Handlers), but apart from that there seem to be ship scripts (see Ship Script Event Handlers).
Now I do not intend to create my own ship, so shipdata.plist tweaking won't help. Instead I'd like to attach my event handler to the player ship (and maybe some selected others). But how exactly does this work?
Now I do not intend to create my own ship, so shipdata.plist tweaking won't help. Instead I'd like to attach my event handler to the player ship (and maybe some selected others). But how exactly does this work?
Sunshine - Moonlight - Good Times - Oolite
- Wildeblood
- ---- E L I T E ----
- Posts: 2453
- Joined: Sat Jun 11, 2011 6:07 am
- Location: Western Australia
- Contact:
Re: OXP Event handlers
You spot a useful looking event in the list of Ship Script Event Handlers).hiran wrote: ↑Mon Jul 08, 2024 4:54 pmSo there are numerous event handling functions for world scripts (see World Script Event Handlers), but apart from that there seem to be ship scripts (see Ship Script Event Handlers).
Now I do not intend to create my own ship, so shipdata.plist tweaking won't help. Instead I'd like to attach my event handler to the player ship (and maybe some selected others). But how exactly does this work?
You write a function for it, and include it in a world script.
Test it. It will probably work.
If it's one of the rare ones that are only for non-player ships, it won't work. If so, come back to the BB and have a whinge about it.
Yes, I've had a look at that wiki page; there's a section at the bottom clearly labelled "NPC only". You can use all the events listed above that in world scripts. I suspect I've misunderstood your question, haven't I?
In your heart, you know it's flat.
- hiran
- Theorethicist
- Posts: 2403
- Joined: Fri Mar 26, 2021 1:39 pm
- Location: a parallel world I created for myself. Some call it a singularity...
Re: OXP Event handlers
So I decided for this function: shipDumpedCargoWildeblood wrote: ↑Mon Jul 08, 2024 6:07 pmYou spot a useful looking event in the list of Ship Script Event Handlers).
You write a function for it, and include it in a world script.
Test it. It will probably work.
If it's one of the rare ones that are only for non-player ships, it won't work. If so, come back to the BB and have a whinge about it.
Yes, I've had a look at that wiki page; there's a section at the bottom clearly labelled "NPC only". You can use all the events listed above that in world scripts. I suspect I've misunderstood your question, haven't I?
It is listed in Ship Event Handlers but not in World Event Handlers. Thus I added to my world script this content:
Code: Select all
this.shipDumpedCargo = function(cargo)
{
log(this.name, "shipDumpedCargo(" + cargo + ")");
}
Code: Select all
21:27:25.026 [oolite-starter-oxp]: shipDumpedCargo([Ship "Cargo container" position: (-44068.3, 60055.3, 438413) scanClass: CLASS_CARGO status: STATUS_IN_FLIGHT])
Sunshine - Moonlight - Good Times - Oolite
- phkb
- Impressively Grand Sub-Admiral
- Posts: 4830
- Joined: Tue Jan 21, 2014 10:37 pm
- Location: Writing more OXPs, because the world needs more OXPs.
Re: OXP Event handlers
The worldscript event handles events for the player ship. If you want to link up to the same event for any other ship, the code needs to be in a ship script.
- hiran
- Theorethicist
- Posts: 2403
- Joined: Fri Mar 26, 2021 1:39 pm
- Location: a parallel world I created for myself. Some call it a singularity...
Re: OXP Event handlers
Ok, that is a difference.
How would I, from one OXP, hook a ship script onto some other ship?
Sunshine - Moonlight - Good Times - Oolite
- Wildeblood
- ---- E L I T E ----
- Posts: 2453
- Joined: Sat Jun 11, 2011 6:07 am
- Location: Western Australia
- Contact:
Re: OXP Event handlers
You need some reference to the ship, e.g.
player.ship.target
, then you can read its script at player.ship.target.script
, or replace it entirely using player.ship.target.setScript("hiran-ship-script.js")
https://wiki.alioth.net/index.php/Oolit ... #setScript
Says, in its entirety, the following:
function setScript(scriptName : String)
Set, or completely replace, the javascript code associated to a ship entity.
In your heart, you know it's flat.
- hiran
- Theorethicist
- Posts: 2403
- Joined: Fri Mar 26, 2021 1:39 pm
- Location: a parallel world I created for myself. Some call it a singularity...
Re: OXP Event handlers
How do I get that script name? Is it the filename? Relative to my OXP root? What if I need subdirectories? And then think of subdirectories in Windows and in Linux...
Sunshine - Moonlight - Good Times - Oolite
- Wildeblood
- ---- E L I T E ----
- Posts: 2453
- Joined: Sat Jun 11, 2011 6:07 am
- Location: Western Australia
- Contact:
Re: OXP Event handlers
JS files go into the "Scripts" sub-directory of your OXP. The file world-scripts.plist in "Config" lists scripts to be copied into the "worldScripts" namespace at start up, the other files sit there patiently waiting to be used as ship scripts, equipment scripts, etc.
All the OXPs are merged at load. All the JS files in any subdirectory named "Scripts" go into one big pool. (Similarly, all the PNG files in any "Images" directory, etc.) This allows your OXP to use resources from other OXPs. That's why it's important to prefix filenames with something unique (not "oolite-") to avoid naming conflicts. Then you just use the filename, you don't need to worry about the full path.
Last edited by Wildeblood on Fri Aug 02, 2024 6:55 am, edited 2 times in total.
In your heart, you know it's flat.
- Wildeblood
- ---- E L I T E ----
- Posts: 2453
- Joined: Sat Jun 11, 2011 6:07 am
- Location: Western Australia
- Contact:
Re: OXP Event handlers
I once usedWildeblood wrote: ↑Wed Jul 10, 2024 9:13 pmYou need some reference to the ship, e.g.player.ship.target
player.ship.target.target
to check if another ship was targeting me, then if it wasn't, player.ship.target.target.target
to check if its target was targeting me (which would imply my target was helping me, and would be silly to shoot at.) This sort of thing amuses me. I don't know whether
player.ship.target.target.target.target
works*, but I assume it does. I can't think what it would be good for.* i.e. Whether there is some limit in javascript as to how long such a daisy chain of object references can get.
In your heart, you know it's flat.
Re: OXP Event handlers
I'm having a senior moment attempting to grok that!!Wildeblood wrote: ↑Thu Jul 11, 2024 12:36 amAll the OXPs are merged at load. All the JS files in any subdirectory named "Scripts" go into one big pool. (Similarly, all the PNG files in any "Images" directory, etc.) This allows your OXP to use resources from other OXPs. That's why it's important to prefix filenames with something unique (not "oolite-") to avoid naming conflicts. Then you just use the filename, you don't need to worry about the full path.
Why files not referenced in the pool by e.g. bigship.png (my oxp 'bigship.png') and when not in the current, something like com.flibble.dodgyoxp1.bigship.png, an unimaginatively named png file in another but specific oxp?
In my head, I'm imagining OXP authors (attempting to observe due-diligence) having to grep the file-lists of ALL the available other OXP's in an expanding Ooniverse in order to avoid filename clashes. (explosion.png, texture.png, should-I-prefix-all-filenames-with-a-UUID-to-avoid-mayhem.png)
I feel like I missed a sanity clause, and it's not even Christmas.
- Wildeblood
- ---- E L I T E ----
- Posts: 2453
- Joined: Sat Jun 11, 2011 6:07 am
- Location: Western Australia
- Contact:
Re: OXP Event handlers
1. A cocoanut fell on my head recently, so maybe I'm mis-remembering, but:MrFlibble wrote: ↑Thu Jul 11, 2024 1:36 amI'm having a senior moment attempting to grok that!!Wildeblood wrote: ↑Thu Jul 11, 2024 12:36 amAll the OXPs are merged at load. All the JS files in any subdirectory named "Scripts" go into one big pool. (Similarly, all the PNG files in any "Images" directory, etc.) This allows your OXP to use resources from other OXPs. That's why it's important to prefix filenames with something unique (not "oolite-") to avoid naming conflicts. Then you just use the filename, you don't need to worry about the full path.
Why files not referenced in the pool by e.g. bigship.png (my oxp 'bigship.png') and when not in the current, something like com.flibble.dodgyoxp1.bigship.png, an unimaginatively named png file in another but specific oxp?
In my head, I'm imagining OXP authors (attempting to observe due-diligence) having to grep the file-lists of ALL the available other OXP's in an expanding Ooniverse in order to avoid filename clashes. (explosion.png, texture.png, should-I-prefix-all-filenames-with-a-UUID-to-avoid-mayhem.png)
I feel like I missed a sanity clause, and it's not even Christmas.
2. If I am remembering correctly, "I didn't do it!"
3. I notice worldscripts nowadays have a unique ID prepended to them (it was not so back in my day), but to access one from another we still use its "name" property, which must be unique.
Addendum:
I've been sitting here tying to think of words to back-pedal, but... no. That's how I remember it working. We'll just have to await someone more knowledgeable.
In your heart, you know it's flat.
- phkb
- Impressively Grand Sub-Admiral
- Posts: 4830
- Joined: Tue Jan 21, 2014 10:37 pm
- Location: Writing more OXPs, because the world needs more OXPs.
Re: OXP Event handlers
Yes.MrFlibble wrote: ↑Thu Jul 11, 2024 1:36 amIn my head, I'm imagining OXP authors (attempting to observe due-diligence) having to grep the file-lists of ALL the available other OXP's in an expanding Ooniverse in order to avoid filename clashes. (explosion.png, texture.png, should-I-prefix-all-filenames-with-a-UUID-to-avoid-mayhem.png)
Or you just create a prefix for all your files, and you should be 99.9% OK. The 0.1% will be extremely rare (somehow having two OXP's use the same prefix and then the same filename is *possible*, but highly unlikely).
The missing detail here are the "script.js" files in the Config folder. There are literally hundreds of OXP's with this configuration. *In this instance*, each script.js file is kept unique (ie they do not overwrite eachother), and is automatically added to the worldscripts JS pile without needing to use the world-script.plist file.Wildeblood wrote: ↑Thu Jul 11, 2024 12:36 amJS files go into the "Scripts" sub-directory of your OXP. The file world-scripts.plist in "Config" lists scripts to be copied into the "worldScripts" namespace at start up, the other files sit there patiently waiting to be used as ship scripts, equipment scripts, etc.