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.

DarkBASIC Professional Discussion / Issue with variable scope, or I am not groking something...

Author
Message
GregA
13
Years of Service
User Offline
Joined: 21st Jun 2011
Location:
Posted: 20th Jan 2014 17:43
I want to so something fairly simple...



So I get to this point, and read through the literature. I cant change the value of s(globaly), because once it is in a function like that, it is merely a copy, not my actual data anymore.

DBPro doesn't seem to be able to pass it by reference, and I don't think I can return a typedef datatype from a function.

So my question/observation is... As my program grows in complexity, this will make the inner(sync) loop very large. Possibly hundreds or even a couple thousand lines. Because of the limited variable scope, many of my variables will necessarily/effectively be global in scope. Additionally, there is little advantage to using typedef in DBPro programs because of variable scope.

Am I understanding that correctly?
BMacZero
19
Years of Service
User Offline
Joined: 30th Dec 2005
Location: E:/ NA / USA
Posted: 20th Jan 2014 18:11
I think everything you said there is correct. What I have done to get around this is to store all Ships, Projectiles, etc in a global array, and pass the array index of the element I want to pass. The index basically functions as a pointer, then, to the actual object. Typedef is an effective way to lump data together in objects and keep it organized, you just have to find the "DBP way" to do things.

ShellfishGames
12
Years of Service
User Offline
Joined: 6th Feb 2013
Location:
Posted: 20th Jan 2014 18:17 Edited at: 20th Jan 2014 18:19
I think there are two major workarounds (that I know of) for the problem you're describing:

1. Use arrays to store your objects (e.g. a Ship-Array in the above case containing all instances of your Ship-type). This way you can sort of create a call by reference scenario by passing the array index to the function.

2. Use a global "return instance" for your types. This would be an example:



So when calling the function, you\'d do this:



..sort of like that. I personally prefer the first solution though.

I hope that kind of answers your question.

GregA
13
Years of Service
User Offline
Joined: 21st Jun 2011
Location:
Posted: 20th Jan 2014 20:28
BMacZero,

Thanks for that! That will work perfectly. Sometimes I get so locked into ways of doing things, the obvious solution eludes me. In my case it is a whole bunch of coding over the years in C/C++ and C#.

Login to post a reply

Server time is: 2025-05-15 19:48:13
Your offset time is: 2025-05-15 19:48:13