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 / test for or increment a variable

Author
Message
kdr5000
13
Years of Service
User Offline
Joined: 7th Jan 2011
Location:
Posted: 7th Jan 2011 22:17
I have a quick question which is probably extremely easy but this is literally the third day I\'ve been working with DB Pro and the syntax is not what i\'m used to.

I want to check if a global variable exists in a function. If it doesn\'t, I want to declare it and set its value to 1, and if it does (because that function\'s already been called and the variable created), I want to increment it by 1. This is going into a function which will count the indexes of images and objects as I load them -- I\'ve already got it working if I declare the variables first but I\'d like to do so in the function itself so it\'s self-contained.

The pseudocode would be:


thanks in advance for your help.
IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 8th Jan 2011 00:13
First, why not something like the following:


Alternatively, if you still want to do it like you have in your pseudo code:


That may look a little weird, as the ImageIndexInitialised is used before you declare it, but you need to take into account DBPro's 2-phase variable initialisation.

During the compile, all global variables are allocated space, and are set to either zero for numbers, or the empty string for strings. When you run the program, the values are still set to zero, until the flow of execution actually runs through the variable declaration.

So at program start, ImageIndexInitialised is set to zero. When the program first reaches this line:

the comparison succeeds and passed into the first part of the IF statement.

The next line then sets the variable to 1:


The next time the IF statement is reached, the comparison will fail and drop into the ELSE section of the IF statement.

All variables work in this way, most importantly so do DIM statements - the array doesn't have any storage allocated to it until you run through the DIM statement at runtime.

Hopefully I haven't confused you, but I get the impression you aren't really a total beginner in programming in general

kdr5000
13
Years of Service
User Offline
Joined: 7th Jan 2011
Location:
Posted: 8th Jan 2011 00:41
Thanks, that makes sense. I was under the impression you could only save functions in external .dba files to import and I wanted to package everything for that purpose. I had tried the first method you listed and got it working.

I've got a bit of experience with programming, mostly web stuff though (javascript and a bit of php and perl), and game maker. Which I know sort of doesn't count but whatever

Login to post a reply

Server time is: 2024-11-24 08:36:00
Your offset time is: 2024-11-24 08:36:00