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 / Adding a variable in an array by reference

Author
Message
KamaShin
3
Years of Service
User Offline
Joined: 7th May 2020
Location:
Posted: 7th May 2020 21:41
Hi everyone,
I'm fairly new to AppGameKit, i'm trying to see if I can get a reference to an object in an array for later modification.

I would like the following code to "work" even if with a different syntax

Type MyType
X as Integer
EndType

MT as MyType
MT.X = 5

MyArray as MyType[]
MyArray.Insert(ref MT)

MyArray[0].X = 10

//Here I m expecting MT.X to equal 10, is this possible???
Raven
19
Years of Service
User Offline
Joined: 23rd Mar 2005
Location: Hertfordshire, England
Posted: 7th May 2020 23:32
Ref (Reference) is actually quite limited in it's functionality.
It's more or less a workaround to output Types and Arrays from Functions because you can't Typecast the Function itself, and isn't strictly a "Safe" Pointer as you might think it to behave like.
As such, you can't really do what you're trying to... and as AppGameKit completely lacks Pointers, you can't even handle it manually either.

Now what you could potentially do is wrap what you're doing in a Function., there you can use Referencing but that said... I'm assuming you're looking for it to behave where, when you update MT.X it would also update MyArray[0].X as well (because that should be pointing to the MT.X Address).
fubarpk
Retired Moderator
19
Years of Service
User Offline
Joined: 11th Jan 2005
Playing: AGK is my friend
Posted: 7th May 2020 23:58
try this

fubarpk on Itch...………...https://fubarpk.itch.io/
fubarpk on googleplay..https://play.google.com/store/apps/developer?id=fubarpk
KamaShin
3
Years of Service
User Offline
Joined: 7th May 2020
Location:
Posted: 8th May 2020 00:03
I found a way to work around this, and I as thought, it's not doable the same way language with pointers allows you to.
It's a bit more time consuming for the workaround but i managed, thanks all for answering
fubarpk
Retired Moderator
19
Years of Service
User Offline
Joined: 11th Jan 2005
Playing: AGK is my friend
Posted: 8th May 2020 00:16 Edited at: 8th May 2020 00:39
there are other ways of inserting an element

myArray.insert(MT,2)......inserts MT at the second element
myArray.insertsorted(myItem)

https://www.appgamekit.com/documentation/guides/12_array_changes.htm

fubarpk on Itch...………...https://fubarpk.itch.io/
fubarpk on googleplay..https://play.google.com/store/apps/developer?id=fubarpk
Qugurun
Valued Member
9
Years of Service
User Offline
Joined: 8th Dec 2014
Playing: AppGameKit
Posted: 8th May 2020 00:34
KamaShin, please, show how you got around it.
KamaShin
3
Years of Service
User Offline
Joined: 7th May 2020
Location:
Posted: 8th May 2020 09:49 Edited at: 8th May 2020 10:28
@fubarpk i really don't have a problem inserting the object in an array, the problem is, the insertet object is a copy, when I would have wanted it to be that very object (modifying the array OR MT would modify BOTH)
@Qugurun well, the workaround is, well, like any workaround, longer:
the objective of having refs of an object is to be able to access one very unique instance of an object at any point in the code, so i'd do this: keep only one real instance of the object, by assigning a unique ID to each object
coming from OOP anguage, here is how i m developping my objects around the fact there's no OOP in AppGameKit:



so now, to put my object in an array "by ref", i would simply put its unique ID in the array, and always work on the global array that I like to call the "root" array, the one containing the real object
Qugurun
Valued Member
9
Years of Service
User Offline
Joined: 8th Dec 2014
Playing: AppGameKit
Posted: 8th May 2020 19:24
I think this is a great solution, thanks for sharing.

Login to post a reply

Server time is: 2024-04-19 15:26:54
Your offset time is: 2024-04-19 15:26:54