Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with 'slimming down' my program
#1
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.
ovaProgramming.

One night I had a dream where I was breaking balls. The next morning, BALLSBREAKER was born.

Quote: Excellent. Now you can have things without paying for them.

BALLSBREAKER 2
~-_-Status Report-_-~
Engine: 94%
Graphics: 95%
Sound: 100%
A Severe Error has crippled BB2 for the time being... I have to figure it out, but until then you won't see much of it Sad.
-----------------------------
Reply
#2
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.
b]Hard Rock[/b]
[The Stars Dev Company] [Metal Qb flopped] [The Terror]
Stop Double Posts!
Whats better? HTML or Variables?
Reply
#3
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.
Reply
#4
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).
i]"I know what you're thinking. Did he fire six shots or only five? Well, to tell you the truth, in all this excitement, I've kinda lost track myself. But being as this is a .44 Magnum ... you've got to ask yourself one question: 'Do I feel lucky?' Well, do ya punk?"[/i] - Dirty Harry
Reply
#5
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:
ovaProgramming.

One night I had a dream where I was breaking balls. The next morning, BALLSBREAKER was born.

Quote: Excellent. Now you can have things without paying for them.

BALLSBREAKER 2
~-_-Status Report-_-~
Engine: 94%
Graphics: 95%
Sound: 100%
A Severe Error has crippled BB2 for the time being... I have to figure it out, but until then you won't see much of it Sad.
-----------------------------
Reply
#6
actually, that code would work fine
Reply
#7
Yes, you can use arrays of strings. However, they look like this:

Item$(n)

not

Item(n)$

*peace*

Meg.
Reply
#8
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.
Reply
#9
oops, Meg is right, I didn't even see that you had the type delineator on the wrong side of the subscript
Reply
#10
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.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)