Qbasicnews.com
May 23, 2022, 09:59:08 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] 2
  Print  
Author Topic: D&D Character Program  (Read 5146 times)
DcDeathscythe
New Member

Posts: 13


« on: May 05, 2003, 05:07:18 PM »

This project I have been working on is for use with d&d roleplaying to help the players keep track of their characters as well as information about other peoples.  I am currently having trouble with the passing of data to variables so that it can be saved.  I am trying to save the types but my attempts have failed in a simple error message of ' Type Mismatch'.  Any help would be greatly appreciated.

TYPE savcharfile
    names AS STRING * 20
    race AS STRING * 10
...
end type
...
DIM savecharacter AS savcharfile
...
savecharfile.name = charactername$
...
SUB charcreate (charactername$, characterplayer$)
...
CLS
PRINT "Saving"
PRINT
INPUT "Enter Name of File: ", file$
... ' This is were I've tried several different things to solve my problem.
OPEN file$ + ".dnd" FOR BINARY AS #1
PUT #1, 1, savecharacter
PUT #1, 2, savestats
PUT #1, 3, saveweapon1
PUT #1, 4, saveweapon2

END SUB
Logged

I am the Great Destroyer"
-Duo Maxwell 'Gundam Wing'
Glenn
I hold this place together
*****
Posts: 786



WWW
« Reply #1 on: May 05, 2003, 05:17:03 PM »

haven't been passed to your subroutine or otherwise defined there.  I don't know what the current variables are in your SUB statement, but you'd need to add the SAVECHARACTER variable:


SUB charcreate (charactername$, characterplayer$, SAVECHARACTER AS SAVCHARFILE))


Then, when you call CHARCREATE, it would be

CALL charcreate (charactername$, characterplayer$, SAVECHARACTER)
Logged

ravelling Curmudgeon
(geocities sites require copying and pasting URLs.)
I liked spam better when it was something that came in a can.
Windows should be defenestrated.
wizardlife
Na_th_an
*****
Posts: 1456


WWW
« Reply #2 on: May 05, 2003, 05:17:09 PM »

You need to use RANDOM file access instead of BINARY. Binary is for accessing the individual bytes of a file, for example in the case of loading a picture or a string of numbers or something... whereas random is when you have a bunch of records that all have equal length.
Logged

Glenn
I hold this place together
*****
Posts: 786



WWW
« Reply #3 on: May 05, 2003, 05:19:30 PM »

want at one time with BINARY files.   (I do it quite regularly.)
Logged

ravelling Curmudgeon
(geocities sites require copying and pasting URLs.)
I liked spam better when it was something that came in a can.
Windows should be defenestrated.
DcDeathscythe
New Member

Posts: 13


« Reply #4 on: May 05, 2003, 05:26:37 PM »

Well... I was going in and associating the variables in the sub routine like so...

savcharfile.names = charactername$

it would take it as valid till the test run...it would say type mismatch.

I want to save several TYPES.  But i didn't know if the sub routine parentesis would be some long line of code or not.  I've been trying to avoid that.
Logged

I am the Great Destroyer"
-Duo Maxwell 'Gundam Wing'
Glenn
I hold this place together
*****
Posts: 786



WWW
« Reply #5 on: May 05, 2003, 05:35:41 PM »

isn't allowed inside your subroutine unless the subroutine knows about the SAVECHARFIL user-defined variable type.  As with other statements, SUB statements can be quite long, certainly longer than 80 characters.  But I try to avoid long lines too.  If that's a big problem for you, you can make your variables global and not have to explicitly pass them to the subroutine by using a COMMON SHARED statement in your MAIN routine (after where you define the types):

COMMON SHARED /MYTYPES/SAVECHARACTER AS SAVECHARFIL [, ..., etc.]

or (if you aren't going to use multiple modules), you can do

DIM SHARED SAVECHARACTER AS SAVECHARFIL[, ..., etc.]

(Either statement replaces your current DIM statement.)

(The "/MYTYPES/" is just a label for the COMMON block.  You can use about whatever name you like.)
Logged

ravelling Curmudgeon
(geocities sites require copying and pasting URLs.)
I liked spam better when it was something that came in a can.
Windows should be defenestrated.
DcDeathscythe
New Member

Posts: 13


« Reply #6 on: May 05, 2003, 05:38:11 PM »

so that should be able to let me pass data back and forth through the variables with out the long coding?  This could be a big help.  I've been at this program for a month and this is the only thing I haven't been able to look through other codings to get an idea of how to do. LOL
Logged

I am the Great Destroyer"
-Duo Maxwell 'Gundam Wing'
Glenn
I hold this place together
*****
Posts: 786



WWW
« Reply #7 on: May 05, 2003, 05:41:46 PM »

routines with the global variables.  (And it might be safer to use the global variables.  Usually, user-defined types pass just fine by explicitly referring to them in the SUB and CALL statements.  However, I've found that QB sometimes screws it up, for no reason that I can find.)
Logged

ravelling Curmudgeon
(geocities sites require copying and pasting URLs.)
I liked spam better when it was something that came in a can.
Windows should be defenestrated.
Meg
Ancient QBer
****
Posts: 483


« Reply #8 on: May 06, 2003, 03:23:18 AM »

Code:
TYPE CharacterType
  CName AS STRING * 20
  Level AS INTEGER
  Experience AS LONG
END TYPE

DIM SHARED PC AS CharacterType, PBlank AS CharacterType

CALL CreateCharacter

END

===============================================

SUB CreateCharacter
  OPEN "players.dat" FOR RANDOM AS #1 LEN = LEN(PC)
    Spot = 0
    DO
      Spot = Spot + 1
      GET #1, Spot, PC
      IF LEFT$(PC.CName) = " " THEN
        DO
          INPUT "Enter Character Name: ", PC.CName
        LOOP UNTIL UCASE$(LEFT$(PC.CName)) >= "A" AND UCASE$(LEFT$(PC.CName)) <= "Z"
        PC.Level = 1
        PC.Experience = 0
        PUT #1, Spot, PC
        EXIT DO
      END IF
    LOOP
  CLOSE #1
END SUB
Logged
DcDeathscythe
New Member

Posts: 13


« Reply #9 on: May 07, 2003, 07:36:31 PM »

the data isn't showing up in the file.  I did the dim shared but the program seems to not be taking the input from the user & storing it.  Its either that or my data isn't being opened properly.  I don't know which right now. What would be a good way of opening the data.
Logged

I am the Great Destroyer"
-Duo Maxwell 'Gundam Wing'
Glenn
I hold this place together
*****
Posts: 786



WWW
« Reply #10 on: May 07, 2003, 09:34:10 PM »

statements, e.g.,

PUT #1, 1, savecharacter
PUT #1, 2, savestats
PUT #1, 3, saveweapon1
PUT #1, 4, saveweapon2

The numbers after the first comma refer to byte positions.  If your variables are more than one-byte long (and they are), you're overwriting data.   Since you're writing the file sequentially, you can just do


PUT #1,, savecharacter
PUT #1,, savestats
PUT #1,, saveweapon1
PUT #1,, saveweapon2


Otherwise, you have to take into account the byte-size of your variables when you specify the position.
Logged

ravelling Curmudgeon
(geocities sites require copying and pasting URLs.)
I liked spam better when it was something that came in a can.
Windows should be defenestrated.
DcDeathscythe
New Member

Posts: 13


« Reply #11 on: May 08, 2003, 02:27:20 PM »

would it make a difference using the input, output or append words instead of the random or binary.
Logged

I am the Great Destroyer"
-Duo Maxwell 'Gundam Wing'
Glenn
I hold this place together
*****
Posts: 786



WWW
« Reply #12 on: May 08, 2003, 02:51:00 PM »

If you want a text file, OUTPUT or APPEND would work.  But BINARY should work too.  You just have to access the file right.  Presuming you're passing everything to the subroutine correctly, i.e., it has the data it needs to write, try the PUT# without the position specifiers and see what happens.
Logged

ravelling Curmudgeon
(geocities sites require copying and pasting URLs.)
I liked spam better when it was something that came in a can.
Windows should be defenestrated.
Meg
Ancient QBer
****
Posts: 483


« Reply #13 on: May 08, 2003, 04:27:08 PM »

... is to use a RANDOM type file and store your information in records (see the TYPE statement).  Then you can open the RANDOM file and specify the record length, for example:

Code:
' create the record TYPE.  let's call it "DataType"

TYPE DataType
  TheirName AS STRING * 10
  TheirAge AS INTEGER
END TYPE

'create array of records, each one looks like the TYPE

DIM Person(1 TO 10) AS DataType

'fill the array with some values, just to see how they work

FOR i = 1 TO 10
  PRINT "Entering data for person "; i
  INPUT "Enter Name: ", TempName$
  INPUT "Enter Age: ", TempAge

  Person(i).Name = TempName$
  Person(i).Age = TempAge
NEXT i

'now that the array is full, save the 10 records to the
'first 10 "spots" in a RANDOM file

OPEN "people.dat" FOR RANDOM AS #1 LEN = LEN(Person)
  FOR i = 1 TO 10
    PUT #1, i, Person(i)
  NEXT i
CLOSE #1

'now let's get the information out of the file and print it to the
'screen.  first we need a place to store the information that's
'the same TYPE as our record.  note that we don't need an array
'for this.  One variable of the TYPE will do:

DIM OnePerson AS DataType

'now we can get the info that we saved to the file.

OPEN "people.dat" FOR RANDOM AS #1 LEN = LEN(Person)
  FOR i = 1 TO 10
    GET #1, i, OnePerson
    PRINT "Information stored in file, location #"; i
    PRINT "Name: "; OnePerson.Name
    PRINT "Age: "; OnePerson.Age
  NEXT i
CLOSE #1


Hope this makes sense!

*peace*

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


BTW
« Reply #14 on: May 08, 2003, 04:35:38 PM »

incidentally, i can see where you're getting the type mismatch error in the code you posted up top.  you're doing the same thing i see a lot of coders do when working with TYPEs.

When you make a data TYPE, you are making a new "kind" of variable, just like an integer, or a string.  It's a type.  You do this with the TYPE statement.

Once you make a type, you have to declare a VARIABLE to be OF that type.  You do this with the DIM statement.

You can then manipulate the variable.

Here's an example:

Code:
TYPE DataType
  FirstName AS STRING * 10
  MiddleInit AS STRING * 1
  LastName AS STRING * 10
END TYPE

DIM FullName AS DataType


See?  Now you have a VARIABLE ("FullName") of TYPE "DataType"

You can manipulate the variable:

Code:
FullName.FirstName = "Marvin"
FullName.MiddleInit = "B"
FullName.LastName = "Edison"
INPUT "Enter First Name: ", FullName.FirstName
IF FullName.MiddleInit = "B" THEN PRINT "Your middle init is B!"


All of these are valid commands.

***THIS IS NOT***

Code:
DataType.FirstName = "Marvin"


***THIS IS NOT***

You can't manipulate the data type like that.  It's like trying to say

Code:
INTEGER = 4


*peace*

Meg.
Logged
Pages: [1] 2
  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!