can an array function be made in GDScript?

memzenmemzen Posts: 16Member
edited December 2017 in Programming

Is there a way to code some make array functions using GDScript so this could be done:

var levelUnlocked = array1D(25,false)
var boardElement = array2D(10,10,0)

Tagged:

Tags :

Answers

  • CalinouCalinou Posts: 189Admin Godot Developer
    edited November 2017

    Arrays are a built-in GDScript feature. Like in most programming languages, N-dimensional arrays are just arrays contained inside other arrays, so this would be a 2D array:

    var 2d_array = [
        [2, 4, 6],
        [4, 6, 8],
        [6, 8, 10],
    ]
    
  • memermemer Posts: 39Member

    I think the idea is to have it so that array1d(N, x) makes a N-sized array filled with x, and array2d(N, M, x) makes a NxM sized array filled with x.

  • memzenmemzen Posts: 16Member

    Thanks for the replies. I had hoped one line array creation was possible because all of the for loops to get 30+ arrays makes the code extra long instead of elegant.

  • memzenmemzen Posts: 16Member

    FYI - these are snips of code I use to make 2D arrays with 3.0 beta 1.

    In global.gd:

    var boardElement = []

    func _init():
    var r
    for r in range(BoardSizeRows):
    boardElement.append([])
    boardElement[r].resize(BoardSizeColumns)

    In game.gd:

    var g = preload("res://scripts/global.gd").new()

    func board_fill():
    var r; var c
    for r in range(g.BoardSizeRows):
    for c in range(g.BoardSizeColumns):
    g.boardElement[r][c] = randi() % 9 + 1

  • SolarLuneSolarLune Posts: 35Member
    edited December 2017

    @memzen said:
    Thanks for the replies. I had hoped one line array creation was possible because all of the for loops to get 30+ arrays makes the code extra long instead of elegant.

    Ah, really? It seems like you'd only need to do it once or twice, here or there for most situations. If you find yourself doing this a lot, you could just make a function to create 1D or 2D arrays and return you the empty array.

  • memzenmemzen Posts: 16Member
    Answer ✓

    You are correct, my match 3 game uses 13 1D arrays and only 12 2D arrays. But I like code that is less verbose when it can be done.

    Here is how to make 2D arrays using a function.

    # declare arrays
    var q1 = []
    var q2 = []
    var boardTile = []
    var boardItem = []
    
    func array_2d(a,rows,columns):
        for r in range(rows):
            a.append([])            # add a row
            a[r].resize(columns)    # add columns
        
    func _ready():
        q1.resize(10)               # allocate arrays
        q2.resize(25)
        array_2d(boardTile,10,15)
        array_2d(boardItem,10,15)
        q2[23] = 77
        boardTile[9][9] = 1
        print("q1[5]=",q1[5])
        print("q2[23]=",q2[23])
        print("boardTile[9][9]=",boardTile[9][9])
        print("boardItem[5][12]=",boardItem[5][12])
        boardItem[5][12] = 4
        print("boardItem[5][12]=",boardItem[5][12])
    
Sign In or Register to comment.