Results 1 to 4 of 4

Thread: Reference to a Record in a Record

  1. #1
    Join Date
    Mar 2012
    Posts
    6
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default Reference to a Record in a Record

    Hii im am very new to simba scripting and currently I am trying to create a record type where one or more of its items is a reference/pointer to another record object. However, it seems that if you make a change to a record object in another record object, it doesn't affect the original object. Here's an example:

    Simba Code:
    program new;

    {$i srl/srl.simba}

    type player = record name : string; hp : integer; end;

    var
    a : player;
    b : player;

    type playergroup = record p1 : player; p2 : player; rating : integer; end;

    var
    aa : playergroup;

    Procedure InitializeAll;
    begin
    a.name := 'Player A';
    a.hp := 5;
    b.name := 'Player B';
    b.hp := 5;

    aa.p1 := a;
    aa.p2 := b;
    aa.rating := 5;
    end;

    Procedure Change(var players : playergroup);
    begin
    players.p1.name := 'Someone in group AA';
    Inc(players.p1.hp);
    Inc(players.rating);
    end;

    begin
    InitializeAll;

    WriteLn('Before:');
    WriteLn('Player 1 name:            '+a.name);
    WriteLn('Player 1 hp:              '+inttostr(a.hp));
    WriteLn('Player 2 name:            '+b.name);
    WriteLn('Player 2 hp:              '+inttostr(b.hp));
    WriteLn('Group 1''s Player 1 name:  '+aa.p1.name);
    WriteLn('Group 1''s Player 1 hp:    '+inttostr(aa.p1.hp));
    WriteLn('Group 1''s Player 2 name:  '+aa.p2.name);
    WriteLn('Group 1''s Player 2 hp:    '+inttostr(aa.p2.hp));
    WriteLn('Group 1''s rating:         '+inttostr(aa.rating));

    Change(aa);

    WriteLn('');
    WriteLn('After:');
    WriteLn('Player 1 name:            '+a.name);
    WriteLn('Player 1 hp:              '+inttostr(a.hp));
    WriteLn('Player 2 name:            '+b.name);
    WriteLn('Player 2 hp:              '+inttostr(b.hp));
    WriteLn('Group 1''s Player 1 name:  '+aa.p1.name);
    WriteLn('Group 1''s Player 1 hp:    '+inttostr(aa.p1.hp));
    WriteLn('Group 1''s Player 2 name:  '+aa.p2.name);
    WriteLn('Group 1''s Player 2 hp:    '+inttostr(aa.p2.hp));
    WriteLn('Group 1''s rating:         '+inttostr(aa.rating));

    end.

    Output:
    Code:
    Before:
    Player 1 name:               Player A
    Player 1 hp:                   5
    Player 2 name:               Player B
    Player 2 hp:                   5
    Group 1's Player 1 name:  Player A
    Group 1's Player 1 hp:      5
    Group 1's Player 2 name:  Player B
    Group 1's Player 2 hp:      5
    Group 1's rating:             5
    
    After:
    Player 1 name:               Player A  //the same
    Player 1 hp:                   5
    Player 2 name:               Player B
    Player 2 hp:                   5
    Group 1's Player 1 name:  Someone in group AA  //only this one changes..
    Group 1's Player 1 hp:      6
    Group 1's Player 2 name:  Player B
    Group 1's Player 2 hp:      5
    Group 1's rating:             6
    So yeah, making a change to aa.p1 in Change() does not change the player object assigned to aa.p1 in InitializeAll(), and I'm looking for a way make it do so. There doesn't seem to be any sort of pointers, or references to variables, soo anyone know how to do this? o:

  2. #2
    Join Date
    Nov 2007
    Location
    Nowhereville
    Posts
    1,155
    Mentioned
    0 Post(s)
    Quoted
    3 Post(s)

    Default

    What you are trying to do with records is create new variables. You can't really "link" the variables from a new type of function (or if you do I would assume it would be hard), as it's just like calling a new variable. It would be like calling one integer and having it change all integers in your script...

    In your change function why don't you just inc the individual players' variables? It's just another line of code and way simpler than trying to do whatever it is you want to.
    Formerly known as Cut em2 it

  3. #3
    Join Date
    Mar 2012
    Posts
    6
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Well just changing the individual players' variables works when there are just 2 players, but say they weren't players but rather items, and I needed to group together maybe 10-100 of them in each record instead of just 2. Then it would be extremely tedious and would take up a very large amount of space.

  4. #4
    Join Date
    Nov 2007
    Location
    Nowhereville
    Posts
    1,155
    Mentioned
    0 Post(s)
    Quoted
    3 Post(s)

    Default

    Why don't you make an array of "player" types. I think that would achieve what you need.

    Declaration:

    Simba Code:
    var
      NameOfArray: array of player;

    Then you can run for to do loops to make edits to all of the "players".
    Formerly known as Cut em2 it

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •