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 / Pointers, Structures, and Functions...DBPro - Help Please

Author
Message
timmayj
18
Years of Service
User Offline
Joined: 10th May 2006
Location:
Posted: 11th May 2006 06:19
Hi - I posted this before, but It never showed up on the forum. I dont have the message that it still being reviewed anymore, so Ill risk posting again.

Basically, what Im trying to do is write a function that can act on ANY type of object_info. The problem is, I need to POINT to the type that I am calling movement on. As it stands, the variables of the type wont change, rendering the function useless. Any help with this would be appreciated.

PS:A simple tutorial on POINTERS in Dark Basic Pro would help a lot.

Quote: "
type object_info
object `object number
accel `rate of acceleration
topspeed `top speed
brake `brake rate
turn `turn rate
coast `slow without breaking\accelerating
speed `current speed
reverse `max reverse speed
left `left turn value
right `right turn value
go `movement
endtype

movement(*player)

FUNCTION movement(player as object_info)
if player.right then turn object right player.object,player.turn
if player.left then turn object left player.object,player.turn

if player.go=1 then player.speed=player.speed+player.accel
if player.go=0 and player.speed>0 then player.speed=player.speed-player.coast
if player.go=0 and player.speed<0 then player.speed=player.speed+player.coast
if player.go=-1 then player.speed=player.speed-player.brake
if player.speed>player.topspeed then player.speed=player.topspeed
if player.speed<player.reverse then player.speed=player.reverse

move object player.object,player.speed
ENDFUNCTION"


http://timmayj.cjb.net/
indi
22
Years of Service
User Offline
Joined: 26th Aug 2002
Location: Earth, Brisbane, Australia
Posted: 11th May 2006 06:52
When you join this forum, there is a peroid of time when your posts wont be visible to all users until scanned by a moderator.

One post is sufficient next time, just be patient, we will flag them visible and in time you will lose this newb status.

If no-one gives your an answer to a question you have asked, consider:- Is your question clear.- Did you ask nicely.- Are you showing any effort to solve the problem yourself &#63743;
timmayj
18
Years of Service
User Offline
Joined: 10th May 2006
Location:
Posted: 13th May 2006 00:47
*BUMP* + info...

The problem with the original code is that the commands in the function body arent changing the variable values in the type being passed to it. As it stands, the function is creating a completely separate TYPE of object_info, and operating on that.

What I need is a way for my function to change the actual VALUES of the variables owned by the TYPE that I pass into the function. In C++ I can use pointers for this, but in Dark Basic PRO Im unsure of the proper syntax... I have tried using the '*' character in the prototype, but found that it then points to the ACTUAL type 'player' and NOT the type I am passing to the function:

Quote: "
movement(square)

FUNCTION movement(*player as object_info)
if player.right then turn object right player.object,player.turn
if player.left then turn object left player.object,player.turn

if player.go=1 then player.speed=player.speed+player.accel
if player.go=0 and player.speed>0 then player.speed=player.speed-player.coast
if player.go=0 and player.speed<0 then player.speed=player.speed+player.coast
if player.go=-1 then player.speed=player.speed-player.brake
if player.speed>player.topspeed then player.speed=player.topspeed
if player.speed<player.reverse then player.speed=player.reverse

move object player.object,player.speed
ENDFUNCTION

//THIS function code (even though Im calling it on 'square') operates //directly on 'player' -- not the intended result!
"


Again, I need pointers to make this function UNIVERSAL, so it could be effectively used as follows:

Quote: "
movement(square)
movement(triangle)
movement(cube)
movement(player)
movement(player2)
movement(enemy)
"


I hope that makes my problem more clear -- I have a hard time putting these things to words.

Any help would be greatly appreciated!

http://timmayj.cjb.net/
IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 13th May 2006 02:33
DBPro doesn't have pointers, can't pass by reference, and can't return a UDT, doesn't have inheritance/polymorthism ... which make things a little difficult, although not impossible.

I've just posted a piece of code here that updates a UDT from a function - take a look.

You can also do something like this with global arrays too, passing around array indexes rather than using global UDT variables.

For free Plug-ins and source code http://www.matrix1.demon.co.uk
timmayj
18
Years of Service
User Offline
Joined: 10th May 2006
Location:
Posted: 13th May 2006 14:16
Thanks, thats very helpful =D

http://timmayj.cjb.net/

Login to post a reply

Server time is: 2024-11-26 21:33:19
Your offset time is: 2024-11-26 21:33:19