godotengine / godotengine/godot-visual-script

VisualScript node for array access of an element by index

Open
#15 8 comments 1 reaction 0 assignees View on GitHub
enhancement
Dominant language
C++
Stars
131
Forks
25
PR merge metrics
No merged PRs in 30d

Description

**Describe the project you are working on:**
Any project using VisualScript.
**Describe the problem or limitation you are having in your project:**
The existing nodes for interacting with arrays have no way to access a single element of an array using the element's index within that array. The node-based workaround creates unnecessary iterators, harming performance.
**Describe how this feature / enhancement will help you overcome this problem or limitation:**
A node that functions the same way as a "var = array[n]" statement in GDScript would entirely eliminate the problem.
**Show a mock up screenshots/video or a flow diagram explaining how your proposal will work:**
Arrays in VisualScript should have a node that takes two inputs, an array and an integer, and outputs the data stored at the index corresponding to that integer within the array. If the integer is greater than the size of the array, NULL could be returned.
**Describe implementation detail for your proposal (in code), if possible:**
See above, it's really just a node for "var = array[n]" with a bit of bounds checking. VisualScript already has access to all of GDScript's array functions, but none of those methods approximate "array[n]". In order to fully match the usefulness of "array[n]", a second node could be added that would have three inputs: the array, the index integer, and a variable. This would set the element at the index integer in the array to the input variable. Then it might return the edited array. In essence, a node-based way to implement "array[n] = var," though admittedly the node-based workaround for this is fairly simple ("array.insert(n, var)" followed by "array.remove(n+1)").

I'm not sure how the Godot engine implements VisualScript nodes under the hood, so here's some pseudocode:

Array element 'n' getter:

element_get(Array a, int index) {
if (index >= a.size() || index < 0) {
return NULL;
} else {
return a[index];
}
}

Array element 'n' setter:

element_set(Array a, int index, var data) {
if (index >= a.size() || index < 0) {
return NULL;
} else {
a[index] = data;
return a;
}
}
**If this enhancement will not be used often, can it be worked around with a few lines of script?:**
It is an extremely common use for arrays, and the current node-based workaround is a dedicated function that copies the array, loops through it by popping the front and decrementing the index, then returns the value at index 0 in the copied array once the index argument equals zero - a mountain of nodes and wasted computational power.
**Is there a reason why this should be core and not an add-on in the asset library?:**
VisualScript's interaction with arrays without such a node is severely limited, as accessing the array becomes needlessly obtuse, which is something VisualScript should try to avoid as part of its design philosophy.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.