SRL Archive - OSR Help

Reference to a Record in a Record


teyr

2012-03-27 01:08:20

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:

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:
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:

Raskolnikov

2012-03-27 01:14:57

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.

teyr

2012-03-27 01:21:20

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.

Raskolnikov

2012-03-27 02:35:03

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

Declaration:

var
NameOfArray: array of player;

Then you can run for to do loops to make edits to all of the "players".