SRL Archive - Help and Support

Two Questions


RISK

2011-12-27 09:46:40

When does using global variables become bad? I know that you should use local variables as much as you can. Say for X, Y, TPA, T2DPointArray and such. But for variables that you use only for settings that you can't use as local variables?

When does that become bad? Why is it bad? Is it only for standards and readability's sake or is there some other reason?


How does having something in a plugin help the speed? Is the speed increase really that much better? Say a script is 20k+ lines of code and there is no plausible way to shorten it. Would putting that script's functions & procedures in to a plugin help?
(Also I'm not sure that's how plugins actually work, but I'm just taking a wild guess.)


Thank you very much in advance to anyone that can answer my questions.

Kyle Undefined

2011-12-27 09:54:10

Global Variables can't help but be used when you add Stats to a script, so they're not that bad if those are the only reasons for them.

Really, to me, 10+ global variables are bad. However, that's just my opinion. When you define a global variable, there is memory allocated at runtime so you can use it when needed. It will not de-allocate until the script finishes. Unlike local variables, which will de-allocate when the procedure/function finishes.

Using simple Ints/Strings in global variables really doesn't affect the script too much, as long as you don't go overboard. Arrays allocate more memory than Ints/Strings.

I don't know much about plugins, so I can't answer that question :p

RISK

2011-12-27 10:01:10

Alright, I understand that and I learned something new. Thank you very much.
But what about this?:
var
rockColor, rockTolerance, rockPixelWidth, rockPixelHeight,
rockMinPixel, rockSPSSurface: Integer;

rockSPSAreas: TStringArray;

rockHue, rockSaturation: Extended;

rockAreaPoint, bankAreaPoint: TPoint;
rockPathToBank, rockPathToRocks: TPointArray;

This would allow the creation of a global miner (Just a rough example). But it has to use those global variables to know what to mine. These global variables are set up on run time with a procedure that grabs what the user of the script desires to mine and sets the variables accordingly.

I've tried using a type for the information, but it would not 'Save'. Or maybe I was doing it wrong, lol. :p

Kyle Undefined

2011-12-27 10:19:55

A type would be better, how were you using it?

RISK

2011-12-27 10:29:17


type
rockInfo = record
Color: Integer;
end;

procedure SetupMining;
var
Info: rockInfo;
begin
Info.Color := 1010110110;
WriteLn('SetupMining - Color = ' + ToStr(Info.Color));
end;

procedure Setup;
var
Info: rockInfo;
begin
SetupMining;
WriteLn('Setup - Color = ' + ToStr(Info.Color));
end;

Compiled successfully in 344 ms.
SetupMining - Color = 1010110111
Setup - Color = 0
Successfully executed.

It doesn't 'Save' it, you see. :x

Kyle Undefined

2011-12-27 10:53:42

Well duh, you're not setting your local var to the rockInfo Type that gets set in the SetupMining procedure. Try this ;)


program new;

type
rockInfo = record
Color: Integer;
end;

function SetupMining : rockInfo;
var
Info: rockInfo;
begin
Info.Color := 1010110110;
WriteLn('SetupMining - Color = ' + ToStr(Info.Color));
Result := Info;
end;

procedure Setup;
var
Info: rockInfo;
begin
Info := SetupMining;
WriteLn('Setup - Color = ' + ToStr(Info.Color));
end;

begin
ClearDebug;
Setup;
end.

Gu3st...

2011-12-27 10:56:21

You're doing it wrong;

type
TRock = record
Name : String;
Color, Tolerance : Integer;
end;

function GetRockInfo(RockName : String): TRock;
begin
with Result do
begin
Name := RockName;
case lowercase(RockName) of
'iron':
begin
Color := 123;
Tolerance := 2;
end;
end;
end;
end;

procedure FindRock(var X, Y : Integer; RockName : String);
var
Rock : TRock;
begin
Rock := GetRockInfo(RockName);
// code here...
end;

RISK

2011-12-27 11:05:18

Ooh, thank you both! I love learning new things. :o +rep to you both!

Now if I could just find out about the plugin thing.