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.

Newcomers AppGameKit Corner / Array only giving zeroes?

Author
Message
Fluorescent
FPSC Reloaded TGC Backer
18
Years of Service
User Offline
Joined: 1st Aug 2005
Location: Stockholm, Sweden
Posted: 30th Apr 2016 13:41 Edited at: 30th Apr 2016 13:58
I'm missing something obvious, but can't figure it out. I'm looping over an array of integers, but only get zeroes. Anyone seeing what I'm doing wrong?


A small update. When the dummy array is defined inside a function, the same function where I do my loop, I get zeroes. When I define my array globaly outside the function, it works as expected.
Dybing
12
Years of Service
User Offline
Joined: 12th Sep 2011
Location: Bergen, Norway
Posted: 30th Apr 2016 16:01 Edited at: 30th Apr 2016 16:02
You've not entirely wrapped your thinking around the concept of scope quite yet. In very basic terms, everything inside a function will remain inside the function unless you explicitly say it shouldn't be. This can be done by either returning a variable, datatype or array from the function, referencing one or more variables, data types and/or arrays in the definition of the function, or by being lazy and just give a variable, data-type or array global scope by defining it as such in the main part of your program.

For a short example, see my last post in this thread: https://forum.thegamecreators.com/thread/216949
Fluorescent
FPSC Reloaded TGC Backer
18
Years of Service
User Offline
Joined: 1st Aug 2005
Location: Stockholm, Sweden
Posted: 30th Apr 2016 18:00 Edited at: 30th Apr 2016 18:00
So if I define an array inside a function. Then loop through the array inside the same function I won't get the values because of scope? That doesn't seem to make much sense, shouldn't the array be scoped to the function that it is defined in?
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 30th Apr 2016 19:36
I don't know if this is right or just working around a bug, but arrays need to be global, You can declare them inside a function, but they still need to be declared as global. This will work:

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Quidquid latine dictum sit, altum sonatur
TutCity is being rebuilt
Dybing
12
Years of Service
User Offline
Joined: 12th Sep 2011
Location: Bergen, Norway
Posted: 30th Apr 2016 20:02 Edited at: 30th Apr 2016 20:50
Arrays don't have to be global, this works perfectly fine:



edit: hmmm, there is something strange going on, I tried the OPs code like so:



...and it isn't doing much... Must investigate

edit2: a bit of head scratching later and it would seem that locally at least, you can not both define an Array *and* assign it with values in the same call. You need break it up into two or more calls. One to define, another (or several) to assign with values.

If you must assign values to an array at the same time that you define it, it must be done at the top-level - not inside a function. This do not necessarily make it a global array (unless you say so), so if you want the changes to remain local inside the function, you merely pass it to the function. Like so:

BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 30th Apr 2016 22:32
If you define it at the top level, then it automatically becomes global.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Quidquid latine dictum sit, altum sonatur
TutCity is being rebuilt
Dybing
12
Years of Service
User Offline
Joined: 12th Sep 2011
Location: Bergen, Norway
Posted: 1st May 2016 05:51 Edited at: 1st May 2016 05:55
Not necessarily so Batvink. With the old method, then yes. With the new method however, unless you explicitly say the array is supposed to be global, it'll only be available within the scope you defined it.

So this won't compile because the array is unknown inside the function, eventhough it got defined at the top level:



Change 'myArray as integer[10]' to 'global myArray as integer[10]' however, and it'll run just fine. Though globals are by and large evil, so in the next code-snippet it is further demonstrated how even a top-level array is local unless stated otherwise:



As you can see when you run it, all you get is a sequence 0 through 9 - the values assigned to the array at the top level. It totally disregard the random values assigned in the function, as they are at different scopes. So despite having the same name and all, it does not matter. For all intents and purposes, the array outside of and inside of the function are two different arrays. They do share properties though - as the array inside the function will inherit the length and initial values of the array from outside that got passed to it.

And as you know, if you want the values given the array inside the function to transfer outside of it without resorting to demonic globals, you either pass by reference or return it.
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 1st May 2016 17:12
Useful to know, as a long time user of DBPro and AppGameKit V1 I assumed they were still global at the top level.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Quidquid latine dictum sit, altum sonatur
TutCity is being rebuilt
blink0k
Moderator
11
Years of Service
User Offline
Joined: 22nd Feb 2013
Location: the land of oz
Posted: 3rd May 2016 08:35
Initializing arrays when they are defined within a function does not work. you have to manually initialize the array

Login to post a reply

Server time is: 2024-04-20 03:44:21
Your offset time is: 2024-04-20 03:44:21