Qbasicnews.com

Full Version: Help with 'slimming down' my program
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Hey everybody... I am making an RPG and just recently it is saying something equivalent to: 'out of room' (I can't remember exactly what it said.) So, the help said to break it up into subroutines... well, I have a lot of SUBs, but would adding more SUBs make this help? Also... I know it is possible to DIM multiple variables like this

DIM SHARED flag(10) AS INTEGER

but is there some way to do this:

DIM SHARED item(10) AS STRING

and dim multiple things as strings? Any help would be appreciated.
Quote:DIM SHARED item(10) AS STRING

I just copied + pasted your code and it worked fine. So id assume yes.

Quote:well, I have a lot of SUBs, but would adding more SUBs make this help? Also... I know it is possible to DIM multiple variables like this

If you remove duplicated code by doing so then yes. If not then no.
You can DIM string as follows:

Code:
DIM Text$(1 TO 10)
this will give you variable-length strings.

Code:
DIM Text(1 TO 10) AS STRING * n 'n = number of characters in each string
this will give you set-length strings.

To get rid of the "out of memory" error, you can put code into subroutines, and then put subroutines into modules. There are many tutorials on how to do this. Na_th_an wrote a good one that's floating around this forum someplace. Try the FAQ or doing a search for "modularize" and see what turns up.

*peace*

Meg.
the only thing you can't use varaible-length strings on is in type structures (though you can use fixed length ones).

but yeah, as meg already said, modularize means to split up into files and include them into your program (you need the full version of qb to do this).
So does that mean that this would *not* work?

Code:
DIM SHARED item(10) AS STRING

item(1)$ = "Hat"
item(2)$ = "Sword"
item(3)$ = "NOTHING"

because if that would work I would be able to dramatically downsize the length of some of my code... I am using strings that take up a lot of space...

item1$
item2$
...
item10$

so if this item(n)$ were to work it would make my life easier... thus I could do something like
Code:
for i = 1 to 10
if item(i)$ = "NOTHING" then item(i)$ = "Hat": goto endit
next i
print "There is no open slot..."
endit:
actually, that code would work fine
Yes, you can use arrays of strings. However, they look like this:

Item$(n)

not

Item(n)$

*peace*

Meg.
There are many ways in which you can cut down on the size of your code. I *HIGHLY* recommend putting all item-handling code into subroutines. If you are not sure how to do this, follow this:

Code:
DIM SHARED Inventory$(0 to 9) 'make a 10-item inventory that can be accessed by subroutines

CALL ListInventory 'this will display your inventory on the screen
CALL AddItemToInventory("Hat") 'this will add a hat to your inventory
CALL DropItemFromInventory(3) 'this will drop whatever item is in spot 3 of your inventory.

END 'end the program

'=========== SUBROUTINES GO AFTER PROGRAM ENDING ===========
'==== (or sometimes in their own window, depending on QB version) =====

SUB ListInventory
  PRINT "You are carrying the following items:"
  PRINT "-------------------------------------"
  FOR i = 0 TO 9 'loop through all inventory slots
    PRINT i; " - " + Inventory$(i)
  EXIT i
END SUB

SUB AddItemToInventory (NewItem$)
  spot = -1
  FOR i = 9 TO 0 STEP -1 'find the first open slot in inventory
    IF Inventory$(i) = "" THEN spot = i
  NEXT i

  IF spot = -1 THEN 'no slots were open
    PRINT "You can't carry any more items!  Drop something first!"
  ELSE 'a slot was open
    PRINT "You pick up the item and put it in your pack."
    Inventory$(spot) = NewItem$
  END IF
END SUB

SUB DropItemFromInventory (DropThisNumber)
  IF Inventory$(DropThisNumber) = "" THEN 'nothing to drop!
    PRINT "You aren't carrying an item in that slot!"
  ELSE 'something to drop
    PRINT "You drop the " + Inventory$(DropThisNumber) + "on the gruond."
    Inventory$(DropThisNumber) = ""
  END IF
END SUB

I hope this makes sense and that I don't have any typos/logic errors. Let me know if you have any questions.

*peace*

Meg.
oops, Meg is right, I didn't even see that you had the type delineator on the wrong side of the subscript
Quote:oops, Meg is right, I didn't even see that you had the type delineator on the wrong side of the subscript

Oh my. That comment just made me realize how much of a total dork I am.

*peace*

Meg.
Pages: 1 2