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 / passing references around besides in function calls

Author
Message
tmu
7
Years of Service
User Offline
Joined: 2nd Feb 2017
Location:
Posted: 7th Aug 2017 21:00
Is there some way to use type references in types/arrays? AppGameKit seems to always copy and I cannot figure out how to make it use references.

2 examples:




In case 1, typeRefTest() does not change the values in the original "thetop" variable, because searchTheTopForBiggerThan() results are copies and not references.

In case 2, typeRefTest2() does not change the "thetop" even though the same type object was added to both "thetop" and "thetop2".

Any way to make these work so the changes are made as references ?
Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 7th Aug 2017 23:18 Edited at: 7th Aug 2017 23:25
example, you need ref and with [] brackets it is an array.
and i believe you can not pass a simple integer with ref but inside a user defined type you can pass the own struct with reference.
function func2( a ref as point ) // pass by reference, variable is the original
a.x = 7 // this change modifies the original variable
endfunction
function func2( a ref as integer[] ) // pass by reference, variable is the original
a[0] = 7 // this change modifies the original variable
endfunction
https://www.appgamekit.com/documentation/guides/12_array_changes.htm

if you use = it just make a copy!
AGK (Steam) V2017.07.19 : Windows 10 Pro 64 Bit : AMD (17.4.4) Radeon R7 265 : Mac mini OS Sierra (10.12.2)
tmu
7
Years of Service
User Offline
Joined: 2nd Feb 2017
Location:
Posted: 8th Aug 2017 06:09
Thanks @Markus. But those are examples of modifying the data in a user defined type passed as an argument to a function. This is the only case I already had figured how to do.

But in my two examples above I have something different.

For example, from my example code above:

searchTheTopForBiggerThan(limit) <-- can I return references from this and not copies? Not pass references in to the function but return them back (out)?

typeRefTest2() <-- can I put the same type (object) instance into two arrays so they both reference the same? So if I change thetop.nested[0], also thetop2.nested[0] will be changed? Where thetop and thetop2 are global type variables and "nested" is an array in that type. See the code above for details..?



Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 8th Aug 2017 07:23
em, i think you need a index list. not oop

AGK (Steam) V2017.07.19 : Windows 10 Pro 64 Bit : AMD (17.4.4) Radeon R7 265 : Mac mini OS Sierra (10.12.2)
GarBenjamin
AGK Developer
7
Years of Service
User Offline
Joined: 30th Nov 2016
Location: USA
Posted: 9th Aug 2017 02:40
I'm not exactly sure what you are wanting to do but if you are trying to return a ref to a variable (of any kind) created inside a function I don't think you could do that any language I am familiar with because once the function returns that variable would be out of scope... no longer exist.
TI/994a (BASIC) -> C64 (BASIC/PASCAL/ASM/Others) -> Amiga (AMOS/BLITZ/ASM/C/Gamesmith) -> DOS (C/C++/Allegro) -> Windows (C++/C#/Monkey X/GL Basic/Unity/Others)
Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 9th Aug 2017 07:37 Edited at: 9th Aug 2017 07:39
@GarBenjamin
c# is oop, you can find a object, memory it in a local variable return it you you can access the object outside.
the object is valid anywhere and will be deleted if it is not is use by garbage collector.
yes the variable is in a scope but she contains only a reference, if you remove this variavle is original still exist.

https://en.wikipedia.org/wiki/Garbage_collection_(computer_science)
AGK (Steam) V2017.07.19 : Windows 10 Pro 64 Bit : AMD (17.4.4) Radeon R7 265 : Mac mini OS Sierra (10.12.2)
GarBenjamin
AGK Developer
7
Years of Service
User Offline
Joined: 30th Nov 2016
Location: USA
Posted: 10th Aug 2017 06:22
@Markus that is interesting. I've used C# since around 2003 and never tried such a thing. Always figured as soon as the method ended any variables declared in it were invalid to use and marked for deletion. This is my you learn something new everyday for today. Lol
TI/994a (BASIC) -> C64 (BASIC/PASCAL/ASM/Others) -> Amiga (AMOS/BLITZ/ASM/C/Gamesmith) -> DOS (C/C++/Allegro) -> Windows (C++/C#/Monkey X/GL Basic/Unity/Others)
Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 10th Aug 2017 09:17 Edited at: 10th Aug 2017 09:18
@GarBenjamin yes, the life is full of surprises
C# a Form with Button Click and Function.


C# Class with public variable
AGK (Steam) V2017.07.19 : Windows 10 Pro 64 Bit : AMD (17.4.4) Radeon R7 265 : Mac mini OS Sierra (10.12.2)
tmu
7
Years of Service
User Offline
Joined: 2nd Feb 2017
Location:
Posted: 10th Aug 2017 17:08
Thanks for the thoughts guys.

What I was trying to do was to search a list for specific set of items, collect this subset to a list, and return that list. Then perform specific actions on that returned subset, with the subset being references. So I could then just update the subset and the main list it was collected from would update as well.

I wanted to put this in a function of its own as I need the same subset in different places. I ended up copy-pasting the code to create the subset in several places.

Traverse a graph, collect some path based on given criteria. Return that path. Have updates to returned set/path also directly applied to the graph itself.

Not quite sure where you guys headed with the variable returns stuff. I pass in a list, pull in subset from that list, return that subset. I don't know a programming language where you cannot return a variable from a function/method? Well I guess you have some C# magic there, never did that myself..


GarBenjamin
AGK Developer
7
Years of Service
User Offline
Joined: 30th Nov 2016
Location: USA
Posted: 10th Aug 2017 20:53 Edited at: 10th Aug 2017 20:53
Yeah today reading the above I am trying fo figure out what I was thinking myself. Because we're always doing this kind of thing. Lol

I knew my mind was getting game dev / programming burnout but didn't realize it was that fried. Lol I am sure I was thinking of something just I'll need to figure out what that something was.
TI/994a (BASIC) -> C64 (BASIC/PASCAL/ASM/Others) -> Amiga (AMOS/BLITZ/ASM/C/Gamesmith) -> DOS (C/C++/Allegro) -> Windows (C++/C#/Monkey X/GL Basic/Unity/Others)
tmu
7
Years of Service
User Offline
Joined: 2nd Feb 2017
Location:
Posted: 11th Aug 2017 20:42
LOL, it happens. Still it is nice that people participate.

I finally figured a keyword from Markus's post could work. So I would not return the type objects from the function but indices to those type objects in the array. This way I could then modify the actual graph array itself by using the indices. Maybe the ultimate answer would have been a fully optimized, customized data structure allowing all my operations on it directly and easily. Of course, this never happens in practice.

Well, my copy-paste solution works for now but good to know these hacks the next time..
Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 12th Aug 2017 08:16 Edited at: 12th Aug 2017 08:33
a problem with indices is if you modify the array insert or delete entry your id's are invalid, please keep in mind.

i made a feature request.
AGK (Steam) V2017.07.19 : Windows 10 Pro 64 Bit : AMD (17.4.4) Radeon R7 265 : Mac mini OS Sierra (10.12.2)
tmu
7
Years of Service
User Offline
Joined: 2nd Feb 2017
Location:
Posted: 13th Aug 2017 12:31
thanks. that is very useful.

Login to post a reply

Server time is: 2024-11-24 09:51:36
Your offset time is: 2024-11-24 09:51:36