How to remove [brackets] from an integer in Godot?

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

I’m trying to use an integer which is pulled from an array to use another array. What I’d like it to be able to do:


The problem is that when I call the values from the other Array, they come out as [1], [2], [3], and so on rather than 1, 2, 3. So it throws an error “Invalid get index ‘[1]’ (on base: ‘Array’).”. Is it possible to strip the square brackets off the output of the other function, so that it will try to print “(my_array[1])” instead of “(my_array[[1]])”? Maybe the int() could work but I can’t seem to implement that properly without getting errors. Thanks!

An integer can’t have “brackets” - it’s just a number. I suspect you have something else going on. Can you actually show the rest of your code, including where you define the array?

kidscancode | 2019-01-28 05:59

Sounds like you have a multi-dimensional array. An array of arrays. And what you’re actually passing is another array instead of an integer. Like kidscancode said, if you can post more code, we may be able to offer more advice :slight_smile:

i_love_godot | 2019-01-28 15:02

Sorry, I’ve been gone all day! Anyways, here’s some more specific information including the exact code I’m trying to use. masterlist is a script which (for now) just appends a series of strings:

var track = []

func _ready():

And I have an ItemList with the following code:

func _ready():
for i in range(0, masterlist.track.size()):

func _on_ItemList_item_selected(index):
var selected = get_selected_items()

Let’s assume I’ve just opened the game. I select the item “three” on the list. This throws the error "Invalid get index ‘[3]’ (on base ‘Array’).

Eventually my goal is to add the strings that are called by this code to another array, but since I’m pretty new to coding, I’ve been going in “baby steps” by printing as I go along to help me better understand exactly what the code I’m writing does.

toed | 2019-01-29 05:33

:bust_in_silhouette: Reply From: kidscancode

get_selected_items() returns an Array of the items you have selected. This is because it’s possible to select more than one item in an ItemList. It’s still an array even if it only has one item in it. So effectively, you’re trying to do


You either need to loop through the selected items:

func _on_ItemList_item_selected(index):
    for selected in get_selected_items():

Or, since the signal passes the index of the clicked item, you could just do this:

func _on_ItemList_item_selected(index):

Note: It’s a good habit to check the return type of any methods you’re using. See here:

kidscancode | 2019-01-29 06:04

Ah! I figured I was using the ItemList functions wrong. I couldn’t really find many tutorials on this stuff, but you’re a lifesaver! Thanks a lot!

toed | 2019-01-29 06:07

:bust_in_silhouette: Reply From: Darc

I have discovered a super easy solution to this problem.

The solution is to double cast…

var coffeeCrisp= int(str(m_chocolateBars))

That should do the trick…

int(str(m_chocolateBars)) gets turned into a string, then when converting into a integer the brackets get truncated.