how to replace array to other array?i make puzzle game like tetris.

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By bgegg

insert function for insert.
can i replace list array like this?
i want replace value of 3 and 4 index as mino.


this is code.

extends Node2D

var list = [1,0,0,0,0,0,0,1]
var mino = [2,2]

func _ready():
	var sl = list.slice(2,3)
:bust_in_silhouette: Reply From: jgodfrey

If I understand the question, it’d take a number of steps to do what you want. Though, it’d be easy to wrap up in a function.

You could do this a number of ways, but here’s one possibility:

func _ready()
	var target = [0, 1, 2, 3, 4, 5]
	var new = [5, 6]
	array_replace(target, new, 2)

func array_replace(array_target, array_new, index):
	for item in array_new:
		array_target[index] = item
		index += 1


[0, 1, 2, 3, 4, 5]
[0, 1, 5, 6, 4, 5]

Note, there’s no error checking in the above function. Things like array bounds checking should probably be incorporated…

thanks for comment.
i can understand your code in now.

bgegg | 2020-11-15 06:38