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.

AppGameKit Classic Chat / Issue with Print for an element in array

Author
Message
Jeku
Moderator
21
Years of Service
User Offline
Joined: 4th Jul 2003
Location: Vancouver, British Columbia, Canada
Posted: 25th Jan 2015 23:00 Edited at: 25th Jan 2015 23:31
Hey all,

Just porting one of my games over to AGK2 and I'm having an issue which seems like there must be something simple I'm missing.

For one thing, the documentation found on the website for arrays refers to the old AGK1 way of handling arrays, but then there's another page that outlines the "new" way. I wish the AGK1 documentation was in a completely different area so people won't get them confused.

Anyways, I'm loading in a dictionary file with 961 3-letter words:



And when I Print() the size:



I get 961, which is great. However, when I print a random element, say, 100, it's always empty:



It just displays Hello:

Now, my first thought was that my file load wasn't working. I put a



inside of the loop that reads the files, and it's showing a bunch of words as they're loading in. I know that I'm feeding into this array.

Is there something easy I'm missing, or is there some kind of array display bug in AGK2? Thanks!

Senior Developer - CBS Interactive Music Group
Hockeykid
DBPro Tool Maker
17
Years of Service
User Offline
Joined: 26th Sep 2007
Location:
Posted: 25th Jan 2015 23:20
Shouldn't this :



Be this?:




Sean

Funnell7
13
Years of Service
User Offline
Joined: 8th Sep 2011
Location: UK, England
Posted: 25th Jan 2015 23:20
Shouldn't your print command refer to dict3$?

Using AppGameKit V2 Tier 1
Jeku
Moderator
21
Years of Service
User Offline
Joined: 4th Jul 2003
Location: Vancouver, British Columbia, Canada
Posted: 25th Jan 2015 23:31
I messed that up... actually I have dict3$, dict4$, and dict5$, but in the example I put the wrong one by accident.

The problem still happens with dict3$ though!

I'll update it now.

Senior Developer - CBS Interactive Music Group
Jeku
Moderator
21
Years of Service
User Offline
Joined: 4th Jul 2003
Location: Vancouver, British Columbia, Canada
Posted: 25th Jan 2015 23:41 Edited at: 25th Jan 2015 23:48
Just to make matters worse, I've found another weird issue with the way AppGameKit 2 loops perform.



The above is written in that after the first element is read into the array, it should STAY inside the do loop and just display the first element forever, right?

Check out what ACTUALLY happens:

https://www.dropbox.com/s/fn2aun23ko8wlvp/screenie.png?dl=0

Let's assume that the AppGameKit 2 interpreter is actually following the DO LOOP properly.. .that means that every one of my lines got read into the first element. This is probably good for me, because that means I have to figure out why the carriage return on this machine didn't end the line. The next step then is for me to go back to the original test and:



YES, that's it! The entire file is being displayed from element 0. I was getting confused, and I thought that DO LOOP wasn't even working the way it should.. the fact of the matter is the DO LOOP was wroking just fine, but the entire file was being stuffed into just the first element.

EDIT:

Now let's see if Sublime Text has some way of converting carriage returns to the different OS ways.

EDIT:

Fixed! I should use ReadLine instead of ReadString. That's a bit confusing from the documentation, but I got it now. Thanks for helping me out with this. Head scratcher for sure. Loving AppGameKit 2 so far

Senior Developer - CBS Interactive Music Group
SoftMotion3D
AGK Developer
19
Years of Service
User Offline
Joined: 24th Aug 2005
Location: Calgary,Alberta
Posted: 26th Jan 2015 06:30
Quote: "Fixed! I should use ReadLine instead of ReadString"

was just about to mention that...

www.sheldonscreations.com
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 26th Jan 2015 10:06
Pity I just got to this thread, I could have saved you a lot of time, I've already made that mistake!

Is it possible for you to know the number of elements you need before you start? Doing so many inserts is not very efficient. If it works the same way as DBPro, every time you insert it has to recreate the whole array.

1. You could put the number of entries in your text file as the first line.

2. For dynamically sizing arrays, I always add multiple lines at a time to reduce the amount of effort, e.g

myArray.length = myArray.length + 10

Quidquid latine dictum sit, altum sonatur
Jeku
Moderator
21
Years of Service
User Offline
Joined: 4th Jul 2003
Location: Vancouver, British Columbia, Canada
Posted: 27th Jan 2015 01:30
Hi BatVink, thanks for popping in here!

How else can I read in my thousands of word, other than one at a time? I understand what you mean by reading in everything at once, but I can't think of a better way to manage a large list of words than using an array.

I know exactly how many words I will use for my dictionary. Is it better to first DIM the array with the number of elements and then feed it in with the FOR LOOP?

Thanks

Senior Developer - CBS Interactive Music Group
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 27th Jan 2015 09:03
My bad explanation... It's just the array sizing I'm talking about. Try to change the array size as little as possible. It's less important before the game starts, but personally I always try to do it as little as possible.

Quidquid latine dictum sit, altum sonatur
Paul Johnston
TGC Developer
22
Years of Service
User Offline
Joined: 16th Nov 2002
Location: United Kingdom
Posted: 28th Jan 2015 17:39
Quote: "If it works the same way as DBPro, every time you insert it has to recreate the whole array."


AGK uses a different method so that using insert() will allocate the array in chunks, each 1.5 times larger than the last, under the assumption that you are likely to insert more elements in the near future. However if you do happen to know exactly how many elements you will need it will be slightly faster to allocate an array of that size and then fill it with the data.
Jeku
Moderator
21
Years of Service
User Offline
Joined: 4th Jul 2003
Location: Vancouver, British Columbia, Canada
Posted: 2nd Feb 2015 19:32
Great, thanks for the official reply! I'll use DIM on these arrays since I know exactly how many strings will be added to each array.

Senior Developer - CBS Interactive Music Group
Scraggle
Moderator
21
Years of Service
User Offline
Joined: 10th Jul 2003
Location: Yorkshire
Posted: 5th Feb 2015 21:30
I believe the DIM command has been included just for backwards compatibility (or something). My understanding is that the preferred method would be this


AGK V2 user - Tier 1 & 2
Conjured Entertainment
AGK Developer
19
Years of Service
User Offline
Joined: 12th Sep 2005
Location: Nirvana
Posted: 7th Feb 2015 04:34 Edited at: 7th Feb 2015 04:38
shouldn't ...


... be ....


...as in ...

Quote: "
myArray.insert(15,2) // insert 15 at the second index of the array
"


That way you are telling it where to insert the string into the array.
Apologies if I am wrong, because I am new to v2, but that is how I am reading it in the help files.


Coding things my way since 1981 -- Currently using AppGameKit V2 Tier 1
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 7th Feb 2015 11:58
You can leave out the position parameter, and it will add it to the end. If you want to put it anywhere else, then use the position parameter.

There is also array.insertsorted() which looks useful

Quidquid latine dictum sit, altum sonatur
Conjured Entertainment
AGK Developer
19
Years of Service
User Offline
Joined: 12th Sep 2005
Location: Nirvana
Posted: 7th Feb 2015 15:12 Edited at: 7th Feb 2015 15:39
Quote: "You can leave out the position parameter, and it will add it to the end. If you want to put it anywhere else, then use the position parameter."

Right, and in the example starting with 5 it added the 15 in the next slot, so the original ones went unchanged.
If he started with it empty, then where is it stepping from? Nowhere? So it keeps adding to 0? Ending up with the whole thing in that first element?
That would explain why he got those weird results when he tried to show the first element. (see his image)
I am not sure that insert without the position parameter is best for an empty array. (and I think this is the problem)
I do not have his data file to test, but I would be interested in finding out what happens when he makes that suggested change.
It couldn't hurt to try and you may be surprised when the original code works with that ,i added in.
If my theory is correct, then the issue is not with the print() but with the way insert() is handling empty arrays when that position parameter is omitted.


Coding things my way since 1981 -- Currently using AppGameKit V2 Tier 1
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 7th Feb 2015 17:08
Quote: "If he started with it empty, then where is it stepping from? Nowhere?"

It starts at -1, so adds the new element at 0.

Quote: "So it keeps adding to 0? Ending up with the whole thing in that first element?"


No, the last element is now 0, so it adds the next one at 1.

Little demo...



Quidquid latine dictum sit, altum sonatur
Conjured Entertainment
AGK Developer
19
Years of Service
User Offline
Joined: 12th Sep 2005
Location: Nirvana
Posted: 7th Feb 2015 18:10
His example reads from a file, and the array is not defined as global, but none of that matters either.

Quote: "It starts at -1, so adds the new element at 0."

I notice you didn't start at -1.
Neither should the insert(), IMO, it should have the position parameter if you know the position you are inserting to.
I would only leave it blank for appendage if you have a defined array, not an empty one.

Quote: "No, the last element is now 0, so it adds the next one at 1."

Your example does, but his obviously doesn't.
Thanks for the explanation though, as it helps me understand v2 arrays better.

Sorry I couldn't help here.


Coding things my way since 1981 -- Currently using AppGameKit V2 Tier 1
Jeku
Moderator
21
Years of Service
User Offline
Joined: 4th Jul 2003
Location: Vancouver, British Columbia, Canada
Posted: 9th Feb 2015 07:06 Edited at: 9th Feb 2015 07:08
The way arrays are done with AppGameKit 2 are kind of annoying and confusing. It's weird that something with one element would have a size of 0, and no elements have -1.

In the docs:
"Therefore an array of length 0 has one element and an empty array will return the length "-1"."

This is really going to mess me up with this project that deals so heavily with arrays and string manipulation

Already I've stumbled a few times trying to read element 0 of an array and getting something unexpected.

Senior Developer - CBS Interactive Music Group
Scraggle
Moderator
21
Years of Service
User Offline
Joined: 10th Jul 2003
Location: Yorkshire
Posted: 9th Feb 2015 08:23
That's how arrays work on every platform (that I know).

Even on DBP arrays started with the initial element 0.
-1 is a good indicator to say that the array is empty.

AGK V2 user - Tier 1 & 2
JimHawkins
15
Years of Service
User Offline
Joined: 26th Jul 2009
Location: Hull - UK
Posted: 10th Feb 2015 00:51
It's a hangover from rather daft Basic conventions.

Length is in AppGameKit the HIGH index. LOW is usually 1. But, of course, element zero is there as a phantom.

It's like that to protect some legacy code using DIM.

Onwards and sometimes upwards
Jeku
Moderator
21
Years of Service
User Offline
Joined: 4th Jul 2003
Location: Vancouver, British Columbia, Canada
Posted: 10th Feb 2015 06:45
It's definitely not how arrays work in C++, but I digress since this is BASIC. Getting a handle on things now, thankfully!

Senior Developer - CBS Interactive Music Group
Conjured Entertainment
AGK Developer
19
Years of Service
User Offline
Joined: 12th Sep 2005
Location: Nirvana
Posted: 10th Feb 2015 14:43 Edited at: 10th Feb 2015 15:19
Quote: "Length is in AppGameKit the HIGH index. LOW is usually 1. But, of course, element zero is there as a phantom.
"

I use 1 to the array length in my loops and leave 0 as a reserve, but I always declare the size when declaring the array.
I'm not sure why I would want an array of an undefined length when arrays can eat up so much memory.

Quote: "It's definitely not how arrays work in C++, but I digress since this is BASIC."

I guess that is my problem too sorta... I'm used to using BASIC instead of C++.
There seems to be differences in arrays depending on which language, as some start at 0 and others at 1.
This is the first time I have encountered the -1 thing in all my years, but I don't use empties, so that makes sense.

Quote: "Getting a handle on things now, thankfully!"

Me too.
This is my first time seeing the insert().

@ Jeku
Did you try the ,i ?
I am still wondering if that was a quick fix, but its not biggie if you didn't try.
Was just hoping for a result if you did try it.

Quote: "This is really going to mess me up with this project that deals so heavily with arrays and string manipulation"

You could also use 0 as a reserve and assign it a value.
Then use 1 to length for the loops calling the insert() and you should be okay.
This should avoid the -1 slot, as long as you assign the 0 element with a value before using the insert().

It's always been easier for me to use arrays starting at 1 to whatever because I count things with the first thing being 1.
So, like to have that 0 slot sitting there unused for the data.
I do use it on occasion for boolean flags (int arrays), or titles etc (str arrays), or other things that require a variable.
I just call the xelement[0] specifically, and not included in a data manipulation loop.


Coding things my way since 1981 -- Currently using AppGameKit V2 Tier 1
Jeku
Moderator
21
Years of Service
User Offline
Joined: 4th Jul 2003
Location: Vancouver, British Columbia, Canada
Posted: 12th Feb 2015 02:33
@Conjured

Quote: "Did you try the ,i ?"


Actually I didn't have to do that in the example above because I'm just inserting elements onto the end of the array. I understand I can designate an index parameter though, so thanks for the heads up!



Senior Developer - CBS Interactive Music Group

Login to post a reply

Server time is: 2024-11-25 16:51:57
Your offset time is: 2024-11-25 16:51:57