Dynamic Arrays lazarus / 61-Beginners ---------------------------------------------------------------------- thenarrator · 2015-10-15 02:09 Hi, am trying to write a program which trains the user to remember ingredients and order of ingredients for a sandwich, so a target is selected, the user enters their selections which are displayed on screen and then the input is compared against the target. Am having issues with drawing my cheese (lol) I put it in a separate procedure from DrawSandwich because I want the cheese to be drawn on the top bun and the buns on the bottom, is there an easier way around this? Currently the program runs as intended up until I click one of the buttons in CheeseSelect. After a button is clicked the menu hides but no cheese is drawn and the program then freezes. This is what shows in terminal when I force quit the program Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i ./run.sh: line 77: 15832 Segmentation fault: 11  "$EXE_PATH"Jacobs-MacBook- Pro:BigBoyBbq jacobrodda$ 3 Program Code Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program SandwichTrainer;uses SwinGame, sgTypes, SysUtils; type        FoodKind = (Bun, Rye, Bbq, Ketchup, Mayo, Mustard, Lettuce, Tomato, Beetroot, Pickles, Strings, None, Chicken, Pork, SaucyBeef, SliceBeef, Pastrami, Tasty, Swiss, Scamorza);        SandwichKind = (Carolina, Dorothy, ZMan, NewYork, Aussie, Kids);          //FoodData = record        //    name: FoodKind;        //      bmp: Bitmap;        //end;         InputData = record                cheese: array of FoodKind;              toppings: array of FoodKind;                SAndS: Boolean;      end;         TargetData = record                cheese: array of FoodKind;                kind: SandwichKind;                toppings: array of FoodKind;        end;         GameData = record              input: InputData;                target: TargetData;                score: Integer;                currentPanel: String;   end; procedure LoadResources();begin        LoadBitmapNamed('BottomBun',    'BottomBun.png');        LoadBitmapNamed('TopBun',               'TopBun.png');        LoadBitmapNamed('Lettuce',              'Lettuce.png');        LoadBitmapNamed('Beetroot',             'Beetroot.png');        LoadBitmapNamed('Tomato',               'Tomato.png');        LoadBitmapNamed('Tasty',                        'Tasty.png');  LoadBitmapNamed('Bbq',      'Bbq.png');  LoadBitmapNamed('Ketchup',      'Ketchup.png');  LoadBitmapNamed('Pickles',    'Pickles.png');end;  function FoodBitmap(input: FoodKind): Bitmap;begin        case input of              Bbq: result := BitmapNamed('Bbq');                Ketchup: result := BitmapNamed('Ketchup');                Lettuce: result := BitmapNamed('Lettuce');                Tomato: result := BitmapNamed('Tomato');                Beetroot: result := BitmapNamed('Beetroot');                Pickles: result := BitmapNamed('Pickles');                Tasty: result := BitmapNamed('Tasty');                Scamorza: result := BitmapNamed('Scamorza');        end;end; procedure DrawCheese(const game: GameData);begin  DrawBitmap(FoodBitmap(game.input.cheese[0]), Round((ScreenWidth() - BitmapWidth(FoodBitmap(game.input.cheese[0]))) / 2), 10);end; procedure DrawSandwich(const game: GameData);var      i: Integer;begin  DrawBitmap(BitmapNamed('BottomBun'), Round((ScreenWidth() - BitmapWidth(BitmapNamed('BottomBun'))) / 2), Round((ScreenHeight() - BitmapHeight(BitmapNamed('BottomBun'))) - 20));  DrawBitmap(BitmapNamed('TopBun'), Round((ScreenWidth() - BitmapWidth(BitmapNamed('BottomBun'))) / 2), 10);         for i := 0 to (Length(game.input.toppings) - 1) do         begin                DrawBitmap(FoodBitmap(game.input.toppings[i]), Round((ScreenWidth() - BitmapWidth(FoodBitmap(game.input.toppings[i]))) / 2), Round((ScreenHeight() - BitmapHeight(FoodBitmap(game.input.toppings[i]))) - 20));        end;   for i := 0 to (Length(game.input.cheese) - 1) do   begin    DrawCheese(game);  end;  end;   procedure AddTopping(var input: InputData; topping: FoodKind);begin        SetLength(input.toppings, (Length(input.Toppings) +1));  input.toppings[(Length(input.toppings) -1)] := topping;end; procedure AddCheese(var game: GameData; cheese: FoodKind);begin  SetLength(game.input.cheese, (Length(game.input.cheese) + 1));  game.input.cheese[(Length(game.input.cheese) + 1)] := cheese;end; procedure SauceSelect(var game: gameData);begin         if ButtonClicked('BbqButton') then        begin                AddTopping(game.input, Bbq);                HidePanel(game.currentPanel);        ClearScreen(ColorWhite);                      game.currentPanel := 'SaladPanel';              end;   if ButtonClicked('KetchupButton') then  begin        AddTopping(game.input, Ketchup);         HidePanel(game.currentPanel);    ClearScreen(ColorWhite);                  game.currentPanel := 'SaladPanel';        end; end;  procedure SaladSelect(var game: gameData);begin     if ButtonClicked('LettuceButton') then                AddTopping(game.input, Lettuce);             if ButtonClicked('BeetrootButton') then                AddTopping(game.input, Beetroot);          if ButtonClicked('TomatoButton') then                AddTopping(game.input, Tomato);          if ButtonClicked('PicklesButton') then                AddTopping(game.input, Pickles);          if ButtonClicked('DoneButton') then        begin                HidePanel(game.currentPanel);        ClearScreen(ColorWhite);                        game.currentPanel := 'CheesePanel';          end;end; procedure CheeseSelect(game: gameData);begin         if ButtonClicked('TastyButton') then    begin        HidePanel(game.currentPanel);        ClearScreen(ColorWhite);        AddCheese(game, Tasty);    end;     if ButtonClicked('ScamorzaButton') then    begin        HidePanel(game.currentPanel);        ClearScreen(ColorWhite);                AddCheese(game, Scamorza);    end;  end; procedure AssignTarget(var game: GameData);begin        case game.target.kind of                Aussie:                 begin                        SetLength(game.target.toppings, 7);                       game.target.toppings[0] := Bbq;                        game.target.toppings[1] := Mayo;                        game.target.toppings[2] := Lettuce;                        game.target.toppings[3] := Beetroot;                        game.target.toppings[4] := Tomato;                        game.target.toppings[5] := Chicken;                        game.target.toppings[6] := None;                        game.target.cheese[0] := Tasty;                end;                Dorothy:                begin              SetLength(game.target.toppings, 3);                            game.target.toppings[0] := Bbq;              game.target.toppings[1] := Pickles;              game.target.toppings[2] := SaucyBeef;            game.target.cheese[0] := None;            end;           Carolina:          begin              SetLength(game.target.toppings, 3);                             game.target.toppings[0] := Bbq;            game.target.toppings[1] := None;              game.target.toppings[2] := Pork;              game.target.cheese[0] := None;         end;     ZMan:    begin      SetLength(game.target.toppings, 3);             game.target.toppings[0] := Bbq;      game.target.toppings[1] := None;      game.target.toppings[2] := SliceBeef;      game.target.cheese[0] := Scamorza;    end;     Kids:    begin      SetLength(game.target.toppings, 4);            game.target.toppings[0] := Ketchup;      game.target.toppings[1] := None;      game.target.toppings[2] := Chicken;      game.target.toppings[3] := None;      game.target.cheese[0] := None;    end;    end;end; procedure SandwichSelect(var game: GameData);begin          if ButtonClicked('AussieButton') then        begin                HidePanel(game.currentPanel);                ClearScreen(ColorWhite);                        game.target.kind := Aussie;                      game.currentPanel := 'SaucePanel';                      end;       if ButtonClicked('CarolinaButton') then        begin                HidePanel(game.currentPanel);                ClearScreen(ColorWhite);                        game.target.kind := Carolina;                      game.currentPanel := 'SaucePanel';                      end;       if ButtonClicked('DorothyButton') then        begin                HidePanel(game.currentPanel);                ClearScreen(ColorWhite);                        game.target.kind := Dorothy;                      game.currentPanel := 'SaucePanel';                      end;       if ButtonClicked('ZManButton') then        begin                HidePanel(game.currentPanel);                ClearScreen(ColorWhite);                        game.target.kind := ZMan;                      game.currentPanel := 'SaucePanel';                      end;         if ButtonClicked('KidsButton') then        begin                HidePanel(game.currentPanel);                ClearScreen(ColorWhite);                        game.target.kind := Kids;                      game.currentPanel := 'SaucePanel';                      end; end; function CheckResults(game: GameData): Boolean;var        i: Integer;begin        for i := 0 to High(game.input.toppings) do      begin                if (game.input.toppings[i] = game.target.toppings[i]) then                        result := True              else                        result := False;        end;end; procedure Main();var        game: GameData; begin        OpenAudio();  OpenGraphicsWindow('SandwichTrainer', 1000, 800);  LoadDefaultColors();  LoadResources();        LoadResourceBundle( 'NumberBundle.txt' );         GUISetForegroundColor(ColorBlack);    GUISetBackgroundColor(ColorWhite);     game.currentPanel := 'MenuPanel';    SetLength(game.target.cheese, 1);    SetLength(game.input.cheese, 0);    SetLength(game.input.toppings, 0);        repeat        ProcessEvents();        UpdateInterface();      DrawInterface();        RefreshScreen(60);                  ShowPanel(game.currentPanel);                   SandwichSelect(game);        AssignTarget(game);        SauceSelect(game);        SaladSelect(game);      CheeseSelect(game);        DrawSandwich(game);      until(WindowCloseRequested());     end; begin        Main();end.          DrawBitmap Code Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   procedure DrawBitmap(name: String; x: Single; y: Single); overload;  begin    sgImages.DrawBitmap(name,x,y);  end; ---------------------------------------------------------------------- User137 · 2015-10-15 13:58 Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure CheeseSelect(game: gameData);You forgot var from the game parameter. You are sending it to AddCheese() for modification, which at least has that already.