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 Professional Discussion / Variables not working

Author
Message
super nintendo man
16
Years of Service
User Offline
Joined: 20th Aug 2009
Location:
Posted: 22nd Feb 2011 01:50
At the top of the code for a game I’m making, I’m setting variables like so…

dim gravity(1000)
collbox#=2
playobj#=1
radius#=0
movement#=.5

all looks good so I try to use the variable collbox# in a function.

cd#=intersect object(collbox#,cx#,cy#,cz#,ca#,cy#,cb#)

When I run the game, it exits out and tells me “Object Number illegal.” So I use Print to see the variable and it says 0.

So what I can’t figure out is why it returns 0 when I set it to 2. Please Help
Grog Grueslayer
Valued Member
21
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 22nd Feb 2011 05:35 Edited at: 22nd Feb 2011 05:36
To get variables working within functions you need to set them as GLOBAL otherwise they always start as zero within a function. Arrays are automatically GLOBAL by default.



Stab in the Dark software
Valued Member
23
Years of Service
User Offline
Joined: 12th Dec 2002
Playing: Badges, I don't need no stinkin badges
Posted: 22nd Feb 2011 16:24
When you use the # symbol at the end if your variable, you are declaring it as a float. Your object number should never be a float. A better way would be




[img][/img]


Custom,intel 3.0GHz Hyper Threaded,4GB RAM,ATI X1500 256mb,
WindowsXP DBpro v7.5 Synergy
The coffee is lovely dark and deep,and I have code to write before I sleep.
super nintendo man
16
Years of Service
User Offline
Joined: 20th Aug 2009
Location:
Posted: 23rd Feb 2011 03:58
The global thing actually set off red flags in the compiler.

I tool a closer look at my code and I found the problem. The code calling the variable was inside a custom function since I was testing them out. All I did was replace the function command with a gosub and it worked perfectly.

Also, I have one last question for Mod’s. I’ve been a new member here for roughly 1½ years. How many more posts do I need to be a regular member?
Grog Grueslayer
Valued Member
21
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 23rd Feb 2011 22:44 Edited at: 23rd Feb 2011 22:45
Setting a variable to GLOBAL shouldn't cause any errors. Oh, it's probably because I left the defining values on them... sometimes it works ok sometimes not.

Try this instead:


They generally don't take people out of post approval if you just read messages. Just post normally and eventually you'll get out of the newbie slap. I was out of the newbie slap within a day after I started posting.

IanM
Retired Moderator
23
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 23rd Feb 2011 23:17 Edited at: 23rd Feb 2011 23:17
IIRC, I believe you simply have to assign the correct type of value to avoid the errors:


[edit]Doh! Forgot to add the 'global' keyword

Grog Grueslayer
Valued Member
21
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 24th Feb 2011 00:49
Thanks IanM.

There you go Super Nintendo Man... put those routines back into functions and it'll work the way IanM said.

TheComet
18
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 24th Feb 2011 12:33
Aren't objects only words? Why make them integers? You can save 2 whole bytes:



I suppose you will all now say "doh comet, 2 bytes isn't going to make a difference", and prove me wrong, but I say it DOES make a difference. Of course it's only going to be a few nanoseconds, but if you have a game like mine with 500+ variables, it quickly adds up to be a few microseconds slower. This may sound silly, but I prefer to be as efficient as possible in EVERY aspect, even if it only saves me a few microseconds. Why waste processing power when you don't have to?

TheComet

IanM
Retired Moderator
23
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 24th Feb 2011 14:02
It doesn't make a difference here - DBPro still allocates 4 bytes for each variable, and your x86 processor will access 32 bits at a time.

It MIGHT make a difference if the values were being used in calculations, but I suspect that you lose that in the conversions that DBPro carries out behind the scenes. That's if you can measure the difference in the first place.

TheComet
18
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 24th Feb 2011 16:35
I suspected as much...

My critical thinking on this theme comes from programming microcontrollers, as their resources are very limited.

TheComet

Login to post a reply

Server time is: 2026-07-17 13:57:03
Your offset time is: 2026-07-17 13:57:03