Godot: Add Array.last()

Created on 21 Sep 2016  路  20Comments  路  Source: godotengine/godot

Currently, to get the last element of an array we have to write array[array.size()-1], which is a bit tedious given how many times this case happens.
Could we have an array.last() method like STL does with back()?

feature proposal junior job core

Most helpful comment

Note that we support array[-1] syntax just fine, so this might be useless nowdays.

All 20 comments

Hey, I'm able to do that, it's really simple.

But I have a doubt about the array files at /godot/core/array.h \ cpp;
Just by changing the files and modifying the /doc/base/classes.xml to match, the new "version" of my array will work?

I saw this is different from Custom modules that we 'bind' the methods, that's the why of my doubt.

Edit: We need to write some test case for the new implemented function?

@felaugmar no it won't be exposed to gdscript automatically, you still have to bind it.
The binding for array methods happens in core/variant_call.cpp specifically here and there.

Just for record, it should return null on an empty array right?
And while we are at it, please let's also consider a array.first() function.

@Hinsbart, thx, I'll see if I can figure out how the binding works (it seems a little complex) and if I can understand, I'll contribute with both functions.

@Zylann array.last()
@supaiku-o array.first()

Note that we support array[-1] syntax just fine, so this might be useless nowdays.

@bojidar-bg oh! Brilliant, I didn't know that :)
that makes my request obsolete. Just, be careful about off-by-one coder errors :p

Actually, I wonder if it's really coherent with the C++ side. I don't see access by negative index in Array and Vector, only error.

Well, I did the functions, it's working, but faulty and is not so simple as I thought.
I can't figure out how to bind with the Variant returning value directly and how to break in editor in case of _p->array.size() == 0.

Well, temporarily, I give up of editing the core, it's missing some knowledge for me to do this.
馃槩

@felaugmar usually I think error checking is done with something like

ERR_FAIL_COND(size() == 0, "The array is empty");

Should this be closed, or you prefer it staying open?

I don't mind if nobody else wants this, I use [-1] now in GDScript. I would eventually like to have it in C++ because I saw it's not there, but I don't really need it now.

Retagging for core then...

I would like to try implementing this. From what I gather from @felaugmar's attempt, I have to:

  1. Implement methods Array::front and Array::back in core/array.{h,cpp}
  2. Raise an error if the array is empty
  3. Update the bindings in core/variant_call.cpp
  4. Update the documentation in doc/base/classes.xml
  5. Write tests?

Is that correct?

@Kazuo256 yes :)

Alright. I'll probably come back to ask some things when I get to the tests.

I just noticed I actually wrote front/back, but the original proposal was first/last. Is there a preferred one? Or should I make aliases?

Besides this point, I've finished steps 1 through 4. Time for tests. Is there a guide or quick reference I can follow for this?

Godot does not have automated tests.

OK. Then I only need to know which name to use.

When I first wrote the issue I was not really bothering about the names, as long as they are meaningful. Front and back sound fine, it's like STL.
Thank you for working on this :)

Solved by #7149.

Was this page helpful?
0 / 5 - 0 ratings