Log in

View Full Version : Setting local variables for a function from another function, possible?



Wolygon
05-07-2011, 06:54 AM
Hey, I have the following case to set variables to use in the function (there will be more actions):

Example 1:
case action of
'OpenDoor' : begin
TileOffset.x := 5;
TileOffset.y := 5;
ObjID := 29261;
end;
'ClimbDown' : begin
TileOffset.x := 5;
TileOffset.y := 5;
ObjID := 29261;
end;
end;

This is clearly repetitive as I'm doing the same thing over and over, so a function would be good like:

Example 2:
case action of
'OpenDoor' : ObjFindingVar(5, 5, 29261);
'ClimbDown' : ObjFindingVar(5, 5, 29261);
end;

And then set them in the function.

But since the variables are local this would not work, instead I would have to make them global. So is there a way to set "local variables for a function from another function", I would guess not as that is kind of the point of a global variable.

If not what do you think would be the best method of doing this:
1) Make global variables
2) Just use Example1
3) Something else

I just don't which is considered worse, messy looking code (example1) or setting them as global variables.

Thanks.

Iamadam
05-07-2011, 07:17 AM
procedure setSomeVars(var var1, var2: Integer);
begin
var1 := 5;
var2 := 5;
end;

procedure something;
var somevar, somevar1 : Integer;
begin
setSomeVars(somevar, somevar1);
writeln(toStr(somevar) + ', ' + toStr(someVar1));
end;

begin
something;
end.
end;

Daniel
05-07-2011, 07:27 AM
case action of
'OpenDoor' : begin
TileOffset.x := 5;
TileOffset.y := 5;
ObjID := 29261;
end;
'ClimbDown' : begin
TileOffset.x := 5;
TileOffset.y := 5;
ObjID := 29261;
end;
end;

You are setting the same thing, so why not just get rid of the case statement altogether? :S

Iamadam
05-07-2011, 07:54 AM
It'll be different, it was just an example I think.

Wolygon
05-07-2011, 08:34 AM
Thats awesome, thanks.

Yes it was just an example, there will also be more actions (around 4 so it'll get messy).

Thanks again. Happy me :).

Yago
05-07-2011, 05:29 PM
In case .... You can put two actions in the same case

Sex
05-07-2011, 07:40 PM
procedure setSomeVars(out var1, var2: Integer);
begin
var1 := 5;
var2 := 5;
end;

procedure something;
var somevar, somevar1 : Integer;
begin
setSomeVars(somevar, somevar1);
writeln(toStr(somevar) + ', ' + toStr(someVar1));
end;

begin
something;
end.

BraK
05-07-2011, 09:16 PM
Records/arrays anyone :)

~BraK

E: Daniel longtime no see :p good to see your still around. Are you on #SRL-School anymore?

Shuttleu
05-07-2011, 09:47 PM
Records/arrays anyone :)

~BraK

E: Daniel longtime no see :p good to see your still around. Are you on #SRL-School anymore?
but would that fix his problem?

to be honest, the way above would make them static in the sense that all of them would be that same if he used that function

i dont see how he would make each case different without making a function for each case or making the function accept the values he wants, but that would defeat the purpose of using a function because he could do it faster without

his best option i think would be to make the vars global and make a function which only takes the values and sets the vars accordingly

~shut

EDIT:
var
var1, var2: Integer;

procedure setSomeVars(val1, val2: Integer);
begin
var1 := val1;
var2 := val2;
end;

procedure something;
begin
setSomeVars(3, 5);
writeln(toStr(var1) + ', ' + toStr(Var2));
end;

begin
something;
end.

EDIT:
or another way

var
TileInfo: Array [0..2] of Integer;
begin
case action of
'OpenDoor' : TileInfo:= [5, 5, 29261];
'ClimbDown' : TileInfo:= [5, 5, 29261];
end;
end.

Sex
05-07-2011, 09:54 PM
Why do you have it as a TVariantArray when it could just be a TIntegerArray? :P

Shuttleu
05-07-2011, 09:56 PM
Why do you have it as a TVariantArray when it could just be a TIntegerArray? :P

i realised that, so i changed it to integer before you posted :)

~shut

Daniel
05-08-2011, 01:44 AM
He doesn't want them global...

@BraK: No I'm not.

Shuttleu
05-08-2011, 07:51 AM
He doesn't want them global...

@BraK: No I'm not.
well the last example wasnt meant to be global

here it is again

procedure whatever;
var
TileInfo: Array [0..2] of Integer;
begin
case action of
'OpenDoor' : TileInfo:= [5, 5, 29261];
'ClimbDown' : TileInfo:= [5, 5, 29261];
end;
{do whatever}
end;