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 Discussion / Returning multiple values in one function

Author
Message
ATBGrafix
19
Years of Service
User Offline
Joined: 8th Mar 2007
Location: Nashville, TN USA
Posted: 9th Mar 2007 14:32
How do you do this in DBC?

I'm creating a breakout'ish game for my high school programming class, and the function that moves the ball needs to return all the position and speed variables.(4 total)

Can I use an array, or is there some command I don't know about?
Roxas
20
Years of Service
User Offline
Joined: 11th Nov 2005
Location: http://forum.thegamecreators.com
Posted: 9th Mar 2007 22:48
Why not use global..

Speed as global
X as global
Y as global
Z as global

But not sure does those exist in dbc but arrays does exist's.. And arrays are global as default When using global variables you dont have to return anything Oh and if dbc has UDT's u could do this too

type BallType
X as float
Y as float
Z as float
Speed as integer
Endtype

Global Ball as balltype

then you could just change values like Ball.X=2456 and you dont need to return anything coz those are globals



Quisco DaLuse
19
Years of Service
User Offline
Joined: 3rd Feb 2007
Location: USA
Posted: 10th Mar 2007 00:24
DBC will not recognize global variables inside a function. Plus, you can pass multiple values to a function, but you can only return one value. I would use an array instead and put in a subroutine using gosub and return. Unfortunately, there is no TYPE command available in DBC either.

You idiots! You've captured their stunt doubles!
Latch
19
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 10th Mar 2007 00:54
Like Quisco said, DBC has no convention to handle GLOBAL variables - variables cannot be declared as GLOBALS. In DBC, the closest thing to a GLOBAL variable, is an array. An array will not lose it's value inside or outside of a function.

If you want to return several values of all the same type (float, string, or integer), you can use a single array in a function and dimension it to the number of indexes you need to store your values.



The conplexity of the function is up to you, but what ever values are assigned to the array will be carried over outside of the function.

If you want to get fancy, you can simulate a TYPE definition with a memblock. That method will allow you to store multiple data types, indexed within the memblock based on the byte size of the data type. In the case of a string it's indexed by byte size*length .

Enjoy your day.

Login to post a reply

Server time is: 2026-07-06 03:59:10
Your offset time is: 2026-07-06 03:59:10