+6 votes

Hello, I'm stuck on something apparently very simple :

I have an array with only objects in it and I'd like to remove one of those objects by naming it

var array = [obj1, obj2, obj3, obj4]
array.remove(obj3)  # > not working

remove() only works with integrals, not objects. So I have this error popping :

Invalid type in function 'remove' in base 'Array'. Cannot convert argument 1 from Object to int.

I could just write array.remove(3) but if I had removed obj2 before, for instance, then array.remove(3) would remove obj4 instead of obj3...

Is there another function I could use in this case ? :/
Thx !

in Engine by (245 points)

3 Answers

+12 votes
Best answer

There is erase function for it.

array.erase(obj3)
by (9,662 points)
selected by

What does this do to the index of the other elements?

the index is changing

pseudo code :
array = [a,b,c,d,e]
print d index , the result is: 3

erase c in array

print d index , the result is: 2

i have been test it just now

+3 votes

What you could do is search the element you want to delete first, and once you have that's element index, you can remove it.

var i = array.find(obj3)
array.remove(i)
by (689 points)

What does this do to the index of the other elements?

to Aaron Franke

same as my comment up there, the index are changing

+1 vote

oh, so an object is considered as a value ?

I was looking at this :
http://docs.godotengine.org/en/stable/classes/class_array.html
and thought only the "sort_custom" function could deal with objects...

thanks for your clarifications guys !

by (245 points)
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 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 webmaster@godotengine.org with your username.