NEW YORK — With 40 seconds left and the Boston Celtics trailing by six, Kadeem Allen stepped foot onto Madison Square Garden’s vaunted floor.

How many legendary NBA moments have been played on this very court?

And there he was on Thursday, defending next to Kyrie Irving, roving the perimeter to protect the Celtics’ No. 1 pick, Jayson Tatum, and doing his best to make an impact in what would be but a brief cameo appearance.

At 12:45 a.m. the night before, about an hour after arriving in Portland, Maine, from his hometown of Wilmington, North Carolina — where he’d spent a precious few days visiting his 1-year-old daughter, Genesis — Allen got the call.

He flew to Manhattan on Thursday morning to rendezvous with the Celtics as they faced the Knicks in one of the most famous sports venues in the world.

Allen finally got some shut-eye and woke up for his 5 a.m. flight, which was then delayed for two hours.

Oh, well. Allen — a two-way player who shuttles between the Celtics and G League’s Maine Red Claws — made it to the Big Apple just in time for his Broadway debut, with enough time for a little film study.

Such is the life of the former UA starter and young father. One foot in the doorway of his dreams, one foot still in the grind.

“Having a chance to play in the NBA, to play with these guys, the highest level basketball in the world — it’s fun just being around it,” Allen said after the game. “It’s a great opportunity for all of us. Maybe one day we’ll have a guaranteed roster spot.”

He shrugs his shoulders, as if to say that even if it doesn’t work out, even if the NBA is not his future, he’ll still be OK.

• • •

What Sean Miller wouldn’t give for another Allen right about now.

He said as much in a late-November news conference, looking almost forlorn as he recalled what Allen brought to his team the last two seasons.

“I don’t know if anyone here gave Kadeem Allen enough credit,” Miller said.

“But where you should really give him credit is right now. Because without him, man, it’s like pulling teeth right now. Because he is so unselfish, he’s such a hard-playing guy, he’s so talented defensively, that you couldn’t help but raise your level because of how hard he played.”

And if you think for a second that Allen has forgotten that, forgotten what’s gotten him this far, you’re kidding yourself.

“Defense is my ticket. It’s my full ticket,” he said. “It’s what got me here, like what Coach Miller said. Coach (Brad Stevens) trusted me in a close game tonight, in Madison Square Garden, to get a stop, to go in and make a play. I went in, and made a play.”

That he did. Allen was pinned to the pine for 47-plus minutes against the Knicks before summoned late in a tight game. The Celtics were down to 10 players because of an injury.

Three seconds into a short appearance, Allen helped force a Doug McDermott turnover, giving Boston a chance to cut the Knicks’ lead to three points.

Stevens, who made his bones at Butler but is considered one of the rising young coaches in the NBA, points to Allen’s biggest strength as the reason for his late cameo.

“What he does, what he has always done, is he’s a great defender,” Stevens said. “He can get his hands on balls, he’s got long arms, he’s quick and he thinks of himself as a guy who defends, which is a good attribute.”

After the game, Stevens beamed about Allen — and was happy with his decision to insert the former Wildcat into the game.

“It wasn’t a surprise to me that he could go in there and with (Marcus Smart), get us a tie-up and the ball back and give us a chance,” he said.

•••

That’s all Allen wants — a chance.

On a roster that includes former Cal star Jabari Bird and a host of other just-missed NBA prospects, Allen is a clear-cut leader.

“The G League really shows your development,” he said. “The more you play, the better games you play, the better chance you have to get called up. I’ve been playing at a high level, trying to prove to the coaches I can keep playing at that level, trying to show consistency and just play hard.”

The talent level is a far cry from the NBA, of course, but the mentality is the same.

“You’re still trying to win every game. You’ve still got 30 GMs watching each game,” he said. “Each guy in the G League really tries to bring it. Like any other arena, you just have to go hard and compete. NBA, G League, Arizona I’m just going to try to do what I do, which is play hard.”

The goal, of course, is to stick in the NBA, and once he’s there, to get more than 18 seconds. Allen has played in one other Celtics game this season, a 108-85 loss at Chicago on Dec. 11. He got five minutes, collected two rebounds and scored his lone NBA point on a free throw.

Given Allen’s G League play and Stevens’ glowing talk, the former Wildcat appears destined for more time in short order.

“Even when a guy like him just gets to see stuff like this, he can take off and run with it,” Celtics guard Terry Rozier said.

“It’s big for him. He enjoys being up here, gets along with everybody, and everybody likes Kadeem – he’s a great guy and he has been great since Day 1 — and it’s always good for them to come up sometimes. He’s just learning. He’s letting things come to him, and that’s the best way to be sometimes. I think it’s going to work out for him.”

•••

For now, Allen has his eye on the prize. That prize is not NBA riches or fame.

He just wants to play at the top of his game, and he wants to provide for his daughter. Allen makes a good enough living — his G League contract is for the base salary of $75,000, with the potential to earn another $200,000 more, depending on how much time he spends up with the Celtics. But he’s not an instant millionaire like so many of Arizona’s recent draft picks.

For now, the mansion will have to wait.

“I don’t try to live that lifestyle,” he said. “But that is a motivation. I think about my daughter. I think about her every day and every night, and when I step on the court, I try to picture my opponent and tell myself that he’s taking food out of my daughter’s mouth.”

And he thinks about when he’ll get to see her next. He’s hoping that means Christmas in North Carolina, though it’s not for certain.

The Red Claws have a break until Dec. 28. The Celtics, though, host the Washington Wizards on Christmas Day.

“Those visits, they carry me through a lot,” he said of his recent return to North Carolina. “Those three days were a lot to me, just being able to wake up next to her. To hear her say ‘Daddy.’ That was big for me.”

And it will take him through the next few days, whether that includes Christmas with the Celtics or not.

“I just think about her, honestly,” he said. “This world is not very good right now, and I just want to show her that I’ll do what I have to do, financially, as a father, as a friend, to show her a better life. If that’s here on a basketball court or working a 9-to-5 or whatever, I’m going to do whatever I have to do to make sure she’s straight.

“That keeps me going.”