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.

 [1,0,0,2,2,0,0,1]

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)
	list.insert(3,sl)
	print(list)
: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]
	print(target)
	array_replace(target, new, 2)
	print(target)

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

Output:

[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