0 votes

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? :D

Godot version v3.4.4.stable.official [419e713a2]
in Engine by (95 points)

1 Answer

0 votes
Best answer

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.

by (19,274 points)
selected by

Unfortunately it throws an error: The method "join" isn't declared on base "String".

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")

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.

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.

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"?

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.

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]
        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...

I simply:

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

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"?

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.