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 / How can I pass a user defined type to a function.

Author
Message
Jester1958
12
Years of Service
User Offline
Joined: 22nd Feb 2014
Location:
Posted: 22nd Feb 2014 23:14
I am writing a game program and I need to pass user defined types to a function. However, it seems that DB Pro will only allow you to pass Base types (ie. Integers, strings, Real, etc.). Can anybody tell me how to work around this problem?

The First, The Original, The only True CYBER_JESTER!
WickedX
17
Years of Service
User Offline
Joined: 8th Feb 2009
Location: A Mile High
Posted: 23rd Feb 2014 06:10
Dark Basic Professional will allow passing udt to a function. Although in the function you are not allowed to alter the udt outside of the functions local copy. To solve this, you can make the udt global or make it a udt array, either way you no longer even need to pass it to the function as a parameter.
Phaelax
DBPro Master
23
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 23rd Feb 2014 07:23
You pass it the same way as you do any other primitive data type, but you must define the function parameters.



ShellfishGames
13
Years of Service
User Offline
Joined: 6th Feb 2013
Location:
Posted: 23rd Feb 2014 11:58
Quote: "You pass it the same way as you do any other primitive data type, but you must define the function parameters."


Indeed, but as WickedX said this will only give the function access to a local copy of the udt variable. So you can read its content, but not alter the variable originally passed to the function. I'm also not sure about performance, if DBPro really copies all the values defined in the UDT it might get rather slow for complex types.

I personally prefer the method of storing my udt variables within arrays (such as "dim Unit(UnitCount) as Unit_Type") and pass the array ID to any function that is meant to manipulate the variable.

tiffer
20
Years of Service
User Offline
Joined: 6th Apr 2006
Location: Scotland
Posted: 24th Feb 2014 02:32
IMO you wouldn't. If it's that sort of task that shares variables sterhouldn't it be a subroutine? Functions are designed to be called interchangeably so giving them access to variables could pose problems.

Cwatson
Phaelax
DBPro Master
23
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 24th Feb 2014 05:13
Quote: "Indeed, but as WickedX said this will only give the function access to a local copy of the udt variable."

It's the same for passing any other variable, he never said he needed to pass it by reference.

Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 24th Feb 2014 11:33
Quote: "It's the same for passing any other variable, he never said he needed to pass it by reference."


Indeed.



Powered by Free Banners
Jester1958
12
Years of Service
User Offline
Joined: 22nd Feb 2014
Location:
Posted: 1st Mar 2014 16:14
Thank you VERY much. I will let you know if this solves the problem.
BTW - The function does need to change the value of certain fields in the UDT, and then pass these new values onto a validation function to see if they are allowed.

The First, The Original, The only True CYBER_JESTER!
Mobiius
Valued Member
23
Years of Service
User Offline
Joined: 27th Feb 2003
Location: The Cold North
Posted: 1st Mar 2014 22:31
UDTs are typed arrays, why not simply pass the index to the array and use it that way?

This would be an example...



Phaelax
DBPro Master
23
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 1st Mar 2014 23:06
A UDT doesn't have to be an array. I've done so many times as just a better way to organize variables for a particular system.

But in this case, if he is using an array then yes I agree passing the index would be the best choice.

Login to post a reply

Server time is: 2026-07-07 01:53:39
Your offset time is: 2026-07-07 01:53:39