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.

Author
Message
smerf
19
Years of Service
User Offline
Joined: 24th Feb 2005
Location: nm usa
Posted: 8th Oct 2017 05:49 Edited at: 8th Oct 2017 05:50
Im not really sure how to used functions properly. Ive been using them for years but mostly to organize store and for reusing code. recently been using them for passing information but im not sure what the next step is.


x1=1
y1=1
x2=20
y2=20
do
boxxer(x1,y1,x2,x2)
loop

function boxxer(x1,y1,x2,x2)
box x1,y1,x2,y2
endfunction

this is basically how ive been using them but i know its not right because if i used the function multiple times id end up with a mess of variables, and im pretty sure i should be able to just type in numbers to be passed instead of repeating my vars. some straight forward easy to follow example would be helpfull. thanks
A child's dream never dies.





chafari
Valued Member
17
Years of Service
User Offline
Joined: 2nd May 2006
Location: Canary Islands
Posted: 8th Oct 2017 10:01 Edited at: 8th Oct 2017 10:02
Hi smerf.

This is what i do.

Boxxer(1,20,20,20)

Do

Loop

Function boxxer(n,x,y,z)
Make object box n,x,y,z
Endfunction
I'm not a grumpy grandpa
zero32
7
Years of Service
User Offline
Joined: 28th Jul 2016
Location:
Posted: 8th Oct 2017 12:36
well a function like that doesn't add anything usefull. you just change the name. if you want to change the name you can use #constant

well you could use functions to read the values out of a file or some data section or calculate with the values:


if you want your boxes to use some kind of offset, you could do:

you could also make xoff and yoff a global and change them somewhere else. i use something similar to simulate a window inside of my app, and use the x and y values of that window as an offset for all the window elements.

you could use a function to change the behaviour of a function to match your taste:


maybe you want to make a function to draw a checkerboard pattern:


if your function does not do anything than pass the same values to another dbpro function then a function is not what you want.
an exception to that "rule" could be dll calls.
"It is only slightly easier than changing all sugar in a cake into stevia after it has already been baked" -Bisqwit
smerf
19
Years of Service
User Offline
Joined: 24th Feb 2005
Location: nm usa
Posted: 8th Oct 2017 21:39
Thanks guys. I guess ive been using them right just thought there was more to it then that. and im using them pretty much for the same thing zero for input fileds
Function menu(Input$,x,y,x2,y2,c1,c2,c3,c4,Padding,Padwidth,TextPad).

i see in other languages a function will have a period in between two parts like rotate.Object.ect how can i build a funtino to be called like this instead of filling in the blanks between the commas (, , , ,) thike that
A child's dream never dies.





Sedit
6
Years of Service
User Offline
Joined: 3rd Sep 2017
Location: Ghetto of NJ
Posted: 9th Oct 2017 03:42 Edited at: 9th Oct 2017 03:55
That is OOP(Object Oriented Programming) languages, the periods are pointers for class. What they are for is to call functions from a class which is done as an organizational thing so that various classes have their own private variables that are not shared between other classes or the whole program. It also allows one to make a variable of a specific class hierarchy so that it contains the values needed to make more organized data handling where variables are shared with the parent but not the other children on its same level. Anything higher in the class shares variables and functions of the lower ties but not the other way around unless explicitly told to. Think of them as sort of like an advanced form of the Dim commands for arrays and the Type command for data types. I love OOP langs but some dislike them.

Im sure someone else might be able to explain it better then me but that's the basics for why the dot is there.
zero32
7
Years of Service
User Offline
Joined: 28th Jul 2016
Location:
Posted: 9th Oct 2017 05:36
i know some c++
lets say you have a class called Box:

you now have some variables to save the position and size of the box, on constructor method that will initialize any box and one method to draw that box. methods are functions that belong to a class.
then there is the "main function". a c++ programm will call main(); at the start. if it cannot find that function, the programm will most likely crash:

and that's roughly how it's done in c++.

you can do something similar with dark basic. while you cannot compile a library or define a variable as private, you could do it using the matrix1 commands, building an object from scratch, write some functions to allocate memory to hold your classes and write stuff to memory. i think there was someone who did it with matrix1 commands.
i normally do something like this:
i create a new dba file:

now if i want to use it in one of my programms, i would have to

this is the most basic example i have. i normally add an id field to the type and sort the array by that id. then i add a function that can find that id, which is almost as fast as directly calling the index, because of the sorted array. because if i delete a box, the index of the others can change. calling it by id instead of index is much more stable.

if you are interested, i can append functions to sort the array and to search for an id.
"It is only slightly easier than changing all sugar in a cake into stevia after it has already been baked" -Bisqwit
smerf
19
Years of Service
User Offline
Joined: 24th Feb 2005
Location: nm usa
Posted: 9th Oct 2017 07:28
very complicated there zero beyond my expertise I do get the jest of it though. Gonna stick to regular functions for the moment and avoid the matrixs memory and arrays for the moment.

A child's dream never dies.





Sedit
6
Years of Service
User Offline
Joined: 3rd Sep 2017
Location: Ghetto of NJ
Posted: 9th Oct 2017 07:34
If you added that to your include file are you sure you would have to call _Box in order for it to define? It doesn't compile the data types from include files automatically? In C you would just include something like
#include<box.h>, a header file for all your type and data declarations and function calls ect. IDK Im rusty as hell in it so I am prob missing something.
zero32
7
Years of Service
User Offline
Joined: 28th Jul 2016
Location:
Posted: 9th Oct 2017 09:13 Edited at: 9th Oct 2017 10:48
yeah it will compile types from include files. but all dba files will be merged into one temporary dba file before compiling. first the main dba file and all the other files will be appended to that, so it will never go to the part in the dba file where i dim the array to hold my "object". the gosub is mainly to keep everything neat and clean and packed together so that i do not have to copy anything into my main dba file, while creating a "class like" structure. it's just my way of writing it.

this is also what i use for my video editor (or should i say ffmpeg-gui) for all of my ui stuff.
so i have one ui.dba file, have a gosub_ui and that calls the other gosubs to initialize all my globals and arrays. makes it harder to understand if you are not used to it, but it can keep big projects clean. also, the keyword is big projects. for small projects, this is just too much.
i always start a project, code like there is no tomorrow and then i give myself some time to clean everything up and pack it into that struckture later.
with this, i can read a month old code with out a single comment in an instant.

EDIT: i forgot to mention that i now use Indigo IDE which has an autocomplete feature which is really helpfull with that way of writing code.
the autocomplete feature shows all my arrays and globals and all data types.
"It is only slightly easier than changing all sugar in a cake into stevia after it has already been baked" -Bisqwit
smerf
19
Years of Service
User Offline
Joined: 24th Feb 2005
Location: nm usa
Posted: 10th Oct 2017 04:19
Soo.. i have another question guys.

im wanting the user to be able to click the mouse and make a box using a function
function boxxer(x1,y1,x2,x2)
box x1,y1,x2,y2
endfunction for example
but i want the user to be able to draw more then one box at a time so the function will need to be used more then once at a time one for function call for each box im assuming, but how can i build the program to grow if the user wants to draw 3 boxes and its only set up for 2?
i could use the same function in a loop and just give it the new box data so it would continuously draw both boxes, but thats also something i cant seem to figure out. how do i increment a variable to store new box data? like box1 = x1,y1,x2,y2 but maybey theres 20 boxes or 200 how do i increment box1 one to be box2 by using code and not hard coding it i need to be able to store the new box2 box3 data as well for checking position later and i have no idea how to do that either. would i need an array perhaps to add new boxes and to store their data im not really sure how al this would work can i call a variable thats stored in an arrray or does it have to be called outside ect.
A child's dream never dies.





Sedit
6
Years of Service
User Offline
Joined: 3rd Sep 2017
Location: Ghetto of NJ
Posted: 10th Oct 2017 06:00 Edited at: 10th Oct 2017 07:51
This is how I would go about doing it, atleast a quick and dirty explanation anyway.


// make my UDT ... user defined data type
Type Box
x1 as integer
y1 as integer
x2 as integer
y2 as integer
endtype

// My function that makes it easy to place the gathered data into a single variable. This is so we can create multiple variables that all contain the parameters that we defined in the UDT
Function Boxxer(x1,y1,x2,y2) as Box
Newbox as Box
Newbox.x1 = x1
Newbox.y1 = y1
Newbox.x2 = x2
Newbox.y2 = y2
end function Newbox // Returns the Variable Newbox and places it into the Variable that called the Function as such...

#define box1 as Box
#define box2 as Box etc....

I would make my various Boxes that the user wanted to make by assigning them to the Box variable type

x1,y1,x2,y2 are the variables you assign as the user makes the box, in your case the click points of the mouse. On the second click when the box has all it needs to be created, call the Boxxer function and let it pass on that data to the User defined data type of Box.

user makes a box...
box1= boxxer(x1,y1,x2,y2)
....
.. User does some stuff and makes a second box...
box2= boxxer(x1,y1,x2,y2)

This would place your input values into your variable Box1 and they would be called such as... That dot is how you access the data in the Variable through the data type,
print box1.x1 ; box2.x1 ;
print box1.y1 ; box2.y1 ;
print box1.x2 ; box2.x2 ;
print box1.y2 ; box2.y2 ;

You could even create an array of boxes[10] as data type Box and store the position of each box created that way.

I hope I explained this clear enough to understand. Lord knows my social skills when it comes to describing stuff to people leaves a lot to be desired lol. If truly needed I guess I could write it out in actual code but the issue I have with that right now is I dont even have DBPro to make sure whatever I write out is compiling correctly.


[EDIT]


Sorry missed that part at the end, Yes what I think you need is an Array of

Boxes[Max_boxes] as Box_type.

Can be Called Boxes[Which_Box].x1 etc... etc...

I really don't know if there is a way in Dark basic to append an Array and make it larger then you originally assigned it to be but its a question I need answered right now myself to be honest.
Sedit
6
Years of Service
User Offline
Joined: 3rd Sep 2017
Location: Ghetto of NJ
Posted: 10th Oct 2017 06:07 Edited at: 10th Oct 2017 07:52
...
Sedit
6
Years of Service
User Offline
Joined: 3rd Sep 2017
Location: Ghetto of NJ
Posted: 10th Oct 2017 06:57 Edited at: 10th Oct 2017 07:16
I found an old version of DB on this computer but its not working right and I cant get the following code to compile even though I believe it should.... Its acting like functions of all kinds do not even exist so idk... I gotta go figure this out, so sort of take the following code with a grain of salt until I can figure out whats up with this ancient version of DB I have

smerf
19
Years of Service
User Offline
Joined: 24th Feb 2005
Location: nm usa
Posted: 11th Oct 2017 05:05
thanks a ton sedit i can do alot with that just needed somewhere to start never used types before
A child's dream never dies.





zero32
7
Years of Service
User Offline
Joined: 28th Jul 2016
Location:
Posted: 11th Oct 2017 08:02
Sedit wrote: "function makebox(x1 as integer,y1 as integer,x2 as integer,y2 as integer) as box_type"

sadly, this does not work on dbpro U7.6, but if it did, i would have given you a year worth of pepperidge farm soft baked chocolate chunk dark chocolate brownie cookies.
afaik functions cannot work with UDTs directly. you can't use them as a parameter nor as a return value. so if you use an UDT then use them with globals or with arrays. arrays in dbpro are always global.

Sedit wrote: "I really don't know if there is a way in Dark basic to append an Array and make it larger then you originally assigned it to be"

a little late, but:
add elements to an array with these functions:
array insert at bottom arr()
array insert at top arr()
array insert at element arr(),index
those commands change the current itterator position to the end. so arr() would return the data of the added element.
delete array elements with
array delete element arr(),index
there are other commands too, they are all in the helpfile in the core command section. there is a dbpro wiki page that also contains all those functions, so you do not need the help file if you don't have it.

Sedit wrote: "I found an old version of DB on this computer but its not working right and I cant get the following code to compile even though I believe it should"

https://forum.thegamecreators.com/thread/219455
i posted some ways to get a newer version of dbpro up and running. the last post contains a link to my dbpro folder hosted on mega.nz if everything else fails.
"It is only slightly easier than changing all sugar in a cake into stevia after it has already been baked" -Bisqwit
Sedit
6
Years of Service
User Offline
Joined: 3rd Sep 2017
Location: Ghetto of NJ
Posted: 11th Oct 2017 23:02
[quote=afaik functions cannot work with UDTs directly. you can't use them as a parameter nor as a return value. so if you use an UDT then use them with globals or with arrays. arrays in dbpro are always global.
]

Wow really, I am so sorry then, That's pretty much a deal breaker for me using it much longer then to be honest. I guess Global it is then. Still kind of strange considering older versions of BASIC were able to do it I find it odd someone would have left it out of something relatively new like DBPro.

In that case then Box_Number will need to be global as well as the Array so minor modifications should get you up and running there mate.

ps: Not to late for the Array code that will come in handy.
Ortu
DBPro Master
16
Years of Service
User Offline
Joined: 21st Nov 2007
Location: Austin, TX
Posted: 11th Oct 2017 23:24 Edited at: 11th Oct 2017 23:37
Here:

UDT
extensible UDT list
List iteration
Udt pass to function

http://games.joshkirklin.com/sulium

A single player RPG featuring a branching, player driven storyline of meaningful choices and multiple endings alongside challenging active combat and intelligent AI.
Sedit
6
Years of Service
User Offline
Joined: 3rd Sep 2017
Location: Ghetto of NJ
Posted: 12th Oct 2017 02:45
Ortu I get this error when attempting to compile your code which is exactly what everyone just warned me about in the post above.
"Compilation Failed. User Defined Type Arrays cannot be passed directly into user functions at line 33."


I downloaded your DBPro from that link Zero and fixed my code I posted above. It did have a glitch in that it was automatically seeing it as the second box but now it draws boxes on second click correctly and assigns them to the array. I got it printing the X1 position of the third box in the array so that the OP can see that when the third box is created is adds to the array correctly and displays the x1 value



Ortu
DBPro Master
16
Years of Service
User Offline
Joined: 21st Nov 2007
Location: Austin, TX
Posted: 12th Oct 2017 04:37 Edited at: 12th Oct 2017 04:49
Quote: "Ortu I get this error when attempting to compile your code which is exactly what everyone just warned me about in the post above.
"Compilation Failed. User Defined Type Arrays cannot be passed directly into user functions at line 33."
"


Sorry knocked that one out from my phone and didn't test it.

Here, this one is tested and compiles:



The problem was minor: you can't pass the array element itself, but you can pass a variable that has been assigned the element.
http://games.joshkirklin.com/sulium

A single player RPG featuring a branching, player driven storyline of meaningful choices and multiple endings alongside challenging active combat and intelligent AI.
zero32
7
Years of Service
User Offline
Joined: 28th Jul 2016
Location:
Posted: 12th Oct 2017 13:28
Ortu wrote: "function drawBox(rBox as boxData)"

i didn't know you could pass a UDT variable like that
do you have a way to return a UDT variable without the use of arrays or globals that you want to share? where is your donate button?
"It is only slightly easier than changing all sugar in a cake into stevia after it has already been baked" -Bisqwit
Sedit
6
Years of Service
User Offline
Joined: 3rd Sep 2017
Location: Ghetto of NJ
Posted: 12th Oct 2017 17:13
Not sure I totally understand the question but the Variable that is passed could return data, If you don't want to modify the initial one you could setup a second one function drawBox(rBox as boxData,return as boxData)" like that so that UTD gets returned with it. That's the way I am planning on doing it.
Ortu
DBPro Master
16
Years of Service
User Offline
Joined: 21st Nov 2007
Location: Austin, TX
Posted: 13th Oct 2017 01:10 Edited at: 13th Oct 2017 03:13
unfortunately no, you can't return a UDT from a function.

For that you can either use a global or an array (which are global by default.) ((Assign thier value in the function, no return))

http://games.joshkirklin.com/sulium

A single player RPG featuring a branching, player driven storyline of meaningful choices and multiple endings alongside challenging active combat and intelligent AI.

Login to post a reply

Server time is: 2024-04-26 17:33:33
Your offset time is: 2024-04-26 17:33:33