So if I do:
word := Tword.create('Cat','Word',false);
playedwords.add(word);
And I don't free "word" .. if word is a local variable what happens when it goes out of scope?
If you did not make any copy of the reference (that is, if you did not add it to any list, or assigned it to a global var), then you have a memory leak. There is no garbage collection for classes.
Includes
word := getmem(TWord.InstanceSize;
And "Destroy" is the call to free that memory.
And any
copies a pointer to that memory.
And if you do Word2.Destroy then Word1 points to invalid memory.
(sometimes the invalid memory still has ghost data, and appears to work, but that is never stable)
In other words, you need to keep track which variables, and which lists still hold references to the instance (the allocated memory).
Then the next question is how do I later assign a variable to Tword(playedwords[0])? Or is my way working but likely I had done word.free? I'll have to go back and look.
The typecast is fine as it was. You put a TWord into the list, you cast it back when you take it out.
Or you do:
uses fgl;
type
TWordList = specialize TFPGList<TWord>;
var PlayedWords: TWordList;
begin
wordrec := PlayedWords[i]; // already returns the correct type
You might want to rethink your naming...
"word_rec" sounds like it is of type
type TSomething = record ... end;
which differs from a class.
PlayedWord vs PlayedWords => you gonna misread it at sometime....
use
PlayedWord and PlayedWordsList
Even all the integers c,s,r,d,p....
Give them longer names.
i,c are fine.
Use syncro edit
https://wiki.lazarus.freepascal.org/New_IDE_features_since#Syncron-Edit , once you written the code. (That is what I do, start with short names, then rename them)
Select the entire procedure and rename them.
(Imho 3 to 4 single letter vars are ok, but over that and give them longer names)
Just my 2 cents on the naming.