Recent

Author Topic: Add event to objects  (Read 1000 times)

Jake012345

  • Full Member
  • ***
  • Posts: 154
  • 'The knowledge is the key.'
    • FPCProjectShare
Re: Add event to objects
« Reply #15 on: May 23, 2020, 04:32:17 pm »
Hello again..

How can I 'Delete' this objects?

that's the creation code:

(I want to create them again whit this after the deleting)

Code: Pascal  [Select][+][-]
  1.   for i:=1 to targetcount do
  2.     if ExtractDelimited(1,targetinfo[i],[':'])='t' then begin
  3.       target[i]:=TImage.Create(Form1);
  4.       target[i].Parent:=Form1;
  5.       target[i].Left:=StrToInt(ExtractWord(2,targetinfo[i],[':']));
  6.       target[i].Top:=StrToInt(ExtractWord(3,targetinfo[i],[':']));
  7.       target[i].Width:=StrToInt(ExtractWord(4,targetinfo[i],[':']));
  8.       target[i].Height:=StrToInt(ExtractWord(5,targetinfo[i],[':']));
  9.       target[i].Stretch:=true;
  10.       target[i].Picture.LoadFromFile(homefolder+targetskin);
  11.       target[i].Transparent:=true;
  12.       target[i].Visible:=true;
  13.       target[i].Enabled:=true;
  14.       created[i]:=true;
  15.     end;      
  16.  
Check my ProjectShare site and share your content: FPCShare

TRon

  • Full Member
  • ***
  • Posts: 101
Re: Add event to objects
« Reply #16 on: May 23, 2020, 05:16:41 pm »
How can I 'Delete' this objects?
Didn't lucamar wrote that already ? I think he did  :)

...
Yes, if selecteditem is set with something like:
Code: Pascal  [Select][+][-]
  1. selecteditem := target[x];
then selecteditem is just a reference to target[x] (think of it as equating two pointers, if that's easier for you), so the net result of FreeAndNil(selecteditem) is almost the same as doing:
Code: Pascal  [Select][+][-]
  1. target[x].free;
  2. selectemitem := Nil;
...

iow make your loop through the array and inside that loop free each individual item like
Code: [Select]
target[x].free; or
Code: [Select]
Freeandnil(target[x]);

Jake012345

  • Full Member
  • ***
  • Posts: 154
  • 'The knowledge is the key.'
    • FPCProjectShare
Re: Add event to objects
« Reply #17 on: May 23, 2020, 05:19:49 pm »
But It's always errored...
with:
.destroy;
FreeAndNil();
.free;
:=nil;

all of them :(
Check my ProjectShare site and share your content: FPCShare

TRon

  • Full Member
  • ***
  • Posts: 101
Re: Add event to objects
« Reply #18 on: May 23, 2020, 05:30:53 pm »
In that case i need more context.

If you used that array, as i have been glancing over this thread briefly, then i assume you already have classes that are'stored' in that list but are actually already destroyed ?

That is why it is important to keep track of whether your class is still 'alive' or if it was already destroyed.

It is very dangerous to destroy the objects, f.e. by using a selecteditem variable and then not clear the same item inside the array at the same time. That is what is called a 'dangling' pointer (https://en.wikipedia.org/wiki/Dangling_pointer)

In case you did clear the item in teh array as well, then test for it inside your loop so that the free/freeandnil only gets called when there is an actual object in the array.

Code: Pascal  [Select][+][-]
  1. for i:=1 to targetcount do
  2.   If assigned(target[i]) then FreeAndNil(target[i]);
  3.  

Jake012345

  • Full Member
  • ***
  • Posts: 154
  • 'The knowledge is the key.'
    • FPCProjectShare
Re: Add event to objects
« Reply #19 on: May 23, 2020, 05:41:33 pm »
Yes!!

thanks for tip!!

I'm using an array for this:

created:array[interval of objects array] of boolean;

But I didn't thought I have to use it here too.
Check my ProjectShare site and share your content: FPCShare

TRon

  • Full Member
  • ***
  • Posts: 101
Re: Add event to objects
« Reply #20 on: May 23, 2020, 06:11:39 pm »
Yes!!

thanks for tip!!
In all fairness you made the connection yourself,but you're welcome  :)

Quote
I'm using an array for this:

created:array[interval of objects array] of boolean;

But I didn't thought I have to use it here too.
Ah ok, yes. In case you are keeping track of things elsewhere then use that same 'track'-solution to check if the objects inside the array are still 'valid' or if they are already 'destroyed'.

I don't know if this is a good point in time to mention (introduce you to) it, but in case you are constantly adding/setting items to that array and destroying them again, then keeping track of that (as per your explanation) get's tiresome very fast and the clever people from FPC have thought of that by introducing a class that might be able to help in such cases, see https://www.freepascal.org/docs-html/fcl/contnrs/tobjectlist.html.

But, if you are comfortable with what you are doing right now then please ignore that link. Perhaps you can take a look at it at your convenience. I do not wish to make things more complicated then they already are.

Jake012345

  • Full Member
  • ***
  • Posts: 154
  • 'The knowledge is the key.'
    • FPCProjectShare
Re: Add event to objects
« Reply #21 on: May 23, 2020, 06:15:53 pm »
How can I save my font to an .ini file?

And what is the best way to use additional units?
(I mean, what should I write in units and... yes. So that's it, what Is the best unit using technique?)
Check my ProjectShare site and share your content: FPCShare

jamie

  • Hero Member
  • *****
  • Posts: 2935
Re: Add event to objects
« Reply #22 on: May 23, 2020, 06:21:26 pm »
inifiles unit..

TIniFile class.
Number 1 at blue screen app creations!

Jake012345

  • Full Member
  • ***
  • Posts: 154
  • 'The knowledge is the key.'
    • FPCProjectShare
Re: Add event to objects
« Reply #23 on: May 23, 2020, 06:26:17 pm »
I know. Sorry I wasn't understandable.  :D

So I use it but I don't know how can I write font in .ini file. (there isn't writeFont command or FontToStr).
Check my ProjectShare site and share your content: FPCShare

TRon

  • Full Member
  • ***
  • Posts: 101
Re: Add event to objects
« Reply #24 on: May 23, 2020, 06:30:47 pm »
How can I save my font to an .ini file?
as already said by jamie, tinifile class see https://www.freepascal.org/docs-html/fcl/inifiles/tinifile.html and how to use see https://wiki.freepascal.org/Using_INI_Files

Quote
And what is the best way to use additional units?
(I mean, what should I write in units and... yes. So that's it, what Is the best unit using technique?)
That depends on which person you ask that question  :)

For me personally, i always start out in my main unit adding things f.e. start adding custom classes as i go along. The moment i get uncomfortable scrolling around i take the hint that it is time to start putting things into separate units. If it is a generic (reusable) class i'm implementing then that deserves it's own separate unit, unless there is a bunch of related classes (in which case i stuff them all in there).

I do not like stuffing actual 'work-horse'-code inside form units, i usually reserve form units for GUI-only related things.

Not sure what more there is to say. The longer you work with lazarus/fpc the more comfortable you get when using your own 'rythm' that suits you.


TRon

  • Full Member
  • ***
  • Posts: 101
Re: Add event to objects
« Reply #25 on: May 23, 2020, 06:32:47 pm »
I know. Sorry I wasn't understandable.  :D

So I use it but I don't know how can I write font in .ini file. (there isn't writeFont command or FontToStr).
You decide which properties that belong to the TFont class you wish to 'keep' and store those values inside the ini-file. Reverse the protocol to set the font  :)

edit: ah well, it's weekend.  Link to untested code  http://www.scalabium.com/faq/dct0039.htm to use as an example,and a slightly more complete one: http://www.devsuperpage.com/Articles/views/Delphi/Art_1-1436.asp   :D
« Last Edit: May 23, 2020, 06:51:20 pm by TRon »

Awkward

  • Jr. Member
  • **
  • Posts: 60
Re: Add event to objects
« Reply #26 on: May 23, 2020, 08:14:19 pm »
I know. Sorry I wasn't understandable.  :D

So I use it but I don't know how can I write font in .ini file. (there isn't writeFont command or FontToStr).
that my code (TL2Font is TFont):

Code: Pascal  [Select][+][-]
  1.   //--- Font
  2. var
  3.   config:TIniFile;
  4.   ls:AnsiString;
  5.   lstyle:TFontStyles;
  6. begin
  7.   config:=TIniFile.Create(INIFileName,[ifoEscapeLineFeeds,ifoStripQuotes]);
  8.  
  9.   config.WriteString (sNSBase+':'+sSectFont,sFontName   ,TL2DM.TL2Font.Name);
  10.   config.WriteInteger(sNSBase+':'+sSectFont,sFontCharset,TL2DM.TL2Font.Charset);
  11.   config.WriteInteger(sNSBase+':'+sSectFont,sFontSize   ,TL2DM.TL2Font.Size);
  12.   config.WriteString (sNSBase+':'+sSectFont,sFontColor  ,ColorToString(TL2DM.TL2Font.Color));
  13.  
  14.   lstyle:=TL2DM.TL2Font.Style;
  15.   ls:='';
  16.   if fsBold      in lstyle then ls:='bold ';
  17.   if fsItalic    in lstyle then ls:=ls+'italic ';
  18.   if fsUnderline in lstyle then ls:=ls+'underline ';
  19.   if fsStrikeOut in lstyle then ls:=ls+'strikeout ';
  20.   config.WriteString(sNSBase+':'+sSectFont,sFontStyle,ls);
  21.  
  22.   config.UpdateFile;
  23.  
  24.   config.Free;
  25.  

Jake012345

  • Full Member
  • ***
  • Posts: 154
  • 'The knowledge is the key.'
    • FPCProjectShare
Re: Add event to objects
« Reply #27 on: May 25, 2020, 04:22:00 pm »
Hello again!

I have a problem with my game and I cant solve it and I don't know where is the problem so I attached the all project.

The problem:
I can't kill the enemies like the targets, aafter that I added the health variable.
Check my ProjectShare site and share your content: FPCShare

Jake012345

  • Full Member
  • ***
  • Posts: 154
  • 'The knowledge is the key.'
    • FPCProjectShare
Re: Add event to objects
« Reply #28 on: May 25, 2020, 05:14:00 pm »
damn it!

The problem was the LoadMap>Enemyhealth[n]:=extractDelimited(5....);

5->6

 %)
« Last Edit: May 26, 2020, 07:59:01 am by Jake012345 »
Check my ProjectShare site and share your content: FPCShare

 

TinyPortal © 2005-2018