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 Studio Chat / [SOLVED] Pass by reference into an advanced array does not give a compile warning.

Author
Message
DewarInversion
3
Years of Service
User Offline
Joined: 27th Mar 2021
Location:
Posted: 28th Nov 2021 06:38 Edited at: 28th Nov 2021 12:47
This week working with advanced arrays I wanted to use advanced arrays to limit a search list instead of having a fixed array to search through. The part of my code which calls the array is done so with a pass by reference call between the function and the advanced array. However, it took me a while to find out why my code wasn't working as the compiler accepted the pass by reference as argument but instead of referencing the list array it made a copy, so any changes made in reference were not being recorded.
So my question is, should there be a compiler warning or error on pass by reference into advanced arrays?
How brilliant it would be if the pass by reference into advanced arrays worked in tier 1.

The first function shows the referenced object 'thisEnemyTarget' as a referenced type enemy going into the list, however, this is an illusion as the referenced object is copied and any changes made to this list of type are lost.
The second function shows how the reference needs to be found again and the details 'manually' copied back into the type variables.

The author of this post has marked a post as an answer.

Go to answer

blink0k
Moderator
11
Years of Service
User Offline
Joined: 22nd Feb 2013
Location: the land of oz
Posted: 28th Nov 2021 07:16 Edited at: 28th Nov 2021 07:22
Quote: "The first function shows the referenced object 'thisEnemyTarget' as a referenced type enemy going into the list, however, this is an illusion as the referenced object is copied and any changes made to this list of type are lost."


I do not believe this is correct. I have used the ref modifier many many many times and it's functionality is not as you describe.

I think you are assuming that when you insert it into the array then it is inserting the reference, however it is inserting a COPY of the reference. This is expected behavior
DewarInversion
3
Years of Service
User Offline
Joined: 27th Mar 2021
Location:
Posted: 28th Nov 2021 12:29 Edited at: 28th Nov 2021 13:06
Correct, I have had no problems with arrays passed by reference. The problem I had was passing an array by reference into an advanced array which made a copy but did not reference back to the calling array ( as I was expecting ). Maybe I was wrong to assume a pass by reference would always update the calling function with latter changes made, but in this instance, it does not.

You can see the added reference I implemented to get this code to work in the second function in lines 32 to 37 in the above. The code does not work without this manual inclusion, I've tried!

I'm unsure if the implementation means it works for all types other than types in an array which can only mean it isn't passing by reference as indicated in the code. It's rather confusing and I couldn't work out why the call didn't update the calling function. As you say, it makes a copy of the type so it isn't by reference.

The following code does as expected with data returned to the calling function in a pass by reference call. The problems start at line 27 and 47 when I make the pass, which, as you say, is a copy, not a reference.
nb: I updated the code rem as it wasn't correct (as you rightly pointed out)
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 29th Nov 2021 18:09
What do you mean by advanced array? Is that what you call dynamic arrays that grow/shrink? If so, I've passed those by reference to functions without problem.
Tiled TMX Importer V.2
XML Parser V.2
Base64 Encoder/Decoder
Purple Token - Free online hi-score database
Legend of Zelda

"I like offending people, because I think people who get offended should be offended." - Linus Torvalds

Raven
19
Years of Service
User Offline
Joined: 23rd Mar 2005
Location: Hertfordshire, England
Posted: 29th Nov 2021 19:05
By Default a Function will create an Instanced Copy of the Data that you're passing in.
This means that for all intended purpose that data ONLY exists within the Function...
Now we can change this behaviour with the 'Ref' Operator., that no longer creates an Instanced Copy but rather an Instanced Reference., this means that the Data doesn't ACTUALLY existing within the Function; but instead is the original Source Data that we're interacting in that function via said specified Variable.

So let's provide a quick example of what I mean:



Now it's difficult to see what you're doing with your Showcased Functions.
I can tell you that thisEnemyTarget is simply a SINGLE Element Type., not an Array (unless you have an Array within it). (Remember you need [ ] for each Array Dimension to Ref an Array).
The other thing is that at no point do you put data back into thisEnemyTarget., meaning the Ref is complete pointless.

Finally myMachineGun[] isn't being Referenced in., but is clearly a Global Variable (but I don't know if you've ACTUALLY tagged it as Global., you might want to if it is; and you want to use it within Functions without issue; as while it should scope properly... you just never know what behaviour AppGameKit as a Scripting language will exhibit without explicit declaration... Scripters tend to "Auto Predict" your intention and get things wrong; and is a major reason why I dislike them greatly as it make lazy programmers and leads to incorrect behaviours within codebases)



In blunt terms... I see no issues with the code., the bigger question is WHAT are you EXPECTING the code to be doing; because the issue I suspect is with your Code Logic., not the Ref Operator; because believe me, when you try to do something it doesn't support the Compiler lets you know and quite verbosely at that.
Ref are NOT Pointers., they're Safe Pointers; keep that in mind, it means they're kinda limited in their functionality support.
DewarInversion
3
Years of Service
User Offline
Joined: 27th Mar 2021
Location:
Posted: 30th Nov 2021 15:56
Thanks for your inputs. I was trying to ref myEnemyTarget[i] (a dimension one array) into myMachineGun.targetList.insert(thisEnemyTarget) and thinking they are of the same type it would be referenced. This isn't supported which makes sense when I think about it. Yes, I was thinking pointers and clearly this isn't how it works.
DewarInversion
3
Years of Service
User Offline
Joined: 27th Mar 2021
Location:
Posted: 11th Dec 2021 14:10
This post has been marked by the post author as the answer.
I converted both arrays over to advanced arrays and the pass by reference is working fine now.

Login to post a reply

Server time is: 2024-04-20 09:18:26
Your offset time is: 2024-04-20 09:18:26