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.

Newcomers DBPro Corner / Change the value of an variable in a function

Author
Message
Dark Basic Pro X Collision
17
Years of Service
User Offline
Joined: 20th Sep 2007
Location:
Posted: 9th Jul 2014 01:50
Hi there.

If I have a variable in my main-program that I wish to change inside a function. What is the best way for me to do this?
I have looked into global variables but I can't get it to work.

What I would like to know is if the following programs could be translated to DBPro. If not, maybe someone could be nice and write me an example of how to change a global variable in a function.

In C I would write it like this
void multiplyWithFour(int *a)
{
*a=*a*4;
}
int main()
{
int a = 2;
multiplyWithFour(&a);
printf("%d",a);//This would print 8
}


Does DBPro support pointers?

Or can you maybe return a value from a function?
Like this (In C) :

int multiplyWithFour(int a)
{
return a*4;
}
int main()
{
int a=2;
a=mutiplyWithFour(a);
printf("%d",a);//This line will also print 8
}

Thanks!
Dark Basic Pro X Collision
17
Years of Service
User Offline
Joined: 20th Sep 2007
Location:
Posted: 9th Jul 2014 03:31 Edited at: 9th Jul 2014 04:25
I created a dummy sphere and used the coordinates as global variables. The coordinates can be changed and read from inside the functions. Altough I would prefer to use pointers or a function that returns a value...



I haven't tested the code above, but I think you will get the picture
Dark Basic Pro X Collision
17
Years of Service
User Offline
Joined: 20th Sep 2007
Location:
Posted: 9th Jul 2014 04:39 Edited at: 9th Jul 2014 04:51
Found this:
http://forum.thegamecreators.com/?m=forum_view&t=192789&b=1

And this:
http://forum.thegamecreators.com/?m=forum_view&t=87472&b=7

Perfect!

I should have looked a little more before I posted this thread
WickedX
15
Years of Service
User Offline
Joined: 8th Feb 2009
Location: A Mile High
Posted: 9th Jul 2014 11:14 Edited at: 9th Jul 2014 11:16
To return a single value you can do something like this. You have probable figured this out from TDK's tutorial.



You could also use the make memory function and pass the DWORD address of the memory allocated to the function. But, for returning an objects position or rotation, I would use the 3D-Math vector3 functions. Just pass the vector3 ID to the function. No return parameter required.

Login to post a reply

Server time is: 2024-11-23 13:50:26
Your offset time is: 2024-11-23 13:50:26