Energy bomb [Solved]
Moderators: winston, another_commander
Re: Energy bomb [Solved]
Well, I did look back when I was writing my OXPs, and decided that awardEquipment() was a simpler way of doing it as it gives a return code for success/failure. No need to check first then do it anyway.
OXPs: Furball 1.8, Factions 1.12
- CommonSenseOTB
- ---- E L I T E ----
- Posts: 1397
- Joined: Wed May 04, 2011 10:42 am
- Location: Saskatchewan, Canada
Re: Energy bomb [Solved]
I too am guilty of not checking if the equipment can be awarded first. Unfortunately, I learn by example and seeing exactly how a piece of code is used in an oxp is far more useful to me. If I never see a certain command being used but a different one or group that sort of performs the same function and everyone is doing it that way, then that will probably be the way I learn to program.
Now that equipment that is not available to all cannot be awarded by script unless it is listed in the shipyard file and noone was checking in previous oxps if the equipment can be awarded, well, there might be alot of oxps to rewrite for 1.77 and we will all have to be alot less sloppy, especially me.
Now that equipment that is not available to all cannot be awarded by script unless it is listed in the shipyard file and noone was checking in previous oxps if the equipment can be awarded, well, there might be alot of oxps to rewrite for 1.77 and we will all have to be alot less sloppy, especially me.
Take an idea from one person and twist or modify it in a different way as a return suggestion so another person can see a part of it that can apply to the oxp they are working on.
CommonSense 'Outside-the-Box' Design Studios Ltd.
WIKI+OXPs
CommonSense 'Outside-the-Box' Design Studios Ltd.
WIKI+OXPs
Re: Energy bomb [Solved]
Well, I am disputing your guilt AIUI the API supports two ways of awarding equipment; "check-then-act" (canAwardEquipment followed by awardEquipment, with or without checking return) and "do it but handle potential failure" (awardEquipment and check return). In the absence of firm guidance I would infer that the latter is actually preferable (one function call, no window of vulnerability between check & act).CommonSenseOTB wrote:I too am guilty of not checking if the equipment can be awarded first.
So, devs, sorry I was unclear in my earlier post; can we have clarification on this specific point?
OXPs: Furball 1.8, Factions 1.12
Re: Energy bomb [Solved]
There are three possible outcomes of a call tom4r35n357 wrote:Well, I am disputing your guilt AIUI the API supports two ways of awarding equipment; "check-then-act" (canAwardEquipment followed by awardEquipment, with or without checking return) and "do it but handle potential failure" (awardEquipment and check return). In the absence of firm guidance I would infer that the latter is actually preferable (one function call, no window of vulnerability between check & act).CommonSenseOTB wrote:I too am guilty of not checking if the equipment can be awarded first.
So, devs, sorry I was unclear in my earlier post; can we have clarification on this specific point?
awardEquipment
:1) The equipment exists and can be awarded.
awardEquipment
awards the equipment and returns 'true'.2) The equipment exists but cannot currently be awarded to this ship.
awardEquipment
returns 'false'.3) The equipment does not exist.
awardEquipment
throws an exception.So it's fine to call
awardEquipment
without calling canAwardEquipment
first, provided that:
- you know, for certain, that the named equipment exists (perhaps because you are awarding it from a script of the OXP that defines it); or
- you use the try/catch syntax to catch the exception if it is thrown and recover appropriately
- JensAyton
- Grand Admiral Emeritus
- Posts: 6657
- Joined: Sat Apr 02, 2005 2:43 pm
- Location: Sweden
- Contact:
Re: Energy bomb [Solved]
And, to be fair, assuming built-in equipment will always exist is pretty reasonable. This is a special case.cim wrote:
- you know, for certain, that the named equipment exists (perhaps because you are awarding it from a script of the OXP that defines it); or
However, the script in question was checking but was doing it in a fragile way. Given the choice between
canAwardEquipment
(which checks all relevant conditions based on the current equipment definitions) and testing the conditions you can think of yourself, canAwardEquipment
is definitely the Right Thing.This “window of vulnerability” isn’t a real thing, since the programming model for scripts is single-threaded/serial and atomic with respect to the application’s state updates. In other words, the only way something can change betweenm4r35n357 wrote:I would infer that the latter is actually preferable (one function call, no window of vulnerability between check & act)
canAwardEquipment
and awardEquipment
in the same function is if you do something to cause a change.(Breaking this assumption would vast numbers of scripts and the resulting programming environment would be far to difficult for casual scripters to deal with. If there’s ever any form of scripting concurrency in Oolite, it will probably be something highly isolated, like Web Workers.)
E-mail: [email protected]
Re: Energy bomb [Solved]
OK, thanks for the clarification guys.
I understand the bit about scripts themselves running in a single-threaded environment, but what if, say, you attempt to award some equipment to a ship in flight, and it gets destroyed after the canAwardEquipment() call and before the awardEquipment() call? Yes, it's a contrived and extreme scenario, but it _could_ happen theoretically, couldn't it? So in that case it would still be necessary to do a try/catch?
FWIW, I do award equipment to ships in flight (to speed up launching multiple ships), but it's core equipment so I just award it and check the return (so I could be vulnerable to an exception too, hmm I think I'll risk it!).
I think I might have over-analysed this
I understand the bit about scripts themselves running in a single-threaded environment, but what if, say, you attempt to award some equipment to a ship in flight, and it gets destroyed after the canAwardEquipment() call and before the awardEquipment() call? Yes, it's a contrived and extreme scenario, but it _could_ happen theoretically, couldn't it? So in that case it would still be necessary to do a try/catch?
FWIW, I do award equipment to ships in flight (to speed up launching multiple ships), but it's core equipment so I just award it and check the return (so I could be vulnerable to an exception too, hmm I think I'll risk it!).
I think I might have over-analysed this
OXPs: Furball 1.8, Factions 1.12
- JensAyton
- Grand Admiral Emeritus
- Posts: 6657
- Joined: Sat Apr 02, 2005 2:43 pm
- Location: Sweden
- Contact:
Re: Energy bomb [Solved]
No, it couldn’t, as long as it was all within one event handler or callback. Obviously callingm4r35n357 wrote:I understand the bit about scripts themselves running in a single-threaded environment, but what if, say, you attempt to award some equipment to a ship in flight, and it gets destroyed after the canAwardEquipment() call and before the awardEquipment() call? Yes, it's a contrived and extreme scenario, but it _could_ happen theoretically, couldn't it?
canAwardEquipment()
and setting a flag, returning, then assuming the flag is still valid in some other event would be bad.E-mail: [email protected]
Re: Energy bomb [Solved]
Are frame callbacks in separate OXPs triggering for the same frame/event run concurrently or simultaneously?
Re: Energy bomb [Solved]
Ah, OK, does that mean that all game threads are suspended during (every) JS (function?) execution?
OXPs: Furball 1.8, Factions 1.12
Re: Energy bomb [Solved]
Even in that situation with flags in different event handlers, you wouldn't need to try/catch (unless the event handlers are so far separated that the player could have saved the game, changed their OXP set, and carried on between the two...). The equipment item exists, so the "worst"m4r35n357 wrote:OK, thanks for the clarification guys.
I understand the bit about scripts themselves running in a single-threaded environment, but what if, say, you attempt to award some equipment to a ship in flight, and it gets destroyed after the canAwardEquipment() call and before the awardEquipment() call? Yes, it's a contrived and extreme scenario, but it _could_ happen theoretically, couldn't it? So in that case it would still be necessary to do a try/catch?
awardEquipment
can do is return false.As with the event handlers, frame callbacks are run sequentially and serially.Switeck wrote:Are frame callbacks in separate OXPs triggering for the same frame/event run concurrently or simultaneously?
While Oolite is technically a multi-threaded application, the vast majority of processing - updating the Universe, handling player input, and displaying the graphics - takes place on the same single thread. JS execution is just another part of updating the universe.m4r35n357 wrote:Ah, OK, does that mean that all game threads are suspended during (every) JS execution?