Posted: 8th Jun 2007 0:11
Hey, I just recently started working with DBPro, just for something to do. On my spare time I work on small stuff. Today I was bored and decided to attempt to make a scripting system similar to FPSC's. I don't mean to sound like an overambitious noob though. I've been workin' with this for about a half an hour with no luck. I got some luck with using the LEFT$ and RIGHT$ commands, but that meant all of the scripting commands would have to be a certain length.

I want the script to look something like this:

+ Code Snippet
Text "Hello World"


or

+ Code Snippet
Text : "Hello World"


I've been trying to get the first one, but it's not working. I'm trying to get it so that when it reads a script file and sees that, it prints the text either using PRINT or TEXT.

What I have right now is just a bunch of garbage. I know that you need to have the program open the file to read, but which READ command would I use on this? I looked through the FPSC source, and I saw that all of the FPSC scripting commands were locating in a subroutine, but I'm not sure why they are there and how the whole reading thing goes.

Sorry for asking such a lame question, but I can't really find an easy way to learn DBPro. Thanks if you can help.
Posted: 8th Jun 2007 2:29
Use the read byte command. The character you are attempting to use is a special character. Using ASCII text commands on it is somewhat confusing, so treat it as binary. The value is 34, or 22h for the character you refer to!
Posted: 8th Jun 2007 4:18
The thing is, I tried the read byte command, but it only allows me to store what it read in a variable. It won't let me make a string out of it. Is there something I am missing here?
Posted: 8th Jun 2007 4:24
The 'Str$' command converts a int/float to a string and 'Val' does the opposite, if you want to make a simple script that just prints values and such then you can quite easily just scan through the string for a command like 'print' then scan after it for the string to output.
Posted: 8th Jun 2007 8:50
You just read the byte into a byte variable, say b for example and it is chr$(b). That is a way to make a string from it.

Generally speaking, delimiters that are also delimiters in the system should be dealt with in this manner. The functions you are missing are chr$ and asc. They can be used to circumvent the limitations you are finding using the traditional string based method.

The functions dc mentioned are also good, but str$(34) returns 34, not the quote character.
Posted: 8th Jun 2007 15:47
Um, CHR$(34) will return the quote character. STR$(34) will turn the number 34 into a character string holding the digits "3" and "4". Just thought I'd clear that up.
Posted: 8th Jun 2007 17:36
Read the scripting tuts for QB.
http://www.petesqbsite.com/sections/tutorials/gamedesign.shtml#4

Language is same, i think that you even dont need to modify anything to make them work in DB.

Try..
Posted: 9th Jun 2007 22:57
I tried both STR$ and CHR$. Neither of them worked.
Posted: 10th Jun 2007 9:20
I think it would help this process along immensely if you posted some code, because I am not understanding why you are even trying to use str$(). That is for converting numbers into text strings. The quotation character is not a number; it is a character whose value happens to be 34. You are asking how to work with the quotation characer; how to make a string from the raw byte. That byte is 34. To read it from a file, you should not be using text commands, like read string, for example. You read the file as bytes. To make an ASCII string from a sequence of bytes, you use the function chr$(n). You have said that this does not work, and I am left wondering why. I also mentioned the function call asc(str), which is the reciprocal of chr$(n).

So, it occurs to me that a snippet is in order. I posted one a few weeks, or maybe months ago. Hang on, Ill go look for it.

EDIT: Couldn't run it down, it may be in Newcomers, but here is a function that builds a DBPro string from raw bytes, like I am (not successfully) trying to show you. I hope it makes it a little more clear. This code limits the input stream to the range 32-127, which is a normal thing. Characters below space (32) are terminal control characters, like carriage return, line feed, form feed, backspace, tab, and many others...like the bell character...chr$(7). Those above 127 are delete, and there are some IBM line drawing characters, etc. but they are not defined for ASCII, and do not have a function in text files.

+ Code Snippet
function jzMakeDBString2(ptr as dword)
    local i as integer = 0
    local databyte as byte
    local buildstr as string = ""

    databyte = *ptr
    while databyte > 31 and databyte < 128 and i < 32768
        buildstr = buildstr + chr$(int(databyte))
        inc ptr, 1
        inc i, 1
        databyte = *ptr
    endwhile
endfunction buildstr
Posted: 25th Jun 2007 9:16
I realize this is an old thread, but it's mine, and I haven't gotten around to looking at it and trying this.

Jinzai,

Thanks for your help, but I'm really not understanding that snippet. I have a couple of questions.

1. What does "ptr as dword" mean?
2. What is "dword"? I have seen it before, but I don't know what it is.
3. What does "local as integer = 0" do?
4. What does "local databyte as byte" do?
5. What does "local buildstr as string = """ mean?
6. What does "databyte = *ptr" mean?
7. What does "buildstr = buildstr +chr$(int(databyte))" mean?
8. Why are you increasing "ptr" and "i"?
9. What is "buildstr"?
10. What is "chr$"? (I can't find it in the DBPro Assistant thing)
11. How would I implement this into my code? How would I read and print the text in something such as this:

+ Code Snippet
Text=Hello World


I know I pretty much asked about every last thing in the snippet, but hey, we all have to learn. Thanks, if you, or somebody else, could explain this to me.
Posted: 25th Jun 2007 9:38
1. It makes a new variable named 'ptr' as a 'dword', leading me to...
2. A Dword is a variable type that generally stores large integers. It is the equivilent of 4 bytes
3. Cause an error - but I assume you mean local i as integer, which creates a variable named 'i' as an integer number (it can't be a decimal)
4. Makes a new local (meaning it exists only in the function where it is declared) variable called 'databyte' as a 'byte', meaning it can be no greater than 255.
5. Makes a new local variable named 'buildstr' as a string and sets it equal to the double quote character - though I believe you mistyped it, jinzai's code makes it blank (buildstr = " ")
6. It sets the variable 'databyte' as equal to the memory address at which the value of the variable 'ptr' is being stored, I believe
7. Sets 'buildstr' equal to itself plus the ASCII (character string) value of the integer value of whatever databyte is equal to.
...
10. It returns the character represented by a given number. For instance, chr$(34) returns the double quote character because 34 is the number corresponding to the double quote character.

and the rest are directly at jinzai. Sorry if you didn't want me answering them jinzai, I didn't mean to be intrusive.
Posted: 25th Jun 2007 11:40
do you really have to use the quotes in your script? It is pretty simple to get a line of text out of a file in a well formatted script. For example, instead of searching for the quotes, you could just format your script so say the first 10 characters are a command, the next 10 characters are some text, etc. Then you just loop through the strings and use left or right to get your values.
Something like this:
+ Code Snippet
test_script()

do

loop

function test_script()

file_string as string
a = 0
open to read 1,"script.txt"

Repeat
   read string 1,file_string
   inc a,1
Until file end (1)
close file 1
open to read 1,"E:DevProjectsDemoscript.txt"
Dim store_strings(a) as string
For i = 1 to a
   read string 1,store_strings(i)
next i
close file 1

for i = 1 to a
   Print Left$(store_strings(i),10)
   Print Right$(store_strings(i),18)
next 1

endfunction


there is a .txt file attatched to try this out.
Posted: 25th Jun 2007 13:13
That makes for very limited systems, though. You have to restrict everything to 10 characters. What if you want to print a line more than 10 chars long?
Posted: 25th Jun 2007 18:53
Then you just format the script file for longer lines and change the left/right values accordingly.
Posted: 25th Jun 2007 21:51
@ThinkDigital - I don't mind at all. I do what I do for the same reason you posted what you did. You were here; I was elsewhere. Thanks for taking up my slack!

@WindowsKiller - That actually compiled? I would not have thought it possible.
if quote_open = 0 then quote_open = x else quote_close = x

@Inspire - I think that there are some things that need to be clarified a little more. The first thing is that, in the editor, when you are programming, you need the quotes to tell DBPro...this is a text string. For example, this does not do anything:
+ Code Snippet
Text=Hello World

but this does:
+ Code Snippet
Text="Hello World"

assuming that Text is a string variable.

Next, the local keyword is very important in functions. Functions are better if they are self-contained and modular. That way, you have flexibility in using them. Local variables are stored on the stack, and can only be seen in the function. If you have a global named i, and a local named i - the function only "sees" the local i, they are different variables, although they have the same name. Local guarantees I am using the one that is in the function. That is very important as it is quite likely that someone will already have a variable named i, and I don't want to trash its value, I want to use "my" i.

This is alot coming at you at once. Pointers are not really BASIC, they are C. Pointers are addresses, not the variables but the address of the variable. They are dwords because they are 32 bits. Integers are signed, and are inappropriate for use as pointers. This is how you get the variable's value:

+ Code Snippet
value = *ptr


The * is called the indirection operator. It is not multiplication in this case, its pointer indirection. It means simply, "the thing pointed to by ptr". Indirection looks at the left side of the equation (called the l-value), and since databyte is a byte, it takes the address (ptr), and takes a byte from there, and assigns that to databyte. Why do it this way? Because, if you ask DBPro to assign one string to another, it will ignore the first quote character. That is normal, because use you need it to ignore them when you try to do this:

+ Code Snippet
str = "This is my string."


To make a string with quotes in it, you need to hide them a little, as a result of their use. Like I said above, they are a reserved character in any string based system. (In C/C++, too.)

+ Code Snippet
global str1 as string = "This is a string."
global str2 as string


print str1
str2 = chr$(34) + str1 + chr$(34)
print str2


You cannot, for example do this:

+ Code Snippet
global str1 as string = "This is a string."
global str2 as string


print str1
str2 = " + str1 + "
print str2


Or any form of that.

In my example, which btw is to take a C-style string and make a DBPro string from it, I use the variable i because I am using a control structure (while/endwhile) without a terminating condition. I want to limit the string to 32768 characters because BASIC usually limits strings to that length. The function has no idea how long the string is; i just limits that to 32768. ptr gets incremented because I want to get the next byte. buildstr is a string, and it is the string being built, and also it is the return from the function call. I initialize it to nothing, which is called a NULL string. It is simply an empty string, represented by "". Technically, a single byte of value 0 is what a NULL string is.

It is not surprising that this is not easy for you, if you have not been able to locate the explanation for chr$(). Look here:
Under Help, select "Help Contents", then "Commands". All of the string functions are listed under the item "2. TEXT COMMANDS" Also, you can IM me by clicking the little Yahoo! IM button under this post, or e-mail if you prefer that way. Or just keep this thread open, you option.

Cheers, I hope this helps.
Posted: 25th Jun 2007 22:26
Well, that's why I asked. I had read before elsewhere that it would not. I know you a little better than to assume that you would that. I thought maybe it was pseudocode, or whatever. Thanks, I have a few spots where that is appropriate.
Posted: 25th Jun 2007 22:47
Funny, you are right - that's where I got that misconception! That is the semi-colon, however. (I avoid that one usually.) The places I want a simple one line forks, like what you have there....XLNT! You are truly killer, tks again.
Posted: 26th Jun 2007 0:19
@Inspire - I think that there are some things that need to be clarified a little more. The first thing is that, in the editor, when you are programming, you need the quotes to tell DBPro...this is a text string. For example, this does not do anything:


I know that, I am not a complete noob. I just figured that it would be easier to leave the quotes out of the script, instead of having to account for them and add more code for a miniscule detail.

I'm more of a 3d artist, rather than a programmer. If you want to see some of the stuff I made, click on the Gone logo in my sig. All of those textures were made by me, along with most of the models.

Thanks for you help, I'll test this code out later today. I know DarkBasic is fairly easy compared to other programming languages, and that's why I picked it up, and I've really taken a liking to it. In the small amount of time that I have used it, I have learned alot.
Posted: 26th Jun 2007 5:00
I apologize for offending you; but I am not trying to put you on the spot. As a matter of fact, I had already visited your whip long ago - I am not a newb, either. It is most impressive.
Posted: 7th Jul 2007 22:29
I apologize for offending you; but I am not trying to put you on the spot. As a matter of fact, I had already visited your whip long ago - I am not a newb, either. It is most impressive.


Thanks.

I got farthest with WindowsKiller's code. I still don't understand how to implement Jinzai's code. lol

My question is, how could I use WindowsKiller's code to read a long, script like file?

This is a file I want to have for an FPS I'm working on.


+ Code Snippet
Default="ak47"
Weapon2="mp5"
Weapon3=""
Weapon4=""
Weapon5=""
Weapon6=""
Weapon7=""
Weapon8=""
Weapon9=""
Weapon0=""


The engine would read the weapons, and load the weapons from those folders to the corresponding keys (default is 1, weapon2 is 2, etc).

I really don't feel like repeating WindowsKiller's code 10 times just to read a 10 line file, and I have been trying different ways of doing this quicker. None of them worked.

Can anybody help me out here?