In Steven Spielberg’s thirtieth feature, “The BFG,” the Big Friendly Giant of the title toils away in his workshop, an Aladdin’s cave as designed by Rube Goldberg, putting dreams inside glass jars. The dreams look like multicolored fireflies, whizzing around with a mischievous chittering sound. “The dreams are so quick,” Sophie, the film’s ten-year-old heroine, says. “On the outside,” the BFG says. “They is long on the inside.” The dreams are blown down a long trumpet-like cone into bedrooms by the giant at night, and they fill the heads of sleeping children with fantasies involving phone calls from the President, T. rex chases, soldiers to the rescue, dancing couples. It’s not hard to see the metaphor that Spielberg and his screenwriter Melissa Mathison are sketching out here: the dreams are like the movies. More specifically, they are like Spielberg’s movies. “I was hearing all the wondrous and all the terrible things, all the secret whisperings of the world,” says the BFG—a line whose second half is Roald Dahl’s but whose first half glances at Spielberg’s career, for whom wonder and terror have long been mainstays.

The script for “The BFG” was the first by Melissa Mathison in nearly two decades and the last she wrote before she died, in November, at the age of sixty-five, of neuroendocrine cancer, an illness that was only diagnosed as production of the movie neared its end. Her involvement in the film went far beyond the usual parameters of a screenwriter-for-hire, in part because of the technology involved and in part because of the unusually close working relationship she had with Spielberg, which they first worked out on “E.T. The Extra-Terrestrial.” “Their conversations would go far, far beyond what was simply on the page,” the producer Kathleen Kennedy says. Kennedy first acquired the rights to Dahl’s book in 1991 and saw it through many iterations (at one point Robin Williams was slated to star) before asking Mathison to write a version of the script, with Spielberg producing. Spielberg then decided to direct, in 2014, and Mathison went out to the director’s Long Island residence as he mapped out the film in pre-visualized animations in his garage: they made the entire movie this way, from beginning to end, watched it and changed it.

During principal photography, in Vancouver, in the spring of 2015, Mathison was on the set every day, handing cards to the director with the day’s scene on it—a practice carried over from “E.T.” that encouraged Spielberg to detach from the script in its entirety. “Melissa was so inspirational for me on ‘E.T.’ because she had this technique that I’d never used before, and I’ve only done it with Melissa,” Spielberg says. “She said, ‘Why don’t you leave your script at home and just focus on the day’s work?’ So she wrote out the day’s work on three-by-five cards, and printed the cards and gave me a copy. ‘If you really need to see the script, the continuity person is sitting just over there.’ I always carry the script with me all the time, and she weaned me of holding my script like Linus’s security blanket. When we did ‘The BFG,’ I looked at Melissa and she said, ‘Steve, are you ready to get back to the cards?’ I went, ‘Really, Melissa?’ ‘Yes, I think we need to keep a continuity between the last time we worked and this time,’ so I went right to the cards.”

Whether consciously or not, the technique reproduced the more spontaneous in-the-moment psychology of a child. “They’re in the moment, and to have Melissa on the set means that Melissa is in the moment along with the children,” Spielberg says, describing how Mathison continued to make changes to the script, one day coming to him with something she had dreamed the night before. “Melissa would be watching a scene and she’d call me over to her chair and she’d say, ‘Wouldn’t it be wonderful if Sophie could say this, instead of what I wrote three months ago?’ I would run over to Ruby”—Barnhill, the actress who plays Sophie—“and I’d say, ‘Say this instead,’ and she would. It was a hand-in-glove kind of relationship with her. I wore the glove on her right hand, and she wore the glove that I usually wear on my left.”

Such an intimate collaboration between a writer and director is rare. The days of Howard Hawks playing backgammon on set with Ben Hecht and Charles MacArthur, or John Ford’s marathon poker sessions with Dudley Nichols, or Alfred Hitchcock’s long gourmand lunches with John Michael Hayes, have passed into legend, but the franchise farm that is modern Hollywood tends to work against such recurring collaborations. All but one of the “Harry Potter” films were written by Steve Kloves but farmed out to different directors. The same was true of the “Bourne” and “Captain America” movies. Martin Scorsese teamed up with the writer Paul Schrader four times, for “Taxi Driver,” in 1976, and “Raging Bull,” in 1980, during the making of which they fell out, before reteaming for "The Last Temptation of Christ," in 1988, then “Bringing Out the Dead,” in 1999, which was adapted from Joe Connelly’s novel about fried ambulance drivers, itself an homage to Scorsese’s New York, and thus introducing the danger of a kind of creative-feedback loop. “The heroine’s called Mary,” Schrader warned the director over dinner. “Watch out for the Catholic symbols. You’ve already done that in ‘Mean Streets’ and ‘Raging Bull.’ ”

If self-consciousness is the danger of such reunions, Mathison and Spielberg put it to work for them. Audiences have good reason to fear whenever filmmakers armed with digital paint boxes address the unlimited potential of the imagination as their subject—as Disney’s recent “Alice Through the Looking Glass” showed, C.G.I. imaginariums have a tendency toward gaudy overcrowdedness—but the images of Dahl’s dram country, briefly described in the book, have a classic, organic simplicity: a stream running uphill, a large oak tree reflected in a pool against a starry night sky, its inverted reflection a portal to the dream world. (That tree could easily have been grown by Spielberg and Mathison’s botanist extraterrestrial from 1982.) “The BFG” is a two-hander, like “E.T.,” that tells of the friendship between a lonely child and a kindly figure of fantasy who is in some sense conjured from their own need for companionship and escape. The movie is also a rekindled conversation between Mathison and Spielberg, the dreamcatcher turned corporate-entertainment giant, about the nature of cinematic dreams.

“I think there is a conversation that we’re having, and I think it’s a conversation that we had, ever since I met her in the deserts of Tunisia, in 1980,” Spielberg says. He first talked with Mathison while he was shooting “Raiders of the Lost Ark,” in Nafta, Tunisia, in 1980. She was dating the film’s star, Harrison Ford, who later became her husband. It was a hundred and twenty degrees. Amid a couple of hundred Arab extras dressed in German uniforms, Spielberg saw a figure who “looked like an egret” bent over, looking at fossils and seashells embedded in the desert floor. When he found out that she had written the screenplay for “The Black Stallion,” Carroll Ballard’s 1979 film about the friendship between a boy and a horse, which he had loved, Spielberg peppered her with questions about it and pitched her his idea about a young boy, a child of divorce, who befriends an alien creature who has been left behind on Earth. The story of “E.T” was pretty complete even at that point—the government abducts the alien, who is then rescued and released by the boy—but, while Spielberg was editing “Raiders” with Michael Kahn, he invited Mathison into their editing suite, and, when his attention was free, the two of them hashed out the rest of the film on small cards, scattered about, while sitting on the floor.