Create featured image from a remote url when creating a post

The code worked for me; but I defined the function like this: function generate_featured( $post_id ), then I commented out the global $post;; and finally (although this is not mandatory..), in the $file_array, I set name like this: 'name' => basename( $image_url ),

So: (Note that I intentionally didn’t include the whole code)

function generate_featured( $post_id, $post, $update ) {
    //global $post;
    if($update == false) {
        ...

        $tmp = download_url( $image_url );
        $file_array = array(
        'name'     => basename( $image_url ),
        'tmp_name' => $tmp
        );

        ...

        // Set post thumbnail.
        set_post_thumbnail( $post_id, $id );
    }
}
add_action( 'save_post', 'generate_featured', 10, 3 );

See https://developer.wordpress.org/reference/hooks/save_post/ if you need (further) help with the save_post hook.