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 / Modifying parameters to functions, returning UDTs, getting address of variables, etc.

Author
Message
Diggsey
17
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 27th Nov 2011 00:03 Edited at: 30th Nov 2011 04:18
All of the above are now possible

*IanM's matrix1utils plugin is required*

Add the following code to an include file in your project:


And call "argInit()" at the start of your program. Now you can use it how you want.

Examples

A function to set an integer variable to 42


Multiple arguments (as you can see, calling a set function will return the next argument so it's easy to set multiple arguments):


Skipping arguments (sometimes you only want to set some of the arguments):


The parameter passed into one of these functions must have exactly the right type or nothing will happen (ie. passing a dword to a function which takes an integer and tries to change it will result in the dword not being changed).

These functions should work in all cases except with arrays, which are not yet supported.

Returning UDTs can be acheived by having the calling code pass in the UDT as a parameter, and then the function can call "argSetUDT" with the address of a UDT containing the result. You can get the address of a UDT by getting the address of the first member of the UDT using the "addressOfXXX" functions.

[b]
Chris Tate
DBPro Master
15
Years of Service
User Offline
Joined: 29th Aug 2008
Location: London, England
Posted: 27th Nov 2011 02:33 Edited at: 27th Nov 2011 02:34
Wow. There are no emoticons available to best reflect the expression on my face right now.

This all looks amazingly useful. There are many applications for these kind of operations; such as creating a network packet from a series of parameters without having to specify their names, or using a function to perform operations on delegated variables.

I will be playing around with this.

First question that springs to my mind; assuming that variables must be global; what happens when you call an additional function with the references given in the first? Are the pointers carried through?



Diggsey
17
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 27th Nov 2011 02:45 Edited at: 27th Nov 2011 02:47
Variables don't need to be global. It will work on local variables, global variables and UDT members of local or global variables.

Pointers are not passed through. When you set a parameter value you are setting the value of whatever variable was used by the caller for that parameter. It doesn't change the local copy of the parameter.

You can however use "argGetPtrToXXX()" to get a pointer to the variable a caller used as a parameter, and you can then pass this pointer to another function which can write to it.

[b]
Chris Tate
DBPro Master
15
Years of Service
User Offline
Joined: 29th Aug 2008
Location: London, England
Posted: 27th Nov 2011 14:05
Sweet! I will definately give it a go. It sounds great to be able to reference UDTs and their members.

To work on arrays one could just assign the element to a variable, send the variable to the function, retrieve, then put the variable value into the array.

Nice GUI library by the way; highly recommendable.

Chris Tate
DBPro Master
15
Years of Service
User Offline
Joined: 29th Aug 2008
Location: London, England
Posted: 27th Nov 2011 14:16 Edited at: 27th Nov 2011 14:16
So far when it tries to replace the 'getReturnEDB()' function; the compiler throws a function 'duplicated in the program' error.

The function is also in your GUI library; are they compatible? Is a function name change the solution?

Diggsey
17
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 27th Nov 2011 15:11 Edited at: 27th Nov 2011 15:11
The latest version of TopGui library already contains these functions. It automatically calls argInit from guiInit, so you don't have to add anything. (also it's EBP not EDB)

[b]
Chris Tate
DBPro Master
15
Years of Service
User Offline
Joined: 29th Aug 2008
Location: London, England
Posted: 27th Nov 2011 15:38
Ahaah! Gotya!

Chris Tate
DBPro Master
15
Years of Service
User Offline
Joined: 29th Aug 2008
Location: London, England
Posted: 30th Nov 2011 01:41 Edited at: 30th Nov 2011 01:42
Everything works well so far; but I had trouble working with UDTs at first. I have stored an array UDT in a local variable and have been able to set its properties in the function.

I am able to set integers, floats and inner UDTs; but when I set a string the result differs; when I set the UDT text to "Eddy" the result is " ?T"; when set to "Christopher"; it becomes an empty string.

Have I made a mistake:



Setting regular variable arguments using argSetInteger/String/etc() is much faster than using the argSetUDT() process, but it is still useful for one off tasks.

Diggsey
17
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 30th Nov 2011 03:42
Ah yes, it won't work for UDTs with strings in, I should have spotted that... I should be able to fix it though

[b]
Chris Tate
DBPro Master
15
Years of Service
User Offline
Joined: 29th Aug 2008
Location: London, England
Posted: 30th Nov 2011 04:04
No problem

Diggsey
17
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 30th Nov 2011 04:19 Edited at: 30th Nov 2011 04:26
OK, I've updated the first post with a new version. It does mean that you now have to specify the name of the UDT when using "argSetUDT" but that shouldn't be a problem.

edit:
Updated TopGui with the new version too (revision 50)

[b]
Chris Tate
DBPro Master
15
Years of Service
User Offline
Joined: 29th Aug 2008
Location: London, England
Posted: 30th Nov 2011 21:44
That was quick. Will update. Thanks.

Chris Tate
DBPro Master
15
Years of Service
User Offline
Joined: 29th Aug 2008
Location: London, England
Posted: 11th Jan 2012 21:14
Here's a snippet link for anyone who wants examples of how you could use this library.

This example simply toggles values or cyclings through values in a range.

Benjamin
21
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 13th Jan 2012 06:05
I wrote something similar, albeit not as involved, some time ago.

Nice one.



Support a charitable indie game project!

Login to post a reply

Server time is: 2024-03-28 13:25:36
Your offset time is: 2024-03-28 13:25:36