forums

TNT Basic Forums > Games In Progress
A program in a string
< Last Thread     Next Thread >
Author
Thread        Post A Reply

09-27-2006 12:04

Posted by:
Orteil

Location:
Somewhere in France

Find more posts by Orteil

Hello, I made a procedure for a game which is used to stock some commands into one string, in order to make the data-writing easier to implement.
Here is the code :


procedure text(str st2)

str st1="",st3=""

'3 global vars (may be local)
a=0
b=0'where is the reading cursor in the string
c=0'what to do with the piece of text ?




for a=1 to str length(st2)' read each letter of the string
st1=st1+get char(st2,a-1)' stock the letter in the yet-read list


if mid str(st2,a,1)="•"' if the cursor encounters the symbol • then it means there is a 3-letters command right after it
st3=mid str(st2,a,4)' read the command
a=a+4' skip 4 letters
end if



if st3="•job"' if the command is •job then say to the computer that the string that comes after will be put in the "job" var
c=1
st1=""
end if

if st3="•arg"' the same thing, with the var "money"
c=2
st1=""
end if

if st3="•say"' just display the following string
c=3
st1=""
end if

if st3="•giv"' give an item (the id of which is the string to be read)
c=4
st1=""
end if

if st3="•tak"' remove an item (same thing)
c=5
st1=""
end if

if st3="•snd"' play a sound of id=string to read
c=6
st1=""
end if




if st3="•end"' if the command is •end, then start to translate into real commands

if c=1 then job=as number(st1)' the job thing...

if c=2' the money thing...
money=money+as number(st1)
end if

if c=3' the print thing...
print st1
end if


if c=6' the sound thing...
play sound as number(st1)
end if

if c=4 and item[as number(st1)]=0' the give item thing... (please notice there are several items in my game, so I put them in an array)
item[as number(st1)]=1
play sound 2
end if

if c=5 and item[as number(st1)]=1' the remove item thing.
item[as number(st1)]=0
end if

c=0' OK, now prepare the procedure to read another command in the string (yes, you can put several things in one string).
st1=""
end if



next a

end proc




Some examples of strings to input would be :
"•sayHey, that's 50 bucks !•end •arg50•end"
...this would display "Hey, that's 50 bucks !" and add 50 to the money var. (you can wisdraw money with this command.)

Another :
"•sayHuh, I just got a job as game-designer, I spent 23$ in a king-size pizza and I should whistle more often.•end •job3•end •arg-23•end •giv5•end •snd7•end"
...then, if 3 is the id for game-designer job, if 5 is the id for the pizza item and 7 the id for the whistling sound, the game will make all this at once !


I hope you'll find this piece of code useful. You can also add/custom stuffs (the job thing...) of course.
And think of the use of it if you want the player to input something he wants to be/happen in the game... Pretty useful for user-created contents, doesn't it ?

All times are GMT        Post A Reply

Forum Jump:
< Last Thread     Next Thread >

< Contact Us - TNT Basic >

Powered by: vBulletin Lite Version 1.0.1 Lite
Copyright © Jelsoft Enterprises Limited 2000.