Get all variables in array in order.

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

I want it to automatically retrieve all the variables in an array, not one by one, but in order, all the way to the end. For example:

var array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 ... 100]

Transfer these 100 values ​​you have seen to a “Label”, respectively.
Because it takes a lot of time to do this much value one by one.

For example: we do not know how many numbers are in an array, the game transfers the names of all the files in a folder to the array. It is not possible for us to take these values. Because how can we print the texts we do not know one by one on a “Label”? Is not it? :smiley:

:bust_in_silhouette: Reply From: jgodfrey

If you just want to display the items in the array (in the CURRENT order) in a text label, here’s one way…

var array = ["s1", "s7", "s4", "s2", "s10", "s6"]
$Label.text = "\n".join(array)

That creates a single string consisting of every element in the array, separated by a newline character (to put each element on a different line in the label) and assigns it to the text property of a Label.

Unfortunately it throws an error: The method “join” isn’t declared on base “String”.

Gangster53 YT | 2022-10-10 15:11

Hmmm… Seemsstring.join() isn’t available in 3.4.4 (the above was tested in 3.5). In that case, try this instead:

var array = ["s1", "s7", "s4", "s2", "s10", "s6"]
$Label.text = PoolStringArray(array).join("\n")

jgodfrey | 2022-10-10 15:17

Actually, that’s not exactly what I want. That is, when a function is called, it must first pass the 1st value to “Label”. The next time that function is called, it writes the 2nd value. This way I want a function to pass the next value to “Label” every time it is called.

Gangster53 YT | 2022-10-10 15:42

In that case, there are any number of ways to do that… The simplest might be to just use array.pop_front() to access the next array element each time. That’ll return (and remove) the element at the first position in the array. So, each time you call it, it’ll give you the next element. Ultimately, when the array is empty, it’ll return null.

jgodfrey | 2022-10-10 16:12

Let’s say there are 10 values ​​in the “Array”. As I call this function it will move to the next value, but when it reaches the last value of “Array” it will throw an error. So this is the function. Can we limit to the last value of “Array”?

Gangster53 YT | 2022-10-10 16:50

I mean, the game will throw an error when the value in the “Array” reaches zero. But I only want this function to occur when there is at least 1 value in “Array”. I tried a few things but it didn’t work.

Gangster53 YT | 2022-10-10 17:44

You could write a function to do what you want. Something like this…

func get_next_or_last_element(arr):
	if arr.size() == 0:
		return null
	elif arr.size() == 1:
		return arr[0]
	else:
		return arr.pop_front()

Call it like (passing in your array)

var next_elem = get_next_or_last_element(array)

It’ll return the next element if there’s more than a single element, the last element if there’s only a single element, or null if you pass in an empty array…

jgodfrey | 2022-10-10 22:33

I simply:

func example():
	if not array.size() == 0:
		array.pop_front()

I did it using your code and it works.

By the way, if there is a value in an “Array”, I want it to be removed. Is it possible?
Sample:var sample = ["sample1", "sample2"]
How can I delete the values ​​in this variable from “Array” if it exists in “Array”?

Gangster53 YT | 2022-10-11 13:01