Problem with implementing undo logic lazarus / 61-Beginners ---------------------------------------------------------------------- Eugene Loza · 2015-03-17 09:09 Hi all! I have a problem with implementing undo-redo logic in my program. Every time the data in the class has been changed I record a copy of the class. And then when I try to 'recall' the recorded undo, the program doesn't seem to do anything with the class (other logic is obviously implemented). I understand that I've missed something basic... What am I doing wrong with the pointers? Code: [Select]type AllData = class(TObject) // This is container for all data of the editor   nscenarios:integer;   current:address_record; // shows where the user is 'located' currently   currentpage:integer;            // we handle page separately from address as addres points only to a specific record and used elsewhere   scenario:array[1..maxscenario] of DBScenario;   constructor create; end;    .... var All:AllData;   undo:array[1..maxundo] of ^allData;   nundo,currentundo:integer; .... // This procedure saves an undo slot. procedure Tform1.save_undo; var i:integer; begin  // destroy all leading undos if we are not at the first one  if currentUndo>0 then begin    for i:=1 to currentUndo do dispose(undo[i]);    // now cycle all remaining undos to 1st one    dec(nundo,currentundo);    if nundo>0 then      for i:=1 to nundo do undo[i]:=undo[i+currentundo];  end;  // if undo list is filled - discard the last item.  if nundo=maxundo then begin    dispose(undo[nundo]);    dec(nundo)  end;  // if undo list contains at least one element - slide undos pointers backward  if nundo>0 then    for i:=1 to nundo do undo[i+1]:=undo[i];  //create a new undo item & save current 'all' to it  inc(nundo);  new(undo[1]);  undo[1]^:=all;  currentUndo:=0;              //current undo is zero  UndoButton.enabled:=true;  RedoButton.enabled:=false; end;    {-----------------------------------------------} procedure TForm1.do_undo; begin   //if currentUndo=0 then save_item;   if currentUndo1 then begin     dec(currentUndo);     all:=undo[currentUndo]^;     do_edit;     if currentUndo1 then RedoButton.enabled:=true else RedoButton.enabled:=false;   end else RedoButton.enabled:=false; end; ---------------------------------------------------------------------- Eugene Loza · 2015-03-17 14:27 I.e. in short the problem looks to me like this: Code: [Select]Tall=class(TObject) a:integer; end; ... undo:^Tall; all:Tall; ... all:=Tall.create; all.a:=1; new(undo); undo^:=all; all.a:=2; all:=undo^; //the value all.a is still 2, not 1 as expected ---------------------------------------------------------------------- ChrisF · 2015-03-17 14:37 Sorry, I have not checked all your code (plus, a part is missing: for instance the create constructor for your alldata class), but it looks like to me that, in your logic, you don't store the data, only the pointers to the data. Unlike you are saving your alldata objects somewhere else, if you don't store the data you can't call them back. ---------------------------------------------------------------------- Martin_fr · 2015-03-17 15:04 A class, or rather an instance of a class (such as all : all:=Tall.create;) is just a pointer. To be exact the variable "all" contains a pointer. It points to some memory containing the data of the instance. Code: [Select] new(undo[1]); Creates a new pointer Undo will be a pointer to a pointer to the classdata Code: [Select] undo[1]^:=all; Copies the pointer (but not tde data pointed to) So now "all" and "undo[1]^" point to the same memory / the same data. Changes to all, are also to undo[1]^. You need to use create [1] to have a new object, and you need to copy each field of the class (not just the pointer) Look through LCL and find functions called "assign". They do that too. --- [1] Technically NewInstance, but that is more complicate. Create does that for you. ---------------------------------------------------------------------- Eugene Loza · 2015-03-18 10:56 Oh... I see... Thanks. Actually I thought it was something like that, but was too used to 'record' logic. Quoteyou need to copy each field of the class (not just the pointer)It's a bad idea, since 'All' has many sub-classes which in turn have their own sub-classes and this is a dynamic system, actively extended in the program development process, which will require track- back of all available changes. I need a reliable routine to store all the data stored in 'all' and its components. Moreover, I'll need to copy some of the sub-classes in the 'editing' process... I'll try to dig into 'assign' but no obvious solution available. Hmmm... ---------------------------------------------------------------------- taazz · 2015-03-18 11:31 Quote from: Eugene Loza on March 18, 2015, 11:56:02 amOh... I see... Thanks. Actually I thought it was something like that, but was too used to 'record' logic. Quoteyou need to copy each field of the class (not just the pointer)It's a bad idea, since 'All' has many sub-classes which in turn have their own sub-classes and this is a dynamic system, actively extended in the program development process, which will require track- back of all available changes. I need a reliable routine to store all the data stored in 'all' and its components. Moreover, I'll need to copy some of the sub-classes in the 'editing' process... I'll try to dig into 'assign' but no obvious solution available. Hmmm... No and yes. No you do not need to dive in to the linked classes especially if they are only a link to the data and not the data it self, yes if you allow the linked data to be modified you need to save the linked class data as well but you can use lazy  copying for this. In short make sure that each link has a unique id, field or what ever is needed to be reselected if needed and save only that ID with the rest of the data of the class. Take a look on the memento pattern it is used for undo/redo logic almost exclusively. In any case If you share more details I can give more specifics. ---------------------------------------------------------------------- Eugene Loza · 2015-03-18 12:02 QuoteIf you share more details I can give more specifics. Gladly. I'm writing a RPG game scenario construction kit. It has to store all data required for the game to run, to mod it, to create, to extend, to do anything. It will contain scripts, data, logic, translations, models, sounds, textures, ... even a cutscene editor and level designer. Another thing the kit does is analysis and balancing of the data. So... All is a generic container for all the available data. It contains a set of scenarios for the game. Each scenario contains all the data specific to this scenario: numeric, string data and some complex stuff like scripts. So, the structure below is ~5% of what it will be when the kit is finished. Code: [Select]type TWrap = class(TObject) //twrap is a generic edit class for all scenario objects.   FriendlyName:sstring;   comment:textstring; //  copyof:address_record; script->element // -> pull request? //  ischild:boolean; //children are unedittable   constructor create; virtual; end; //todo: creatures/items need no 'bonus' property? // Or should those be just 'zero' for reference, e.g. if a sword requires +4 in strength?! type DBStats = class(TWrap)   value:array[1..maxstats] of iint;   constructor create; virtual; end; type DBRace = class(DBStats) //DBRace is a race/gender information class   //race-specific parameters end; type DBprof = class(DBStats)   //profession-specific parameters end; type DBItem = class(DBStats) //DBItem is an item information class   //item-specific parameters   //durability end; type DBCreature = class(DBStats) //DBItem is an item information class   //creature-specific parameters   //flying   //hp, etc end; //npc class(DBCreature) //trader class(npc) {--------------------------------------------------------------------- ---} type SND_text = record   shortname:ssstring;   name:sstring;   description:textstring; end; type DBTrn = class(TWrap)   race:array[1..maxrace] of SND_text;   prof:array[1..maxprof] of SND_text;   item:array[1..maxstats] of SND_text;   creature:array[1..maxcreature] of SND_text;   value:array[1..maxstats] of SND_text;   constructor create; virtual; end; {--------------------------------------------------------------------- ---} type DBScenario = class(TWrap)          //Scenario is the most generic container for all data   nrace:integer;                        // current number of races   race:array[1..maxrace] of DBRace;     // races data   nprof:integer;                        // current number of professions   prof:array[1..maxprof] of DBProf;     // profession data   nitem:integer;                        // current number of professions   item:array[1..maxitem] of DBItem;     // profession data   ncreature:integer;   creature:array[1..maxcreature] of DBCreature; // creatures like npc, monsters   ntrn:integer;                         // current number of translations   trn:array[1..maxtrn] of DBTrn;        // all translation data   constructor create; virtual; end; type AllData = class(TObject) // This is container for all data of the editor   nscenarios:integer;   current:address_record; // shows where the user is 'located' currently   currentpage:integer;            // we handle page separately from address as addres points only to a specific record and used elsewhere   scenario:array[1..maxscenario] of DBScenario;   constructor create; end; The most complex part of the kit will be cutscene editor, quest editor and level editor. But right now - just the basics: create text and integer data, modify it, save and load. At the most abstract level possible. I tried to write the previous versions by 'record's but I had to rewrite every 'edit'/'save'/'display' procedure for the data records that practically share the same data types and require equal implementation. That's where classes really helped and I rewritten the editor for editing 2 times more data three times faster and twice less the code lines than for 'previous version'. But bumped into this pointer-problem ending up unable to save undo (which worked fine for records). What I need to do is to make a copy of All every time it is changed to revert if something goes wrong. This is not a critical feature, but would be a good addition. I can make 'undo' to store 'specific edits' at 'address' but it's very bug-unsafe. If I have to copy all sub-elements of 'all' by hand it's also very bug-unsafe (I may easily miss something I've added in the previous data structure edit). The data won't take too much RAM, so I'll be free to store ~100 undos. But I just need an exact copy of the values whatever they might be. The solution like http://stackoverflow.com/questions/4041760/correct- way-to-duplicate-delphi-object would do fine. But it's not working 'obvoiusly' as 'All' has no 'Assign' member. I.e. I need to solve a very complex task in the easiest way for the user and with respect to bug-safety. ---------------------------------------------------------------------- Eugene Loza · 2015-03-18 12:22 By the way, I get the warning: gui.pas(467,46) Warning: use extended syntax of NEW and DISPOSE for instances of objects pointing to the lines with "new(undo[1]);" or "dispose(undo[1]);" However, If I include constructor/destructor as it suggests (ie "new(undo[1],create);"), I get a fatal error: gui.pas(467,46) Error: The extended syntax of new or dispose isn't allowed for a class What is wrong? ---------------------------------------------------------------------- taazz · 2015-03-18 12:43 lets do this a bit slower because you leave out the relevant parts which are the object data them selfs. For example I'll use one of my hierarchies used in my database designer. Code: [Select]  TDBBase = class      FUID : QWord; // a unique id of the object in the database.      FName:string; // the name of the object;   end;   TDBField = class(TDBBase)     FDataType:integer;     FDataSize:integer;//in bytes;     FDataMinValue:Variant;     FDataMaxValue:Variant;   end;   TDBTable = class(TDBBase)      FFields : array of TDBFields;      FIndices: array of TDBIndex;   end; //..... even more classes; now when you edit a table you take snapshot of the tables data eg. Code: [Select]TDBTableSnapShot = record   ID : QWORD;   Name:string[255]; end; TDBFieldSnapshot = record   ID:QWORD;   Name:string;   DataType:integer;   DataSize:integer;   DataMinValue:Variant;   DataMaxValue:Variant; end; function TableSnapshot(const aTable:TDBTable):TDBTableSnapShot; begin   //1) copy the data that are unique to the table   Result.Name := FName;   Result.ID      := FUID; end; Function FieldSnapShot(const aField:TDBField):TDBFieldSnapShot; begin   Result.Name := Fname;   Result.ID := fUID;   Result.DataType:=aField.FDataType   Result.DataSize:= aField.FDataSize   Result.DataMinValue := aField.FDataMinValue;   Result.DataMaxValue:= aField.FDataMaxValue; end; You get the picture? When the table editor is opened the table specific data are saved to the undo list, when you press edit on the fieldname, data type or any field specific information the field snapshot is saved in the undo list when you insert a new field the new field is saved in the undo list with the tag new so you know what to delete and so on and so forth you keep restricting your self to the data and ignore the links until they are changed in some way in which case the linked object is taken in to the undo list. Don't try to save everything at once its not very efficient and depending the size and complexity of the edited object it might even slow down the editing process to annoying levels. Rely as much as possible in the undo abilities of the build in editors eg dont take two or more snapshots of the same data as the user changes eg edit field name snapshot the field edit ends, start editing the data type, snapshot the field, editing ends, no its far better to have an open editor that when closed all the changes are to be saved or discarded and the undo must show the same grouping, ee if the user applies a number of changes in the table ---------------------------------------------------------------------- taazz · 2015-03-18 12:46 Quote from: Eugene Loza on March 18, 2015, 01:22:40 pmBy the way, I get the warning: gui.pas(467,46) Warning: use extended syntax of NEW and DISPOSE for instances of objects pointing to the lines with "new(undo[1]);" or "dispose(undo[1]);" However, If I include constructor/destructor as it suggests (ie "new(undo[1],create);"), I get a fatal error: gui.pas(467,46) Error: The extended syntax of new or dispose isn't allowed for a class What is wrong? give me the definition of undo and its items please. ---------------------------------------------------------------------- Eugene Loza · 2015-03-18 13:16 Quotegive me the definition of undo and its items please. Code: [Select] undo:array[1..maxundo] of ^allData;As I've written above. QuoteYou get the picture?Trying to understand... hmm... definitely I don't even see how it is possible to compile e.g. Result.Name := Fname; but Fname is defined only as a member of TDBBase and I see no reference to the container class... Moreover, the data in the kit is not just a table of variable datatype. E.g. At this point, yes, it consists of string and text data... but, when I'll go on for defining e.g. a creature heart (artificial/natural with derived properties and 'pointers' to respective effects in case of trauma, set of replacement parts required, etc, etc, etc, including a link to 3d model file) - it'll be a sub-record in a class I hardly would be able to reduce to an array (as Variant type expects). Moreover, some peculiarities of the data will be determined in the development process (e.g. I have only very basic idea how to store 'cutscene' or 'level editor' data yet as they refer to very abstract things). I need a universal solution that will just copy the 'all' state (complete) and push it to some temporary storage. I didn't have time to try Assign yet... but if there is no 'universal' way to store the data / or it'll reduce the efficiency to unreasonable level, I'd better completely cancel undo/redo feature The anticipated data structure already makes my brain boil  , so I have absolutely no desire to add another headache in form of 'keeping in mind to save all the field to a backup snapshot, address, and then hope it won't fail during undo'. It's better not to have 'undo' at all, than not to trust it. Actually, I had no object oriented programming experience since Borland Pascal... when Delphi switched to ugly 'classes' and I kept on receiving SIGSEGV errors on classes, and abandoned the stuff as unneeded and buggy... So now, it's sometimes hard for me to go understand the OO peculiarities, despite ~20 years of programming experience  And this is just another example when record is better for implementation... If I only didn't require such abstract stuff... ---------------------------------------------------------------------- Martin_fr · 2015-03-18 13:36 Quote from: Eugene Loza on March 18, 2015, 02:16:52 pmCode: [Select] undo:array[1..maxundo] of ^allData; For starters, since objects are internally pointers. Code: [Select] undo:array[1..maxundo] of allData; This needs the same amount of memory. The actual memory will only be allocated when you call create. (or newinstance, if you do not want to run any code in the constructor) Quotedefinitely I don't even see how it is possible to compile e.g. Result.Name := Fname; but Fname is defined only as a member of TDBBase and I see no reference to the container class... Assuming that all your objects have ONE common baseclass. In the baseclass: Code: [Select] TBaseClass = class(..)    ...    procedure Assign(Other: TBaseClass); VIRTUAL;    ...   end    procedure TBaseClass.Assign(Other: TBaseClass);   begin     FResult := Other.FResult;   end In each inherited class In the baseclass: Code: [Select] TChildClass = class(..)    ...    procedure Assign(Other: TBaseClass); OVERRIDE;    ...   end    procedure TChildClass.Assign(Other: TBaseClass);   begin     inheritde;     if Other is TChildClass then begin        FOtherResult := TChildClass(Other).FOtherResult;     end;   end And so on.... As for creating a copy of the correct class (and you can move this code into baseclass if you like) Code: [Select]  // all is your instance   copy := all.ClassType.create If you need the constructor of any child class  to run, then you need to make the constructor virtual in the base class, and override it. QuoteMoreover, the data in the kit is not just a table of variable datatype. E.g. At this point, yes, it consists of string and text data... Strings are ref counted and copy-on-write, so "S1 := s2" does the job. However dynamic arrays are pointers to, you must copy then element by element. (you can use low level "move()" in some cases (if they do not contain managed types like string, or further arrays). But be careful of "move()" Btw, if your object refers other objects, then in assign (or you can call it CreateCopy as assign is not a typical name for doing this) you can recursively create copies of your objects. The only tricky bit is, if there is a loop  Obj1 -> obj2 -> obj 3 -> obj1 (back to original, no new copy to be created....) Quote or it'll reduce the efficiency to unreasonable level, I'd better completely cancel undo/redo feature Taking a copy like the above will not take a lot of time. That is if run in the game editor. If part of the actual game, where every micro second counts, then it might be to slow. Also you can "export" the data of your objects, in any form you like. It does not even have to be an object (just store the class type so you can reinstantiate it ---------------------------------------------------------------------- Eugene Loza · 2015-03-18 13:45 Thank you for the answer! QuoteThat is if run in the game editor. If part of the actual game, where every micro second counts, then it might be to slow.No,no... Only editor. The Editor will "compile" the scenario in editable form and bake it to (uneditable) game-specific data records. No undo in the game. Just had a thought... File of "class" would be a disaster in the context Seriously... maybe I should revert implementation back to 'records' which have no problems in saving/writing to files, etc...  Reading the answer... I'll need some time to understand it... ---------------------------------------------------------------------- Martin_fr · 2015-03-18 14:16 Quote from: Eugene Loza on March 18, 2015, 02:45:25 pmQuoteThat is if run in the game editor. If part of the actual game, where every micro second counts, then it might be to slow.No,no... Only editor. The Editor will "compile" the scenario in editable form and bake it to (uneditable) game-specific data records. No undo in the game. Well then deep-copy of the objects will be a relatively easy approach. But as I said, if the objects contain other objects, and the point back to the original (loop) that makes it much harder. As for time. an example. If you press a letter in the IDE editor, it creates 4 objects for undo info (some temporarily). Any speed issues? If memory usage is a concern, then copy selectively only data that you need. (If that is possible) QuoteJust had a thought... File of "class" would be a disaster in the context Seriously... maybe I should revert implementation back to 'records' which have no problems in saving/writing to files, etc...  File of class. That is not going to work. You need to serialize yourself. If you do implement serialization, you can use that for undo as well. Note type TFoo = record   i: integer;   s: ansistring; // {$H+} string end; file of TFoo => not going to work either. string is internally a pointer. (shortstring will work.) In general: File of abc, only works if abc is of constant size, has no managed types (string, array), and no class/object, and no pointers. For saving data for a game: you will need your own serialization. Or dig into RTTI. like the IDE saves all published properties. ---------------------------------------------------------------------- taazz · 2015-03-18 15:32 Quote from: Eugene Loza on March 18, 2015, 02:16:52 pmQuotegive me the definition of undo and its items please. Code: [Select] undo:array[1..maxundo] of ^allData;As I've written above. QuoteYou get the picture?Trying to understand... hmm... definitely I don't even see how it is possible to compile e.g. Result.Name := Fname; but Fname is defined only as a member of TDBBase and I see no reference to the container class... Correct, private variables can be accessed by all classes in the same unit, they tell me that the concept is similar to the "friend" concept in java. If you want to restrict their access then declare them in a strict private section and add properties or other methods of access. Quote from: Eugene Loza on March 18, 2015, 02:16:52 pmMoreover, the data in the kit is not just a table of variable datatype. E.g. At this point, yes, it consists of string and text data... but, when I'll go on for defining e.g. a creature heart (artificial/natural with derived properties and 'pointers' to respective effects in case of trauma, set of replacement parts required, etc, etc, etc, including a link to 3d model file) - it'll be a sub-record in a class I hardly would be able to reduce to an array (as Variant type expects). Moreover, some peculiarities of the data will be determined in the development process (e.g. I have only very basic idea how to store 'cutscene' or 'level editor' data yet as they refer to very abstract things). The code shown above was typed directly in the browser and does not reflect actual working code it was created to give a direction.  The code above shows that it saves only the static members. The dynamic members are saved individually when and if they are edited. if you want a complete state then you have to walk down the dynamic tree and ask for each object to save its state. If the same object is used from multiple containers then you have to decide which object is responisble for managing the shared object and then save only the id of that object not the complete state of it. for example to continue my previous theme of a database Code: [Select] TOrder = [orAscending, orDescending]; TDBIndexedField = record    Field :TDBField;    Order : tORder; end; TDBIndex = class(TDBBase)   FFields = array of TDBIndexedField; end; ..... TIdxFieldSnapShot = record   ID:Qword;   Order :TOrder; end; TIndexSnapShot = Record   Name:string   ID : QWord;   Fields :Array of TIdxFieldSnapshot; end; function IndexSnapShot(const aIndex:TDBIndex):TIndexSnapShot; var   vField :TDBIndexedField;   vCntr : integer =0; begin   Result.Name := Fname;   Result.Id := FID;   SetLength(Result.Fields,length(aIndex.FFields));   For vField in aIndex.FFields do begin     Result.Fields[vCntr].ID := vField.Field.FUID;     Result.Fields[vCntr].Order := vField.Order;   end; end; Don't worry about the implementation details look at the idea for now. We will work on the implementation in smaller pieces of code after we have something concrete to work with. Quote from: Eugene Loza on March 18, 2015, 02:16:52 pmI need a universal solution that will just copy the 'all' state (complete) and push it to some temporary storage. I didn't have time to try Assign yet... but if there is no 'universal' way to store the data / or it'll reduce the efficiency to unreasonable level, I'd better completely cancel undo/redo feature Ok. I have no idea what kind of a universal solution you are thinking off. I tried to make things as clear as I could, let me repeat the absolute rule, Each class is responsible to keep and duplicate its own state and its own state only. That means that if you have more class inside a class then those classes should be able to save their state as well and so on until all classes will be able to save their own state only. Never save linked classes with the state of a container class. Only the owner of a class or to put it simple the manager of a class will save that classes state every one else that links to that class should only save an ID that can be used to link to the class after a restart. So step 1 determine which data are static data and which are dynamic in the index sample above the static data is the order and the dynamic is the TDBField link and the snapshot saves the ID only which can be used to link to the in memory object with that ID after an application restart and reload from disk with out saving teh complete state of the TDBField both in the index and the table states. Quote from: Eugene Loza on March 18, 2015, 02:16:52 pmThe anticipated data structure already makes my brain boil  , so I have absolutely no desire to add another headache in form of 'keeping in mind to save all the field to a backup snapshot, address, and then hope it won't fail during undo'. It's better not to have 'undo' at all, than not to trust it. Actually, I had no object oriented programming experience since Borland Pascal... when Delphi switched to ugly 'classes' and I kept on receiving SIGSEGV errors on classes, and abandoned the stuff as unneeded and buggy... So now, it's sometimes hard for me to go understand the OO peculiarities, despite ~20 years of programming experience  And this is just another example when record is better for implementation... If I only didn't require such abstract stuff... Take a big breath, now take a second one. Ok keep breathing because this will take a while to seat in. Lets start with saving and loading those controls to an xml file, when you have a working solution for that you will be able to better understand how the undo will work as well. First create the first 3 or 4 objects in your hierarchy and pm me with the details of those objects you do not need to have the final design figured out just enough for a first pre alpha version that we can start working on. ---------------------------------------------------------------------- Eugene Loza · 2015-03-19 07:55 Thanks a lot for the answers! I'm really sorry... but... I gave up.  I've rewritten the code completely into records until it was not too far away from the beginning... Now everything is working flawlessly including undo and future 'raw file save'. Sorry for disturbing everybody... This information was really very useful. I hope I'll find use for it later, as the Castle Game Engine is OO...