• Dear Cerberus X User!

    As we prepare to transition the forum ownership from Mike to Phil (TripleHead GmbH), we need your explicit consent to transfer your user data in accordance with our amended Terms and Rules in order to be compliant with data protection laws.

    Important: If you accept the amended Terms and Rules, you agree to the transfer of your user data to the future forum owner!

    Please read the new Terms and Rules below, check the box to agree, and click "Accept" to continue enjoying your Cerberus X Forum experience. The deadline for consent is April 5, 2024.

    Do not accept the amended Terms and Rules if you do not wish your personal data to be transferred to the future forum owner!

    Accepting ensures:

    - Continued access to your account with a short break for the actual transfer.

    - Retention of your data under the same terms.

    Without consent:

    - You don't have further access to your forum user account.

    - Your account and personal data will be deleted after April 5, 2024.

    - Public posts remain, but usernames indicating real identity will be anonymized. If you disagree with a fictitious name you have the option to contact us so we can find a name that is acceptable to you.

    We hope to keep you in our community and see you on the forum soon!

    All the best

    Your Cerberus X Team

Indexing a Stack

Paul59

Active member
CX Code Contributor
Joined
Dec 13, 2018
Messages
384
Given that:

Cerberus:
Global playerInventory:Stack<InvItem>

Why does:

Cerberus:
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?
 
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.
 
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.
 
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.
 
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."
 
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.
 
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.
 
Back
Top Bottom