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 / does Type Array in Type not working ?

Author
Message
Freddix
AGK Developer
22
Years of Service
User Offline
Joined: 19th Sep 2002
Location: France
Posted: 25th Apr 2019 21:26 Edited at: 25th Apr 2019 22:07
Hello.

I experience a problem using Types.

I setup my 1st type :



And then my 2nd that contain an array of type 1 :



If somewhere in my program I create a new data like :


And if I try to insert in SecondOne from inside a function that requested my type as parameter, :


I get no error but length always return empty list from outside the method ....
SecondOne.myDefault.length -> -1

Is this normal ? What Am I doing wrong ?

Regards,
fubarpk
Retired Moderator
19
Years of Service
User Offline
Joined: 11th Jan 2005
Playing: AGK is my friend
Posted: 25th Apr 2019 21:56
can you use program comment blocks in future it helps
fubarpk
fubarpk on Itch...………...https://fubarpk.itch.io/
fubarpk on googleplay..https://play.google.com/store/apps/developer?id=fubarpk
Freddix
AGK Developer
22
Years of Service
User Offline
Joined: 19th Sep 2002
Location: France
Posted: 25th Apr 2019 22:04 Edited at: 25th Apr 2019 22:08
@fubarpk : Sorry.

I think I've found the problem.
It's the way AppGameKit work.

When I request a type as a function parameter, when I call this function with my object, AppGameKit creates a copy of the object that will be sent to the function.
If I modify the 2nd type inside my function , it will not affect the object used as parameter of the function (the 2nd type from the object outside the method is unchanged)
I must then return the new object from the function and affect my old object with the new one from the function.
It's a strange behaviour ... that is really annoying with deep type/arrays/dynamic works....

It caused me another problem. I have a global type array :


If I insert an element inside the global type array from inside a method/function, the global type array is unchanged...
Exemple from a function :


PKTile.length = 1 from inside the function ...
But when I leave it, the global PKTile.length -> <Empty> array...

Is this a bug or a specific choice ?

PS : I've updated the 1st message with more precise details as I was from your answer that I forgot to mention specific information concerning this problem. Sorry.

Regards,
blink0k
Moderator
11
Years of Service
User Offline
Joined: 22nd Feb 2013
Location: the land of oz
Posted: 25th Apr 2019 22:21
That should work. I know it works for me.
I know that i used to get a lot of these issues before i started using #option_explicit which forces you to define all your variables before using them
Freddix
AGK Developer
22
Years of Service
User Offline
Joined: 19th Sep 2002
Location: France
Posted: 25th Apr 2019 22:30
Hello nlinkOk, , use of #option_explicit did change ..... nothing ...
blink0k
Moderator
11
Years of Service
User Offline
Joined: 22nd Feb 2013
Location: the land of oz
Posted: 25th Apr 2019 22:45
ok then. This works ok for me
Freddix
AGK Developer
22
Years of Service
User Offline
Joined: 19th Sep 2002
Location: France
Posted: 25th Apr 2019 22:52 Edited at: 25th Apr 2019 22:53
@blinkOk:
Yes,

But this don't work :

Ortu
DBPro Master
17
Years of Service
User Offline
Joined: 21st Nov 2007
Location: Austin, TX
Posted: 25th Apr 2019 22:54
You can pass by reference using the ref keyword to alter the udt from the function scope if you don't want to make it global
http://games.joshkirklin.com/sulium

A single player RPG featuring a branching, player driven storyline of meaningful choices and multiple endings alongside challenging active combat and intelligent AI.
Freddix
AGK Developer
22
Years of Service
User Offline
Joined: 19th Sep 2002
Location: France
Posted: 25th Apr 2019 22:57
Ortu ?
blink0k
Moderator
11
Years of Service
User Offline
Joined: 22nd Feb 2013
Location: the land of oz
Posted: 25th Apr 2019 23:04 Edited at: 25th Apr 2019 23:05
Yes because it makes a copy of the array when it calls the function. I don't see why you would want to pass a global as a parameter though.
Ortu is referring to;

function AddTile( dPKTile ref as PKTiles_Type[] )

Which passes the actual value rather than a copy
Freddix
AGK Developer
22
Years of Service
User Offline
Joined: 19th Sep 2002
Location: France
Posted: 25th Apr 2019 23:12 Edited at: 25th Apr 2019 23:13
Yes, the sample was not perfect ...
I copy directly from my source code as in my dev. I don't pass the global as argument.

In the main source code :

Here is exactly what I have (simplified) in my project :


When I use PKPasteTile, I get the message, and you can see that PKTile.length = 0 and TileID = 1 (tile created successfully and inserted successfully)
blink0k
Moderator
11
Years of Service
User Offline
Joined: 22nd Feb 2013
Location: the land of oz
Posted: 25th Apr 2019 23:41 Edited at: 25th Apr 2019 23:51
Arrays are stored from zero and .length will point to the last element
so a length of -1 means an empty array
a length of zero means the array has one element

So TileID is 1 and it fails the test because 1 is NOT less that .length + 1 (which is 1)
if TileID > 0 and TileID < ( PKTile.length + 1 )
Freddix
AGK Developer
22
Years of Service
User Offline
Joined: 19th Sep 2002
Location: France
Posted: 26th Apr 2019 10:07
@blink0k:

I have updated my code to works like you said and it does not fix my problem. I really think there's a bug there
Look at this piece of code :


Now it return that tileID = -1 and PKTile.length = 0
Popup message said : "PKPastetile #-1 - IncorrectTileID ( range 0-0 )"
There is probably a bug there.

Regards
fubarpk
Retired Moderator
19
Years of Service
User Offline
Joined: 11th Jan 2005
Playing: AGK is my friend
Posted: 26th Apr 2019 10:26 Edited at: 26th Apr 2019 10:45
why are you returning the length-1 because the first element will be 0 and the length will be 0 when you take 1 from 0 it will be -1 its working as arrays should

fubarpk
fubarpk on Itch...………...https://fubarpk.itch.io/
fubarpk on googleplay..https://play.google.com/store/apps/developer?id=fubarpk
Freddix
AGK Developer
22
Years of Service
User Offline
Joined: 19th Sep 2002
Location: France
Posted: 26th Apr 2019 11:12 Edited at: 26th Apr 2019 11:24
@Fubark

From blink0k :
Quote: "Arrays are stored from zero and .length will point to the last element
so a length of -1 means an empty array
a length of zero means the array has one element"


And I read it incorrectly because it's illogic for me to have a array.length = 0 when the array contains 1 element, so my mind integrated this information incorrectly.
Now I understand this strange behaviour of AppGameKit ... It's different from all languages (like Java, C, etc... where length is really the amount of elements from 0 to len-1)

As a final note .length is not the best word for this command/function/method. It's incorrect as it does not represent the true length of the array (in amount of elements) for this method the name should be more like .lastmembernumber or .lastmemberid, etc.
fubarpk
Retired Moderator
19
Years of Service
User Offline
Joined: 11th Jan 2005
Playing: AGK is my friend
Posted: 26th Apr 2019 11:23
I Agree I Think originally it did return the lastmember + 1 which in fact was the length
but a update fixed that issue
fubarpk
fubarpk on Itch...………...https://fubarpk.itch.io/
fubarpk on googleplay..https://play.google.com/store/apps/developer?id=fubarpk
Freddix
AGK Developer
22
Years of Service
User Offline
Joined: 19th Sep 2002
Location: France
Posted: 26th Apr 2019 12:09 Edited at: 26th Apr 2019 14:44
The worst is that I have no way to make a generic method/function that allow me to get the true length of an array of any kind like getArrayLength( Array as Array ).
I am used to code under Java & C so the AppGameKit behaviour is confusing ...

Thank you all for your helps.

Regards,
Ortu
DBPro Master
17
Years of Service
User Offline
Joined: 21st Nov 2007
Location: Austin, TX
Posted: 28th Apr 2019 05:10
The way array length is handled is consistent with dark basic classic and dark basic pro.

That's just how this language is.

Python is different from most languages in how it handles block scoping by indention level rather than some sort of closure symbol, that's just part of the language.

It isn't going to change to adhere to what is common, and doesn't need to. Neither does array.length
http://games.joshkirklin.com/sulium

A single player RPG featuring a branching, player driven storyline of meaningful choices and multiple endings alongside challenging active combat and intelligent AI.
blink0k
Moderator
11
Years of Service
User Offline
Joined: 22nd Feb 2013
Location: the land of oz
Posted: 29th Apr 2019 04:38
I found it odd to start with but as you begin to code it becomes very convenient.
.length will always point to the last element in the array (Unless it's empty)

Login to post a reply

Server time is: 2024-11-22 21:26:31
Your offset time is: 2024-11-22 21:26:31