Qbasicnews.com
May 23, 2022, 08:17:05 PM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Back to Qbasicnews.com | QB Online Help | FAQ | Chat | All Basic Code | QB Knowledge Base
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: Help with 'slimming down' my program  (Read 3187 times)
NovaProgramming
Been there, done that
*****
Posts: 1025



« on: May 04, 2003, 10:54:40 AM »

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.
Logged

ovaProgramming.

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

Quote from: "Haye, Phillip J."
 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.
-----------------------------
Hard Rock
I hold this place together
*****
Posts: 775


WWW
« Reply #1 on: May 04, 2003, 02:23:43 PM »

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.
Logged

b]Hard Rock[/b]
[The Stars Dev Company] [Metal Qb flopped] [The Terror]
Stop Double Posts!
Whats better? HTML or Variables?
Meg
Ancient QBer
****
Posts: 483


Yep
« Reply #2 on: May 04, 2003, 06:18:08 PM »

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.
Logged
toonski84
__/--\__
*****
Posts: 2567



« Reply #3 on: May 04, 2003, 06:42:43 PM »

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).
Logged

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
NovaProgramming
Been there, done that
*****
Posts: 1025



« Reply #4 on: May 05, 2003, 01:56:24 PM »

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:
Logged

ovaProgramming.

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

Quote from: "Haye, Phillip J."
 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.
-----------------------------
Aaron
Member
*
Posts: 35


« Reply #5 on: May 05, 2003, 02:54:01 PM »

actually, that code would work fine
Logged
Meg
Ancient QBer
****
Posts: 483


« Reply #6 on: May 05, 2003, 03:23:22 PM »

Yes, you can use arrays of strings.  However, they look like this:

Item$(n)

not

Item(n)$

*peace*

Meg.
Logged
Meg
Ancient QBer
****
Posts: 483


ok
« Reply #7 on: May 05, 2003, 03:38:21 PM »

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.
Logged
Aaron
Member
*
Posts: 35


« Reply #8 on: May 05, 2003, 05:35:16 PM »

oops, Meg is right, I didn't even see that you had the type delineator on the wrong side of the subscript
Logged
Meg
Ancient QBer
****
Posts: 483


omg
« Reply #9 on: May 08, 2003, 12:29:07 AM »

Quote from: "Aaron"
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.
Logged
NovaProgramming
Been there, done that
*****
Posts: 1025



« Reply #10 on: May 08, 2003, 01:58:20 PM »

Thanks everybody.. I think that the item$(n) will work a lot better then doing if item$ = "" then... for item1$-item10$.... yeah, well, thanks!  this will help a lot!
Logged

ovaProgramming.

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

Quote from: "Haye, Phillip J."
 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.
-----------------------------
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!