Hi! I'm new to Godot and game development in general, and I'm interested in working on a game that makes heavy use of numerical simulations of systems like partial differential equations, cellular automata, or probabilistic systems, which I like a lot (I'm a computational scientist for work) and think could make for some really fun core game mechanics.

I usually work in Python, where there are lots of nice libraries exposing multidimensional array types which have nice overloaded operators (addition, multiplication, matrix multiplication) that make it simple to code up simulations. The foundational library is numpy.

Looking around, it seems like GDScript does not have such a library: the linear algebra code seems limited to 2D matrices and vectors. There are pooled/packed array types, which would form the core of a multidimensional array, but these don't come with math operators.

I'm interested in finding a nice way to work with a numpy-like API in GDScript rather than writing lots of for loops manually. Two options occur to me:

  1. Writing my own numpy-style library just containing the functions I need directly in GDScript.
  2. Trying to expose the API of a C++ ndarray library (such as xtensor) via a GDExtension so that it would be accessible in GDScript.

I'm wondering if anyone here has any perspectives on these options, or ideas for other options. From my point of view (1.) is straightforward but can take a lot of work depending on how much I want to do, and I'm not too worried about speed but it could be an issue for real-time simulations. (2.) could be a timesaver and I think it could be fast, but my guess is that writing a GDExtension and trying to expose an API from C++ into GDScript is not going to be simple, although I am a working programmer and happy in C++ and Python.

My guess is that just writing for loops is probably going to be the best option, but I wanted to ask anyway 🙂. Thanks for reading!

There is also another thread on this forum (https://godotforums.org/d/19635-python-numpy-via-gdnative) about accessing numpy itself, but my understanding is that this would only be possible by working in Python, and I think I'd like to use GDScript.

    mostsquares Two options occur to me:

    1. Writing my own numpy-style library just containing the functions I need directly in GDScript.
    2. Trying to expose the API of a C++ ndarray library (such as xtensor) via a GDExtension so that it would be accessible in GDScript.

    I was going to say

    1. Use godot-python?

    But it seems like it's perhaps not as active anymore as I remembered it being last I checked.

    mostsquares There is also another thread on this forum (https://godotforums.org/d/19635-python-numpy-via-gdnative) about accessing numpy itself, but my understanding is that this would only be possible by working in Python, and I think I'd like to use GDScript.

    And that's a fair point too I suppose.

    The easiest thing would be to write an extension and the a thin wrapper for an existing math library. Depending on your level of C/C++ this could take around a week or two. The only issue with this is that it would make more sense if you were encapsulating the simulation itself, and just passing parameters from GDScript. It would be kind of cumbersome if each math function call needed to go to the extension.

    Also, GDScript does not have operator overloads. So you cannot override the + or * symbols. This means, if you were to write a library in GDScript, it would have to be more object oriented, and use method calls. This has the effect of making math equations looks more complex and confusing.

    2 years later

    Ivorius how would one use this? and for what purpose? real life example?

      18 days later

      kuligs2 It's basically NumPy but for Godot, with similar selling points: Multi-dimensional math that is both quick to write and performant to execute.
      By now, the docs include a few small use-case examples: Tensors and Broadcasting