PDA

View Full Version : tstringlist (help plz, im a noob)



bekuzofu
18-06-2005, 02:17 AM
how does the tstringlist work? is it a component? I'm new to delphi & dont have enough knowledge on strings.

i need to know to set one up and use it. thanx

HopeDagger
18-06-2005, 02:49 AM
how does the tstringlist work? is it a component? I'm new to delphi & dont have enough knowledge on strings.

i need to know to set one up and use it. thanx

TStringList is a class, which in a sense is very similar to a component. Anyways, here's how to create/use one:



procedure MyProc;
var
S: TStringList;
begin
S := TStringList.Create; // This is the class's constructor, all classes need this to be called in order to have the object instantiated
S.Add('Cheese');
S.Add('Fromage');
S.Strings[0] := 'A small houseboat';
S.Sort;

S.Free; // Don't forget to deallocate!
end;

bekuzofu
18-06-2005, 04:15 AM
so what happens if i want to load the strings from textfile when the application runs, and how to save them? this will help me alot thanx.

savage
18-06-2005, 07:08 AM
Loading is just as easy. TStringList contains a LoadFromFile method...


procedure MyProc;
var
S: TStringList;
begin
S := TStringList.Create; // constructor
S.LoadFromFile('LoadMyStrings.txt'); // Load our file
ShowMessage( S.Strings[0] );// Show first element in the list
S.Free; // Free Memory when you no longer need it, this calls the destructor.
end;

bekuzofu
18-06-2005, 09:16 PM
this is helping me alot and i thank you.

but what happens if i want the strings to be read on a txt file, line by line, as if it were a scripting language?
EDIT:
actually...i kinda got it :P
but how do i create a new folder in the script?
and how do i get the script to read unlimited strings?

off topic:
on my game that i'm creating, how do i use collision detection between 2 images so they do not collide with each other?
how do i make so the movements dont flicker?
how do i use the keyboard and assign keys for movement?

my next subject....how do i use delphix? or animation

HopeDagger
18-06-2005, 11:18 PM
but how do i create a new folder in the script?

Check out the MkDir procedure.


and how do i get the script to read unlimited strings?

You mean read strings until it reaches the end? The 'Count' member of at TStringList tells you how many strings are present inside the stringlist, so you'll know how many strings to read in.


on my game that i'm creating, how do i use collision detection between 2 images so they do not collide with each other?

Ehm. How can you use collision detection to keep things from colliding with eachother? There'd be nothing to detect. ;)

Okay, okay. Fine. No more sarcasm. A common method is the usage of bounding boxes, which is an imaginary box around a sprite. To determine if there's a collision, check if those two rectangles intersect.


how do i make so the movements dont flicker?

Assuming you're using GDI+ for your drawing, it's because you're clearing the whole screen and then drawing your new moved sprite. Within that time that the screen was cleared you're seeing that flicker. The easiest solution for this is to make use of a backbuffer. This would be an image (not visible to the user) that you'd draw your whole scene onto, and then draw that image onto the screen, so you don't see any flickering.


how do i use the keyboard and assign keys for movement?

Take a look at the TForm's events, OnKeyPress. Within those events you can check if the desired keys are being pressed.

Sorry if my responses are a bit vague -- you really asked a whole lot of things that were very broad in category. If you were to be a bit more specific in what exactly you want to know, then I'm sure myself and the other forum-mongers would be glad to help. :)

bekuzofu
19-06-2005, 01:35 AM
but how do i create a new folder in the script?

Check out the MkDir procedure.


and how do i get the script to read unlimited strings?

You mean read strings until it reaches the end? The 'Count' member of at TStringList tells you how many strings are present inside the stringlist, so you'll know how many strings to read in.


on my game that i'm creating, how do i use collision detection between 2 images so they do not collide with each other?

Ehm. How can you use collision detection to keep things from colliding with eachother? There'd be nothing to detect. ;)

Okay, okay. Fine. No more sarcasm. A common method is the usage of bounding boxes, which is an imaginary box around a sprite. To determine if there's a collision, check if those two rectangles intersect.


how do i make so the movements dont flicker?

Assuming you're using GDI+ for your drawing, it's because you're clearing the whole screen and then drawing your new moved sprite. Within that time that the screen was cleared you're seeing that flicker. The easiest solution for this is to make use of a backbuffer. This would be an image (not visible to the user) that you'd draw your whole scene onto, and then draw that image onto the screen, so you don't see any flickering.


how do i use the keyboard and assign keys for movement?

Take a look at the TForm's events, OnKeyPress. Within those events you can check if the desired keys are being pressed.

Sorry if my responses are a bit vague -- you really asked a whole lot of things that were very broad in category. If you were to be a bit more specific in what exactly you want to know, then I'm sure myself and the other forum-mongers would be glad to help. :)

can i have a sample of the The 'Count' member of at TStringList? (hope u know what i mean), and can i have one of gdi backbuffering example with collision detection. I think if i solve those two, i am set to go. Through this forums, I have learned to use strings and i'm glad people here can help me even though i'm a noob. Once i'm finished learning gdi, i'll go onto delphix. Thanx to all.

HopeDagger
19-06-2005, 03:01 AM
can i have a sample of the The 'Count' member of at TStringList? (hope u know what i mean)

Alright, I think I know what you mean. ;) Here's an example of reading all of the strings from a file you've loaded:


procedure MyProc;
var
a: integer;
S: TStringList;
begin
S := TStringList.Create;
S.LoadFromFile("myfile.txt");
for a := 0 to S.Count-1 do
begin
SomeMemo.Lines.Append(S.Strings[a]);
end;
end;



and can i have one of gdi backbuffering example with collision detection.

Hopefully someboy else will be generous to help you out on those last two -- it's late here. ;)

Traveler
19-06-2005, 09:48 AM
and can i have one of gdi backbuffering example with collision detection.

You may want to check out this tutorial http://lionprod.f2o.org/articl... (http://lionprod.f2o.org/articles/canvasgameprog.php)

Alternatively, I'd recommend checking out delphix or omega. As you get a lot of stuff, like collision detection for free.

bekuzofu
21-06-2005, 12:47 AM
I'm currently trying to make a scripting language. I have 3 things to ask.

First of all. Using a tstringlist, how do i make it so that I can read a specific line and tell it do a command.

Example:

if i made a command called #text(hi);

The string would first read the first word to know what type of command, in this case its to show text somewhere in the program. the bracket will tell it what to type or what file.

and all together if executed, the screen will show:

hi


my second question, is if u understand on queston1, but if u get what i mean, how do i make a scripting language wuth commands such as while/loop, do/loop, if/then .etc?

my last question, is how do make files so that when i click on the file, it opens up my program? and how do i make .exe files that are made by the user?

This is what i am stuck on, plz help me on this one thanx.

cairnswm
21-06-2005, 04:47 AM
Hi bekuzofu

I am a very experienced programmer, I am also a pretty experienced game programmer. I would not consider writing a scripting language - the complexities are much greater than you can possibly imagine. (I've done a rule based AI interpreter in the past and it was really difficult)

Fortunatly there are a number of scripting language options out there already. Have a look at Delphi Web Script, RemObjects Pascal Script, or the Pascal interpreter in the JVCL components. All of these are ready made script parsers for the pascal language.

If you want to make your own scripting language, consider making a pre interpreter that converts your scripting language to pascal code and then using another existing interpreter to run the code.

Instead of making EXEs consider making a single executable that runs your 'programs'. Then assosiate your scripts with this program. (Do a search on google for shell commands, or assosiate program to find out how). If neccessary you can encrypt your scripts so that other people cant change them. Another option (rather advanced coding required) is to create an exe and have your scripts as resources inside the exe.

My suggestion is to get hold of Delphi Web Script. It includes a nice example that has an IDE and full examples of compiling and running code.

Good Luck and have fun

bekuzofu
21-06-2005, 05:24 AM
I'm just making a simple language.
The main thing i need is to read the command which easy but instead of one simple command, i need something that is like text(hi), sorta like in qbasic with the print command.

but i think your ideas are really good.

cairnswm
21-06-2005, 05:53 AM
The method used to break up a command into various parts is called tokenising (making into tokens).

Ok - heres some help -

Create a Form
Place two TMemo components on it (Memo1, Memo2)
Place a button on it (Button1).

Change the strings property of Memo1 to

Print "Hello William"

Place the following into the Button1Click:

procedure TForm1.Button1Click(Sender: TObject);
Var
I : Integer;
Cmd : String;
Token1 : String;
begin
Memo2.Lines.Clear;
For I := 0 to Memo1.Lines.Count-1 do
Begin
Cmd := Memo1.Lines[I];
Token1 := Copy(Cmd,1,Pos(' ',Cmd)-1);
Cmd := Copy(Cmd,Pos(' ',Cmd)+1,Length(Cmd));
If UpperCase(Token1) = 'PRINT' then
Begin
Memo2.Lines.Add(Copy(Cmd,2,Length(Cmd)-2));
End;
End;
end;


There is a basic script interpreter - ok so it only understands one command but it is a start. :) Run the program and Press the button - you will see the output of the script in Memo2.

Remember:
:arrow: Memo.Lines is a TStrings (Almost the same as a TStringList).
:arrow: You can load a txt file into Memo.Lines using Memo.Lines.LoadFromFile
:arrow: Break the procedure into smaller procedures - call a seperate procedure for each command
:arrow: You need more than 1 Token for most commands - change Token1 into an array and break the whole command into all its tokens (Anything between " and " is a single token - same goes for anything between ( and ) )
:arrow: Add one command at a time. Dont try to do everything at once. The next step here is to tokenise the whole string.
:arrow: The second step to do is to find a way to create variables (Create a string list and use the .Names and .Value properties to make variables) - e.g. Set A = 2 (note - this is 4 tokens)
:arrow: Also make sure you define your language now - before you code. Make sure there are no possible conflicts in your language (In compiler theory this is called lexical parsing)

bekuzofu
21-06-2005, 03:57 PM
How did u learn delphi so well? books, classes, or tutorials on the net? Can you recommend something for me to learn? thanx

cairnswm
22-06-2005, 04:24 AM
I have never been on a Delphi course. I originally learnt Turbo Pascal while at university. When I started working and Delphi came out I worked through "learn delphi in 21 days". Since then I have used Delphi almost every day. I work with Delphi at work and do my games with it while at home.

The best way to use Delphi is to spend a lot of time unsing it.

There are a lot of usefull delphi sites on the web. This one isn't the best for beginners but there are enough people here that are willing to help.

Get your self a Delphi book and work through it. It will give the ground work you need to move onto more advanced stuff.

bekuzofu
22-06-2005, 04:41 AM
though this forum i can actually program now :o .
I'm planning to make a game maker of some sort.
I've already made a map editor :P
Now i just need to learn more on collision detection, and how to make ur own scripting language :x :cry:

cairnswm
22-06-2005, 04:45 AM
Try doing something easier than a game maker to start with :) Have a look on my web site at Wumpus Hunt and try doing something similar.

Dont make your own scripting language. Use Delphi Web Script - there is an old example of getting it to work in the Tutorials section.

bekuzofu
22-06-2005, 05:47 AM
hmm...i'll have research on delphiwebscript to know what it is. and i'll look at ur site too. but one thing that is bothering me, collision detection.
my old game maker is basically a tile engine. There are 19x11 tiles in the board. I was wondering how to stop my sprite when it hits a certain tile, such as a sprite running into a wall. All my tiles are images for now. So basically i'm trying to find out how to make an image move to another image and stop after it has hit that image.


here is what i've done in my map editor so far:
http://gameshape.net/map.png



Ok, so my problem. If i was to create a sprite, how do i make it, so that the sprite will not move ontop of the castle or water?

p.s: where can i find tutorials on directx and delphix

I'm gonna prob use the map editor for a simple game that i will make.
thanx for being patient with me :P

Traveler
22-06-2005, 07:20 AM
Have look at the tutorials on my website. Depsite the fact that they're for delphix, I think they'll answer your questions.

cairnswm
22-06-2005, 07:42 AM
There are basically two ways of doing collision detection:

1. When a sprite moves it first calculates where it would move to. It then checks if that space is free. If the space is free it actually does the move - if the space is blocked it does not do the move.
2. The second method is pixel collision detection. A sprite calculates its move, then does a collision based detection against all the other sprites (or all the sprites in its area) and if there is a collision it does not move.

Based on your screen shot your sprites are positioned in a tile - use tile based collision detection.

When you want to create a sprite. Create it but dont allocate it to a square in the game - first choose where it should go, if that tile is empty then place it - else choose another spot.

I dont know DirectX :) I use the work of other people instead - look at DelphiX - there are lots of examples around the web (Learn to use google).

bekuzofu
25-06-2005, 06:45 AM
I'm currently having troubles with arrays and the scripting language stuff.
I'm trying to create a command split into 3 parts. the first part is the type of command, the second is to which object, and the last is the variable that relates to the speed of the object. Can someone explain how arrays work, and to show how i can split a command into three parts?

cairnswm
26-06-2005, 08:43 AM
An array is declared as follows:

Var
MyArray : Array[0..2] of String;

To access (set or get) a value in the array
MyArray[0]

So to set a value do
MyArray[0] := 'William';
MyArray[1] := 'Morgan';
MyArray[2] := 'Cairns';

Now to split a string into the array. There are a number of methods the easiest one (and the one I use all the time - which is why I consider it the easiest) is similar to how we split the command before

MyName := 'William Morgan Cairns';
MyArray[0] := Copy(MyName,1,Pos(' ',MyName)-1);
MyName := Copy(MyName,Pos(' ',MyName)+1,Length(MyName));
MyArray[1] := Copy(MyName,1,Pos(' ',MyName)-1);
MyName := Copy(MyName,Pos(' ',MyName)+1,Length(MyName));
MyArray[2] := MyName;