SRL Archive - OSR Help

Saving & Opening Files via SCAR?


Daniel

2008-01-10 01:03:24

Is it possible for a script to save what it has changed then close the script and re-open the new one very quickly, or even if there is anything similar?

If there is, could you please tell me how to use them?

Something i want is like:

if(x=100) then
begin
SaveFile(ScriptPath+'\'+ScriptName);
ReloadFile(ThisScript);
end;

But not exactly like that.

Thanks to the people that helps :)

mixster

2008-01-10 02:20:06

It would work better if you had the entire script in another file then just had the script that overwrites it call the procedures after including it. Not sure if you can save to a file you're currently in and open it again using scar script and in that case it would probably end your script.
I would say your best bet would be to have your scar script create a 2nd file, write the data you need stored in there, then open it in scar and give it a variable name, such as if you used something like this:

Function SetupScript(str: String): String;
Var
i: Integer;

Begin
i:= ReWriteFile(ScriptPath+NewFile,False);
WriteFileString(i,Text);
CloseFile(i);
i:= OpenFile(ScriptPath+NewFile,False);
ReadFileString(i,Result,FileSize(i));
CloseFile(i);
End;
With NewFile being the name of the file you want to save your info to and Str being the only part of the function you need to put in (It gets saved into the file). It would then output the files text and you could use that (e.g. Variable:= SetupScript(moo); would then give variable the value moo and write the information to a new script)

I hope this helps

Daniel

2008-01-10 02:33:44

From: mixster05
It would work better if you had the entire script in another file then just had the script that overwrites it call the procedures after including it. Not sure if you can save to a file you're currently in and open it again using scar script and in that case it would probably end your script.
I would say your best bet would be to have your scar script create a 2nd file, write the data you need stored in there, then open it in scar and give it a variable name, such as if you used something like this:

Function SetupScript(str: String): String;
Var
i: Integer;

Begin
i:= ReWriteFile(ScriptPath+NewFile,False);
WriteFileString(i,Text);
CloseFile(i);
i:= OpenFile(ScriptPath+NewFile,False);
ReadFileString(i,Result,FileSize(i));
CloseFile(i);
End;
With NewFile being the name of the file you want to save your info to and Str being the only part of the function you need to put in (It gets saved into the file). It would then output the files text and you could use that (e.g. Variable:= SetupScript(moo); would then give variable the value moo and write the information to a new script)

I hope this helps

Well i do want it to terminate every time it writes the file. This is the script so far:
program Bet_On_The_Flips;

const
Flips = 20; //How many flips to do.
BetHeads = 99; //If Heads has the value of this then you will win &100.
CheatCode = ''; //Enter a cheat code here.

var
AccountBalance, h, t, FlipAmount : Integer;

procedure Randomness;
begin
case Random(2) of
0: h:=h+1;
1: t:=t+1;
end;
end;

procedure DetectCheats;
begin
if(CheatCode='8111-94735') then
begin
AccountBalance:=AccountBalance+1000;
end;
if(CheatCode='MR Kn0w it @11') then
begin
h:=BetHeads;
end;
if(CheatCode='DaCheat') then
begin
Writeln('You ain''t 1337');
end;
end;

procedure ShowProgress;
begin
Writeln('|----------------|');
Writeln('| Heads: '+IntToStr(h));
Writeln('| Tails: '+IntToStr(t));
Writeln('|----------------|');
end;

procedure DefineLoseOrWin;
begin
if(h=BetHeads) then
begin
Writeln('Your bet was successful, congratulations!');
Writeln('&100 has been credited to your account.');
AccountBalance:=AccountBalance+100;
end;
if not(h=BetHeads) then
begin
Writeln('Ooh, your bet failed.');
Writeln('&50 has been removed from your account.');
AccountBalance:=AccountBalance-50;
end;
Writeln('Your new account balance is: &'+IntToStr(AccountBalance));
end;

begin
AccountBalance:=1000;
h:=0;
t:=0;
FlipAmount:=0;
if(BetHeads>Flips) then
begin
Writeln('The amount you have betted is higher than the Flips set.');
Writeln('&100 has been removed from your account.');
AccountBalance:=AccountBalance-100;
end;
DetectCheats;
repeat
ClearDebug;
Randomness;
ShowProgress;
FlipAmount:=FlipAmount+1;
Wait(500);
Writeln('');
Until(Flips=FlipAmount);
DefineLoseOrWin;
end.

I want it to save the account balance to that number everytime the script executes.

If you know, could you please let me know? Thanks :)

mixster

2008-01-10 02:49:32

Add the 2 functions to somewhere near the beginning of script (above the Script Begin)


Function GetBalance(): Integer;
Var
i: Integer;

Begin
i:= OpenFile(ScriptPath+NewFile,False);
ReadFileString(i,Result,FileSize(i));
CloseFile(i);
Result:= StrToInt(Result);
End;

Function SaveBalance(balance: Integer):Boolean;
Var
i: Integer;

Begin
i:= ReWriteFile(ScriptPath+NewFile,False);
WriteFileString(i,IntToStr(balance));
CloseFile(i);
End;

You would then put 'AccountBalance:= GetBalance();' at the beginning and at the end you would put 'SaveBalance(AccountBalance);'. Unfortunately this would be the only way without changing it manually and having it as a Const. You would still need to define NewFile as a Const at the beginning and make sure you tell users not to delete the file as it contains their accountbalance.

Daniel

2008-01-10 04:17:52

From: mixster05
Add the 2 functions to somewhere near the beginning of script (above the Script Begin)


Function GetBalance(): Integer;
Var
i: Integer;

Begin
i:= OpenFile(ScriptPath+NewFile,False);
ReadFileString(i,Result,FileSize(i));
CloseFile(i);
Result:= StrToInt(Result);
End;

Function SaveBalance(balance: Integer):Boolean;
Var
i: Integer;

Begin
i:= ReWriteFile(ScriptPath+NewFile,False);
WriteFileString(i,IntToStr(balance));
CloseFile(i);
End;

You would then put 'AccountBalance:= GetBalance();' at the beginning and at the end you would put 'SaveBalance(AccountBalance);'. Unfortunately this would be the only way without changing it manually and having it as a Const. You would still need to define NewFile as a Const at the beginning and make sure you tell users not to delete the file as it contains their accountbalance.


Please explain further as by what you mean by putting NewFile as a constant? Put it as a string, integer or a boolean? If it is either Integer or String what do i put in it. But, if i am correct, NewFile would be the name of the script so it would be a string.

Example:

const
NewFile = 'coinflipper-1';

So is it the name of the new file to be created?

yanix

2008-01-10 06:12:30

Why is Mixter a reg user? he should be Junior.. and this would be great but u could simply just press X after u edit a script and press Yes on the 'Save Changes' message..

Daniel

2008-01-11 02:01:31

From: yanix
Why is Mixter a reg user? he should be Junior.. and this would be great but u could simply just press X after u edit a script and press Yes on the 'Save Changes' message..

Yanix… Don't SPAM!

Also, i have no idea why mixter isn't a junior member and also, i wanted it to save automatically.

mixster

2008-01-11 16:00:18

NewFile would be a string, with it being the filename you wanted it to call the new file being made that stores the balance, including an extension(.txt or .scar would probably be the best file ext's to use). Also after thinking about it a bit more, you could probably make it overwrite the open file, the only problem would be that they would then have to choose yes from the 'reload this script' popup and also that you would have to find the exact part of where the balance would be stored at the beginning.

Daniel

2008-01-11 19:04:40

From: mixster05
NewFile would be a string, with it being the filename you wanted it to call the new file being made that stores the balance, including an extension(.txt or .scar would probably be the best file ext's to use). Also after thinking about it a bit more, you could probably make it overwrite the open file, the only problem would be that they would then have to choose yes from the 'reload this script' popup and also that you would have to find the exact part of where the balance would be stored at the beginning.

Ok, how do i do that? Thanks :)

mixster

2008-01-12 15:03:38


procedure SetupBalance;
var
filef: Integer;
files: String;

begin
filef:=OpenFile(ScriptPath+'flipgame.scar',False);
ReadFileString(filef,files,filesize(filef));
CloseFile(filef);
files:=ReplaceRegex(files, 'Balance = '+IntToStr(Balance), 'Balance = '+IntToStr(AccountBalance));
filef := RewriteFile(ScriptPath+'flipgame.scar',False);
WriteFileString(filef,files);
CloseFile(filef);
end;
Add a new const and call it 'Balance' then give it the default value of 1000 or whatever you want it to be.
Change 'Accountbalance:=1000;' to ':=Balance'.
Add the above script as a procedure and call it at the end of the script.
Explanation of what it does:-
It starts off by reading the file 'flipgame.scar' (replace both of them with the actual name of the script) then puts it into the 'files' string. Replaceregex then searches through the 'files' string and replaces Balance with Accountbalance. It then writes the file. After it does and the script finishes, it then asks if the user would like to reload the script and if they choose to the accountbalance will be updated to the new value.
Also, a little tip on the script, you have it so that if you choose a higher head flip amount than actual happening flips, it removes $100, but you haven't got it to end the script, so the user misses out on the writeln telling them they have lost $100 and it also runs through the flipping where they lose another $50.