Author Topic: Anatomy of the SEXP?  (Read 1542 times)

0 Members and 1 Guest are viewing this topic.

Offline Lepanto

  • 210
  • Believes in Truth
    • Skype
Hello all. This FREDder thanks you for keeping FSO running, and for coming up with new features and SEXPs we can use in our campaigns.

----------

What's the basic structure of a SEXP?

I'm making an indie space shooter in Unity. Since SEXPs are so flexible and powerful, I'd like to incorporate a similar system into my game. Not copying any FSO code, or doing anything that violates the FSO license; just using the same "if this is true, then this happens in the mission" kind of format.

To that end, can someone tell me what the basic code structure/flow of a SEXP is? How does it get from "game inserts input" to "this effect happens in the game"? I'm a coding newb (only know a little C#, which is what I'm using), so it'd be hard to poke around in the code and find out for myself.

Thanks for your help and patience.
« Last Edit: January 08, 2016, 04:19:06 pm by Lepanto »
"We have now reached the point where every goon with a grievance, every bitter bigot, merely has to place the prefix, 'I know this is not politically correct, but...' in front of the usual string of insults in order to be not just safe from criticism, but actually a card, a lad, even a hero. Conversely, to talk about poverty and inequality, to draw attention to the reality that discrimination and injustice are still facts of life, is to commit the sin of political correctness. Anti-PC has become the latest cover for creeps. It is a godsend for every curmudgeon and crank, from fascists to the merely smug."
Finian O'Toole, The Irish Times, 5 May 1994

Blue Planet: The Battle Captains: Missions starring the Admirals of BP: WiH
Frontlines 2334+2335: T-V War campaign
GVB Ammit: Vasudan strike bomber
Player-Controlled Capship Modding Tutorial

 

Offline Dragon

  • Citation needed
  • 212
  • The sky is the limit.
From what I know, it's basically a Common LISP interpreter with a number of game-specific functions. That's why it's so ridiculously powerful. It doesn't support all of Common LISP, but there were talks of implementing something like that.

 

Offline AdmiralRalwood

  • 211
  • The Cthulhu programmer himself!
    • Skype
    • Steam
    • Twitter
If you have even the slightest understanding of code, sexp.cpp is actually one of the more straightforward pieces of code in the codebase; it's almost entirely self-contained, too, so you don't have to keep switching back and forth between different files to follow what's going on most of the time (the only other file you really need to consult most of the time is sexp.h).

Basically, though, the basic structure works something like this:

  • Something (game loop checking events, arrival cue checking whether a ship can jump in, campaign file checking which mission to advance to, whatever) wants to evaluate a SEXP.
  • The SEXP code looks at which operator it is, and calls the appropriate function (this is a giant switch statement).
  • This function goes through the nodes of the SEXP, doing the effects of the SEXP and/or performing whatever calculations it's supposed to perform, and then returning the expected result. This can result in those nodes being themselves evaluated as SEXPs, taking us recursively back to step 1.
  • The result of the SEXP is handled (certain results get cached so that conditions that can never become true don't have to be evaluated again, for instance; if you're curious, these cached results are checked before step 2) and the ultimate result is finally returned to whatever wanted to evaluate the SEXP in the first place.

There's a lot more code in sexp.cpp, but outside of these steps, that code is used solely for FRED: validating arguments, providing in-editor help explaining what the various SEXPs do, et cetera.
Ph'nglui mglw'nafh Codethulhu GitHub wgah'nagl fhtagn.

schrödinbug (noun) - a bug that manifests itself in running software after a programmer notices that the code should never have worked in the first place.

When you gaze long into BMPMAN, BMPMAN also gazes into you.

"I am one of the best FREDders on Earth" -General Battuta

<Aesaar> literary criticism is vladimir putin

<MageKing17> "There's probably a reason the code is the way it is" is a very dangerous line of thought. :P
<MageKing17> Because the "reason" often turns out to be "nobody noticed it was wrong".
(the very next day)
<MageKing17> this ****ing code did it to me again
<MageKing17> "That doesn't really make sense to me, but I'll assume it was being done for a reason."
<MageKing17> **** ME
<MageKing17> THE REASON IS PEOPLE ARE STUPID
<MageKing17> ESPECIALLY ME

<MageKing17> God damn, I do not understand how this is breaking.
<MageKing17> Everything points to "this should work fine", and yet it's clearly not working.
<MjnMixael> 2 hours later... "God damn, how did this ever work at all?!"
(...)
<MageKing17> so
<MageKing17> more than two hours
<MageKing17> but once again we have reached the inevitable conclusion
<MageKing17> How did this code ever work in the first place!?

<@The_E> Welcome to OpenGL, where standards compliance is optional, and error reporting inconsistent

<MageKing17> It was all working perfectly until I actually tried it on an actual mission.

<IronWorks> I am useful for FSO stuff again. This is a red-letter day!
* z64555 erases "Thursday" and rewrites it in red ink

<MageKing17> TIL the entire homing code is held up by shoestrings and duct tape, basically.

 

Offline Lepanto

  • 210
  • Believes in Truth
    • Skype
Thanks, all. This should be helpful.
"We have now reached the point where every goon with a grievance, every bitter bigot, merely has to place the prefix, 'I know this is not politically correct, but...' in front of the usual string of insults in order to be not just safe from criticism, but actually a card, a lad, even a hero. Conversely, to talk about poverty and inequality, to draw attention to the reality that discrimination and injustice are still facts of life, is to commit the sin of political correctness. Anti-PC has become the latest cover for creeps. It is a godsend for every curmudgeon and crank, from fascists to the merely smug."
Finian O'Toole, The Irish Times, 5 May 1994

Blue Planet: The Battle Captains: Missions starring the Admirals of BP: WiH
Frontlines 2334+2335: T-V War campaign
GVB Ammit: Vasudan strike bomber
Player-Controlled Capship Modding Tutorial