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 / Types and Functions

Author
Message
Reek
13
Years of Service
User Offline
Joined: 21st Aug 2011
Location:
Posted: 23rd Aug 2011 15:20
Hey Guys,
I have a problem with types being passed into a function.
For some reason, the code below is not working..
Am I doing something wrong ?



The above code is not part of the application but an example that illustrates my problem.

If im passing the type variable to the function and performing calculations on one of it's type members it doesn't actually change it. The above example will always output the number 8

Any suggestions ?
LBFN
17
Years of Service
User Offline
Joined: 7th Apr 2007
Location: USA
Posted: 23rd Aug 2011 19:58
Does not even compile for me. You are using Animation.currentFrame in your function. It should be walking.currentFrame.

Why not just declare what the start and end frames are for the player walking instead? Example:


Reek
13
Years of Service
User Offline
Joined: 21st Aug 2011
Location:
Posted: 24th Aug 2011 01:35
@LBFN
I do have a start frame and an end frame. The code I posted was just an example. The reason that I need currentFrame is because I'm going to have a lot of animated objects in the game and I want to keep track of their current frame. The code below will compile but it's still not working ?

LBFN
17
Years of Service
User Offline
Joined: 7th Apr 2007
Location: USA
Posted: 24th Aug 2011 02:44
DBPro can return the object frame easily:


No need to go to all of that trouble.

BTW, you still have listed 'animation.currentFrame ' in the function itself. This is an incorrect usage of the TYPE variable. It should be walking.currentFrame.

WickedX
15
Years of Service
User Offline
Joined: 8th Feb 2009
Location: A Mile High
Posted: 24th Aug 2011 03:33 Edited at: 24th Aug 2011 03:40
Types that are declared in a function are local to that function. Using "animation.currentFrame" in the function is fine. But the value will not change the value in the calling type.



WickedX.
Burning Feet Man
16
Years of Service
User Offline
Joined: 4th Jan 2008
Location: Sydney, Australia
Posted: 24th Aug 2011 13:04
Am I correct in assuming you're wanting a kind of, pointer system here?

If so, I too am interested in what you're trying to do. I know you can use pointers for normal arrays (have actually practices this successfully in my engine), but not too sure if you can do similar tricks for User Types.

Help build an online DarkBASIC Professional help archive.
DarkBasic Help Wikia
LBFN
17
Years of Service
User Offline
Joined: 7th Apr 2007
Location: USA
Posted: 24th Aug 2011 16:46
WickedX:

The question is, Why do it like that? Why not simply just use walking.CurrentFrame? It doesn't make sense to me to do it that way. You are wasting CPU time.

WickedX
15
Years of Service
User Offline
Joined: 8th Feb 2009
Location: A Mile High
Posted: 24th Aug 2011 20:05
LBFN,

I totally agree it’s redundant. Types declared in the programs main body are global so there is really no need to pass them to a function in the first place.

WickedX.
IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 24th Aug 2011 21:12
Huh? I'm not sure that statement makes sense.

Yes, types are global, however instances of those types are not, unless you specifically make them global.

I think the problem here is that Reek is expecting the UDT variables to be passed by reference, not by value as they are. He expects that any changes in the function are reflected in the UDT variable that he originally passed in, and not just on the copy made specifically for the function.

I think you are all talking at cross-purposes here.

Reek
13
Years of Service
User Offline
Joined: 21st Aug 2011
Location:
Posted: 25th Aug 2011 07:10
Thanks for all the replies.

What I'm actually trying to do is pass the reference of the type variable.The code that I posted is just an example that illustrates my problem.In the actual application, the PlayAnimation function is in charge of adjusting the frame rate of any given animated sprite. Since I'm going to have a few animated sprites that I want playing on different frame rates, I decided to write a function that will deal with it instead of rewriting the code every time I'm drawing an animated sprite.

I'm used to programming in c/c++ where I could simple pass on the reference to a pointer but I'm not sure how to do something similar in Darkbasic pro.

I'm sorry if my post wasn't clear enough...
IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 25th Aug 2011 12:00
The functionally equivalent thing to do is to have an array of these things, and to pass an index into the array into your function.



Reek
13
Years of Service
User Offline
Joined: 21st Aug 2011
Location:
Posted: 1st Sep 2011 11:52
@IanM
Thanks a lot! this solution works great for me

Login to post a reply

Server time is: 2024-11-22 17:24:44
Your offset time is: 2024-11-22 17:24:44