Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / Loop an Array to display items sequentially is it possible?

Author
Message
Levanthus
22
Years of Service
User Offline
Joined: 17th Apr 2003
Location: Cumbria, UK
Posted: 11th Aug 2014 17:16
Ok i have another query about arrays, since they are the bane of my existence but damned useful... i have created an array to contain things like notes that you pick up during the game, all good, i even have a way to display them on my screen and scroll through them using arrow keys, brilliant! however... say i pick up two items:-
Slot 0 = Notebook Image
Slot 1 = Item Image 1
Slot 2 = Item Image 2
Now when i bring up my Notebook it automatically shows Slot 0 image if i press the right key it displays item one and again it will display item 2 what i need is:- If i press the right key for a third time it will loop back to the beginning and display the 0 slot image... does that make sense?

This Routine is how I scroll through the items in the array:-



And this is the subroutine that displays the items:-

So far if i press Right a third time it gives me an array out of bounds error message so i just need it to start over again, i've tried many things but none have worked. Cheers.

I can see from your smile, you're not here for the sunset

Windows 7 64 bit, AMD Phenom II x4 Black edition, 7 GB Ram, Radeon HD 4650, 3.6 TB HDD
TheComet
17
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 11th Aug 2014 17:27
I don't know where this is coming from, but stop using next array index. The individual fields of an array are supposed to be accessed by an index.



See the numbers between the brackets? Those specify which index of the array should be accessed.

If your goal is to have an active item selected from within the array, then make a variable that holds an index of the active item, such as:



An "Array out of bounds" error occurs when you specify a number greater than the array size. In the above example, specifying foo(3).bar = 11 would cause this error because the array is currently only size 2.

WickedX
16
Years of Service
User Offline
Joined: 8th Feb 2009
Location: A Mile High
Posted: 11th Aug 2014 17:44 Edited at: 11th Aug 2014 17:45
Darkbasic array’s can use the internal array index. But, as TheComet suggested, array’s are a lot easier to work with if you use an implicit array index. If you prefer working with the internal array index, try something like this.



Levanthus
22
Years of Service
User Offline
Joined: 17th Apr 2003
Location: Cumbria, UK
Posted: 11th Aug 2014 17:49 Edited at: 11th Aug 2014 18:00
So, from what i gather you mean is i should have like:-

yes?

I can see from your smile, you're not here for the sunset

Windows 7 64 bit, AMD Phenom II x4 Black edition, 7 GB Ram, Radeon HD 4650, 3.6 TB HDD
Libervurto
18
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 11th Aug 2014 23:56
Your array name is a bit clumsy. Wouldn't it be nicer to change NoteThing(n).NoteImg to Note(n).image?
Good thinking to store half the screen width and height in variables, but the names NX and NY don't tell us what they are.
On this occasion storing the width/height divided by two doesn't actually save us any processing as these values are involved
in a division by two anyway (you will see what I mean from the code), but it is still worth storing the screen width and height in
variables, I will just give them more descriptive names.
I have turned updateNote into a function. Can you see why I thought it would work better as a function than a subroutine?
I have also added the SilKey function which is great for managing input.


Formerly OBese87.

Login to post a reply

Server time is: 2025-05-14 16:59:46
Your offset time is: 2025-05-14 16:59:46