2018-12-25 01:39:08

I've tried to be calm, I've tried to deal with arrays. But no more! I can't take these things anymore! Arrays have pissed me off more than anything else I've encountered, even more than timers! How the fuck! Do you use these things without getting index out of bounds errors!

2018-12-25 01:44:59

OK... first. Remind you of what arrays are:
* Arrays are collections of items with a specified type.
* Arrays are zero-indexed.
* The last element of an array is the size of the array minus one.

"On two occasions I have been asked [by members of Parliament!]: 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out ?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question."    — Charles Babbage.
My Github

2018-12-25 02:11:09

Here’s what I am attempting to do. A message history buffer. Left arrow says previous message and right arrow says the next message. I have the loop for it, but I don’t know what to write for previous and next. I have tried counter plus 1 and minus 1, doesn’t work

2018-12-25 03:15:45

Keep an external index, and increment that, and access the buffer using that index. Then do a check to ensure your not going out of bounds.

"On two occasions I have been asked [by members of Parliament!]: 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out ?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question."    — Charles Babbage.
My Github

2018-12-25 03:22:07

Assuming you're only inclementing / declementing by 1,
if(--counter==-1) counter=0;
or
if(++counter==menuArray.length) counter=menuArray.length-1;

I don't speak as good as I write, and I don't listen as good as I speak.

2018-12-25 16:04:20

@post4: So like this?
Int index=0; then after a new message index+=1; then counter=index. So when I press left arrow: index-=1; speak(messages[counter]);

2018-12-25 20:35:52

@6, no. Find another use of the counter. Something like:
int index=0;
...
// in loop
...
// C++ example, similar enough to BGT
if (index-1<0) {
// re-speak the message just spoken, we're at the edge of the buffer
} else if (index+1>array.size()-1) {
// out of bounds if we increase index, replay last message
} else if (index-1>=0) {
// play message at index
} else if (index+1<=array.size()-1) {
// play message...
}
That's a bit bulky, but works.

"On two occasions I have been asked [by members of Parliament!]: 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out ?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question."    — Charles Babbage.
My Github

2018-12-26 01:27:19

Thank you for the help. It did fix my out of bounds errors. However, I am having the following issue.
speak(messages[index-=1}); If index = 5, than it will always speak the message number 4. Say I press left arrow, and I've already heard message 4, it will keep repeating message 4 and not go down. I have made sure the boundary is 0 and not 4. It treats it as a number rather than a function. How do I make it decrease by one message without it always ending in the same number?

2018-12-26 03:07:13

Deduct the index outside the function call to speak:
index--; // or --index;.
speak(messages[index]);

"On two occasions I have been asked [by members of Parliament!]: 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out ?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question."    — Charles Babbage.
My Github

2018-12-26 03:45:26

that made it work. thank you Ethan. I had no idea an array would be this difficult. But I am much more comfortable using them thanks to the help you have provided. Again thank you.

2018-12-26 19:40:59

No problem. Once you use them enough, you get used to their querk. C++ (and other languages) makes arrays much easier to work with with classes like std::vector (or Python's lists), providing you safety from going out of bounds and causing the classic "memory overwrite" issue that C allows you to do with standard arrays. So practice arrays in a safe environment, and only use C-style ones when you actually have to.

"On two occasions I have been asked [by members of Parliament!]: 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out ?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question."    — Charles Babbage.
My Github

2018-12-27 11:25:52

That's the tricky thing about arrays. Getting a length will give you the length of the array, but since the array is base 0, you always have to subtract one from the total length so you don't go over. I was spoiled with vb6 since it is always base 1. I can't imagine going back to that though.

Much less active on this forum than in the past.

Check out my live streams: http://lerven.me
follow me on Twitter: http://twitter.com/liamerven

2018-12-27 17:08:34

Arrays in VB6 are base 0.  smile

- Aprone
Please try out my games and programs:
Aprone's software

2018-12-27 17:29:28

they are? I remember always starting at 1.

Much less active on this forum than in the past.

Check out my live streams: http://lerven.me
follow me on Twitter: http://twitter.com/liamerven

2018-12-27 17:45:22

Nope.  I mean you can always choose to start them at 1 by just ignoring the zero spot (actually a great idea in some situations), but they do start at zero just like most other languages.  Actually I think there is a command to force them to start at values other than zero, I've just never used it.

- Aprone
Please try out my games and programs:
Aprone's software

2018-12-27 18:22:41

might be base 1 now that I think of it. I'd have to go check old code

Much less active on this forum than in the past.

Check out my live streams: http://lerven.me
follow me on Twitter: http://twitter.com/liamerven

2018-12-28 11:30:10

well that explains why when using my practice game when selecting stuff from the weapon aray you always have to put the weapon number +1.