Indexing a Stack

Paul59

Active Member
CX Code Contributor
Joined
Dec 13, 2018
Location
UK
Given that:

Cerberus X:
Global playerInventory:Stack<InvItem>
Why does:

Cerberus X:
playerInventory[combineIndex].quantity += tempItem.quantity
Give me "Error : Only strings and arrays may be indexed." on build when help says "Stacks may also be efficiently indexed using an integer index, and elements may be inserted and removed."? Does that only apply to the int/float/string versions perhaps or am I doing something wrong?
 

dawlane

Active Member
CX Code Contributor
Joined
Jun 21, 2017
A Stack is a container object class and not an array. So there are methods to set and get the index of a Stack object.
 

Paul59

Active Member
CX Code Contributor
Joined
Dec 13, 2018
Location
UK
A Stack is a container object class and not an array. So there are methods to set and get the index of a Stack object.
Yes thanks I've seen those and working around isn't a big deal. I assume help is just wrong then.
 

dawlane

Active Member
CX Code Contributor
Joined
Jun 21, 2017
I assume help is just wrong then.
The information output while compiling or debugging can be cryptic.

Error : Only strings and arrays may be indexed.
A string object stores characters as an array. The underlying code allows the use of the array with operator syntax.

Stacks may also be efficiently indexed using an integer index, and elements may be inserted and removed.
This kind of output needs to be improved to something like.
Stacks may also be efficiently indexed using an integer value for the index and elements can found, inserted, accessed and removed using the built in Stack class methods.

The documentation could do with an update to include a section in the Language Reference section to cover the basics on the default container classes types.
 

Paul59

Active Member
CX Code Contributor
Joined
Dec 13, 2018
Location
UK
This kind of output needs to be improved to something like.
Stacks may also be efficiently indexed using an integer value for the index and elements can found, inserted, accessed and removed using the built in Stack class methods.
I think the terminology is wrong. I think most people would read that and think they could access an item in the stack directly as they would with an array, for an example mystack[5]. Your improved text and the help docs seem to imply (at least to me!) that this is possible, which it isn't. If this help text was modified:

Get : T ( index:Int ) Returns the element at the specified index.
Insert : Void ( index:Int, value:T ) Inserts value into the stack, shifting existing elements up if necessary.

to use the word 'position' rather than 'index' it would be much clearer. Then the help text could be simplified:

"Stacks have built in methods that allow elements to be found, inserted, accessed and removed."
 

Phil7

Moderator
Joined
Jun 26, 2017
to use the word 'position' rather than 'index' it would be much clearer.
To me 'position' is not clearer, because I associate it with the real world meaning of a position, where counting allways starts at one.

Maybe it would be sufficient, if in the docs it is pointed out, that only elements of arrays and strings (char arrays) can be accessed via square brackets.
The error message could also be modified to something similar.

The word 'indexing' is the misleading one IMAO, because in the first place it is saying that those data are one after the other linked to a number (indesx) and not the act of retrieving one specific data element linked to an index.
 

Paul59

Active Member
CX Code Contributor
Joined
Dec 13, 2018
Location
UK
To me 'position' is not clearer, because I associate it with the real world meaning of a position, where counting allways starts at one.
In the real world position is usually a relative term :D but I see where you're coming from.

Everything would be fine as it stands with just a change to the help text so it didn't imply that stack items could be indexed; the build error is also fine as it stands, it's just contradicted by help. Changing help to - "Stacks have built in methods that allow elements to be found, inserted, accessed and removed." - solves the problem. Although 'index' is mentioned as a parameter in Get() and Insert() I don't think that matters as it's clear what is meant.
 
Top Bottom