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 / Accessing custom datatypes in dbpro

Author
Message
Captain Surly
18
Years of Service
User Offline
Joined: 24th Nov 2006
Location:
Posted: 24th Nov 2006 10:05
Hey everyone, first post here... looks like i finally hit a real snag... I use functions in a for, next loop to fill an array of a custom data types in darkbasic pro only, they seem to be resetting to zero in some cases. I've posted the source file below. I want to know how to the stop the data in these fields I've entered from being reset... or whatever... that's what I think is happening. Also, if anyone could help me with getting floating point numbers to play nice with the rest of my program I would greatly appreciate it

Attachments

Login to view attachments
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 24th Nov 2006 10:39
I haven't downloaded the code, but I'm guessing you've declared your variables in the function, which makes them local only to the function. Declare them as globals at the top of your program.



Daemon
18
Years of Service
User Offline
Joined: 16th Dec 2005
Location: Everywhere
Posted: 24th Nov 2006 19:32
Why do you have "spritenumber" as a parameter in some functions when you don't use it in that function? For example: "createspritex" never uses it.

Captain Surly
18
Years of Service
User Offline
Joined: 24th Nov 2006
Location:
Posted: 24th Nov 2006 21:51
Daemon: Heh... thanks for bringing that up, I guess I never realized that

BatVink: Well, I thought I did declare it as global when I typed:
in the beginning. As long as I declared it outside a function, it should be global right? Or is there something funny going on with these custom types that I'm not understanding?
IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 24th Nov 2006 22:36
In the case of arrays, that is true - it's not true for other variable types though.

Captain Surly
18
Years of Service
User Offline
Joined: 24th Nov 2006
Location:
Posted: 24th Nov 2006 23:04
IanM: Well, I believe the word for that is 'doh!' is there an elegant way around this? Or am I going to have to do something like use an array for each sprite property?
IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 24th Nov 2006 23:23
Sure:



You can also use the LOCAL keyword within a function to avoid clashes between locals and globals of the same name.

Captain Surly
18
Years of Service
User Offline
Joined: 24th Nov 2006
Location:
Posted: 25th Nov 2006 00:04
hmm... that sounds like it should work, but the variables within the datatype still seem to be behaving the same... I notice this by the way my sprite only moves at one constant speed as I press each of the arrow keys. I think it should be accelerating.


and this is the main loop


and this is the function that calculates the speed of the sprite


I made it accept a position parameter in case in the future I might want the sprite's speed to be affected by what position it is, such as stopping at the edge of the screen.

The behavior I would've been predicting, is if the variables acted like global ones, is that the sprite would've accelerated to a faster, and faster speed, based on how long the arrow key was pressed, and would've decelerated based on either the arrow key being pressed in the opposite direction, or just no arrow keys being pressed at all. Also I would've predicted that the other sprite would've just traveled at a constant speed diagonally until it was off the screen.

So please let me know what you guys think. Thanks for the assistance.
IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 25th Nov 2006 01:46
First, all of the 'global' commands in the first piece of code are unnecessary - global applies to variables, not types within UDTs, and arrays are already global (although it does no harm to declare arrays as global).

Your final piece of code seems a little confused - try this:


... at least, I think that's what you are trying to do - accelerate left or right if one of those keys are pressed, otherwise decelerate to 0. I'm not sure why the position is being passed though.

Captain Surly
18
Years of Service
User Offline
Joined: 24th Nov 2006
Location:
Posted: 25th Nov 2006 08:36
Okay well, that last bit of code does look a bit messy, but it does function, hah... What I don't quite get is that the playerposition seems to keep updating properly,(as indicated by my sprite actually moving around the screen without getting stuck in one place) but none of the speed variables are behaving as expected.(such as adding more speed based on the previous cycles of the loop)

Login to post a reply

Server time is: 2024-11-26 05:49:57
Your offset time is: 2024-11-26 05:49:57