How to make the argument of `regular polygon sides` into a macro?

This is the solution of the puzzle: look in pgflibraryshapes.geometric.code.tex and find that \sides is already used internally:

683 % Shape Regular Polygon.
684 %
685 \pgfdeclareshape{regular polygon}{%
686     \savedmacro\sides{%
687         \pgfmathtruncatemacro\sides{\pgfkeysvalueof{/pgf/regular polygon sides}}%
688     }%

(line numbers added for locating the code). Thus redefining \sides will most likely have adverse effects on processing the code. On page 1112 of the PGF manual we see

enter image description here


Why not use the math tikzlibrary

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{shapes.geometric,math}
\tikzmath{
let \n=15;} 

\begin{document}
\begin{tikzpicture}

    \node [
        draw, regular polygon,
        regular polygon sides = \n
    ] at (0,0) {};
\end{tikzpicture}
\end{document}

@egreg explained you very nicely why the error is there, and here is a simple way how to use \sides nevertheless. You only need to add the .expanded key.

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{shapes.geometric}

\begin{document}
\begin{tikzpicture}
    \newcommand*\sides{15}    
    \node [
        draw, regular polygon,
        regular polygon sides/.expanded=\sides
    ] at (0,0) {};
\end{tikzpicture}
\end{document}

enter image description here