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 / type references local vs global scope

Author
Message
tmu
7
Years of Service
User Offline
Joined: 2nd Feb 2017
Location:
Posted: 2nd Apr 2017 12:37 Edited at: 2nd Apr 2017 12:40
I define custom types, create global variables defining instances these, and in a local function I create new references to these types and try to set these local references to point to the global references. Updating the local variables does not seem to update the global variable. Is there some way to create a new reference to a global variable in function scope?

example:



I was expecting this to say "100, 100". But it says "0,100". I guess x=y for types makes a copy or something? For function calls I can use the "ref" keyword, is there some way to do the same in this example case (localGX=gX1)?
GameDevGuy
9
Years of Service
User Offline
Joined: 18th Jul 2015
Location:
Posted: 2nd Apr 2017 13:26
As you have found variables do not work the way you are suggesting.

In this code:



you are thinking that this line:

localGX = gX1

somehow makes localGX becomes a "pointer" to gX1, and so in other words anything assigned to localGX gets assigned to gX1.

But what only happens is that the value of gX1.x is put into the local variable localGX.x

Nothing is getting put into the global gX1 and the local variable localGX is gone after niceFunction is finished.

Without knowing the big picture of the purpose of the code, this may not be what you want, but is the following of any use?

Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 2nd Apr 2017 14:51 Edited at: 2nd Apr 2017 14:52
you can give variables with userdefined type with the ref keyword into a function.
if u using = it will make a copy (one struct copied to another).
AGK (Steam) V2017.02.28 : Windows 10 Pro 64 Bit : AMD (17.2.1) Radeon R7 265 : Mac mini OS Sierra (10.12.2)
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 2nd Apr 2017 16:54
If you want the effects on localGX to appear outside the function, pass it into the function as a reference, or return localGX and assign it to a variable when the function is called.


or:




"I like offending people, because I think people who get offended should be offended." - Linus Torvalds
tmu
7
Years of Service
User Offline
Joined: 2nd Feb 2017
Location:
Posted: 2nd Apr 2017 18:51
OK thanks all for the answers. I was hoping for a way to make it work without splitting stuff into more and more small functions. Especially since I don't see a way to scope functions.

I will just try to live with it, either by repeating the code multiple times in the if branches, or splitting everything to multiple functions..
Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 2nd Apr 2017 22:17
its more like this

if myCondition = 1
niceFunction(gX1)
else
niceFunction(gX2)
endif

function niceFunction(gX ref as MyType)
gX.x = 100
endfunction
AGK (Steam) V2017.02.28 : Windows 10 Pro 64 Bit : AMD (17.2.1) Radeon R7 265 : Mac mini OS Sierra (10.12.2)
tmu
7
Years of Service
User Offline
Joined: 2nd Feb 2017
Location:
Posted: 3rd Apr 2017 17:12
Thanks Markus for the concrete example.

That is what I figured it should be like, I just have too many functions and don't know of a way to nicely scope them so I was hoping to avoid littering with more little functions.

But I'll manage...

Login to post a reply

Server time is: 2024-11-24 17:03:09
Your offset time is: 2024-11-24 17:03:09