A way to set all variables at once?

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

Post Reply
Andi
Coder
Posts: 10
Joined: Tue Aug 15, 2006 10:11 pm
Location: Chicago, IL
Contact:

A way to set all variables at once?

Post by Andi » Fri Sep 01, 2006 10:37 am

I'm writing a game. Everytime you die in the game, you are thrown back to the main menu. At the top of the menu code I have lots of line devoted to resetting all my variables to 0. For instance:

Code: Select all

start: CLS
treasurenum=0
revolvernum=0
armsnum=0
bulletnum=0
vinenum=0
This goes on and on for over 50 or 60 variables. Is there a way to simultaneously set all my variables to 0 with one command?

Thanks a lot.
Image

User avatar
sid6.7
Veteran
Posts: 318
Joined: Tue Jun 21, 2005 8:51 am
Location: west USA
Contact:

Post by sid6.7 » Fri Sep 01, 2006 10:59 am

to my "limited" knowledge....NO...

that sure would be nice though wouldnt it? :D

i was thinking you could make 1 varible = all yours
and just reset the 1 varible but it ends up being the
same thing you have to type out all 50+ variables anyways...


maybe you should go thru your varibles and see whats
the same as some of the others and reduce some of them
to 1 variable?

bungytheworm
Veteran
Posts: 288
Joined: Sat Feb 18, 2006 4:02 pm

Post by bungytheworm » Fri Sep 01, 2006 11:18 am

No you cant unless you dont stop and rerun program.
But if you have so many variables to clear at once, maybe you should aproach this one from bit different direction.

Do an array, Weapon()
Weapon(1)
Weapon(2)
and so on.

At start of code, DIM all same variable you have now.
revolvernum as 1
bulletnum as 2
and so on.

Now, if value of revolvernum changes (i assume you use simple "variable = variable + 1") etc.

Weapon(revolvernum) = Weapon(revolvernum) + "value here"

Then when you want to "clear" all values after player has dead.

For Counter = 1 TO UBOUND(Weapon)
Weapon(Counter) = 0
Next Counter

Just a hint how you maybe could make it nicer to read (source)

Alltho, you dont have to DIM revolvernum etc. variables necessarely.

Weapon(1) = Weapon(1) + "value" if you can remember on wich cell of Weapon() array, you had revolvernum (or any other) value.

User avatar
Stoves
Veteran
Posts: 101
Joined: Fri Feb 10, 2006 12:24 am
Location: Nashville, TN

Might want to give CLEAR a try?

Post by Stoves » Fri Sep 01, 2006 11:47 am

┌─────────────────────── HELP: CLEAR Statement Details ────────────────────┤↑├─
│ ◄QuickSCREEN► ◄Details► ◄Example► ◄Contents► ◄Index►
│──────────────────────────────────────────────────────────────────────────────
│Details

│Syntax
│ CLEAR [,,stack]

│The CLEAR statement performs the following actions:

│ ■ Closes all files and releases the file buffers
│ ■ Clears all COMMON variables
│ ■ Sets numeric variables and arrays to zero
│ ■ Sets all string variables to null
│ ■ Reinitializes the stack and, optionally, changes its size

│The stack parameter sets aside stack space for your program.
│QuickBASIC takes the amount of stack space it requires, adds the
│number of bytes specified by stack, and sets the stack size to the
│result.

│ Note: Two commas are used before stack to keep QuickBASIC compatible
│ with BASICA. BASICA included an additional argument that set the
│ size of the data segment. Because QuickBASIC automatically manages
│ the data segment, the first parameter is no longer required.

│If your program has deeply nested subroutines or procedures, or if
│you use recursive procedures, you may want to use a CLEAR statement
│to increase the stack size. You may also want to increase the stack
│size if your procedures have a large number of arguments.

│Clearing the stack destroys the return addresses placed on the stack
│during the execution of a GOSUB. This makes it impossible to execute
│a RETURN statement correctly and produces a "RETURN without GOSUB"
│run-time error message. Using a CLEAR statement in a SUB or FUNCTION
│produces a run-time error message that reads "Illegal function call."

│Differences from BASICA

│BASICA programs using CLEAR may require modification. In BASICA
│programs, any DEF FN functions or data types declared with DEFtype
│statements are lost after a CLEAR statement. In compiled programs,
│this information is not lost because these declarations are fixed at
│compile time.

Andi
Coder
Posts: 10
Joined: Tue Aug 15, 2006 10:11 pm
Location: Chicago, IL
Contact:

Re: Might want to give CLEAR a try?

Post by Andi » Sat Sep 02, 2006 5:08 pm

lurah wrote:No you cant unless you dont stop and rerun program.
But if you have so many variables to clear at once, maybe you should aproach this one from bit different direction.

Do an array, Weapon()
Weapon(1)
Weapon(2)
and so on.
Yeah. This is how I thought I was going to have to do it. However, I noticed that the CLEAR statement seems to do the trick. Thanks for the suggestion though.
Stoves wrote:The CLEAR statement performs the following actions:
?
? ? Closes all files and releases the file buffers
? ? Clears all COMMON variables
? ? Sets numeric variables and arrays to zero
? ? Sets all string variables to null
? ? Reinitializes the stack and, optionally, changes its size
It works like a charm Stoves. Hell yes! Thanks a lot.
Image

Eclipzer
Newbie
Posts: 9
Joined: Sat Jul 08, 2006 7:01 am
Location: USA
Contact:

Re: A way to set all variables at once?

Post by Eclipzer » Sat Sep 02, 2006 9:55 pm

Though the CLEAR statement "works" in your specific case, a generalized method would be as follows.

Code: Select all

' Create a structure for our variables
  TYPE ProgamVars
    var1 as integer
    var2 as integer
    var3 as integer
    ..
    varN as integer
  END TYPE

  DIM SHARED vars AS ProgramVars 'create our variables
  DIM SHARED init AS ProgramVars 'create our init values

' Set our init values
  init.var1 = 0
  init.var2 = 0
  init.var3 = 0
  ..
  init.varN = 0

  vars = init 'copy init values into our variables

The advantage of this setup is that you can define your init values to be anything you want, not just zero. More over, because your variables are contained within a TYPE, they become easier to manipulate. Saving, loading and copying can be done by referencing the TYPEd variable (vars or init), instead of the individual variables that comprise the TYPE.

In my experience, properly designing your code structure is one of the most important parts of creating flexible code that's easy to work with. Normally this means grouping your variables properly using User Defined TYPEs (UDTs) and arrays.

-Eclipzer

Post Reply