Results 1 to 10 of 10

Thread: Saving & Opening Files via SCAR?

  1. #1
    Join Date
    Dec 2006
    Location
    Sydney, New South Wales, Australia
    Posts
    4,603
    Mentioned
    15 Post(s)
    Quoted
    42 Post(s)

    Default Saving & Opening Files via SCAR?

    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:
    SCAR Code:
    if(x=100) then
      begin
        SaveFile(ScriptPath+'\'+ScriptName);
        ReloadFile(ThisScript);
      end;

    But not exactly like that.

    Thanks to the people that helps
    You may contact me with any concerns you have.
    Are you a victim of harassment? Please notify me or any other staff member.

    | SRL Community Rules | SRL Live Help & Chat | Setting up Simba | F.A.Q's |

  2. #2
    Join Date
    Jun 2007
    Location
    Wednesday
    Posts
    2,446
    Mentioned
    3 Post(s)
    Quoted
    1 Post(s)

    Default

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

  3. #3
    Join Date
    Dec 2006
    Location
    Sydney, New South Wales, Australia
    Posts
    4,603
    Mentioned
    15 Post(s)
    Quoted
    42 Post(s)

    Default

    Quote Originally Posted by mixster05 View Post
    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:
    SCAR Code:
    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:
    SCAR Code:
    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
    You may contact me with any concerns you have.
    Are you a victim of harassment? Please notify me or any other staff member.

    | SRL Community Rules | SRL Live Help & Chat | Setting up Simba | F.A.Q's |

  4. #4
    Join Date
    Jun 2007
    Location
    Wednesday
    Posts
    2,446
    Mentioned
    3 Post(s)
    Quoted
    1 Post(s)

    Default

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

    SCAR Code:
    Function GetBalance(): Integer;
    Var
      i: Integer;
     
    Begin
      i:= OpenFile(ScriptPath+NewFile,False);
      ReadFileString(i,Result,FileSize(i));
      CloseFile(i);
      Result:= StrToInt(Result);
    End;
    SCAR Code:
    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.

  5. #5
    Join Date
    Dec 2006
    Location
    Sydney, New South Wales, Australia
    Posts
    4,603
    Mentioned
    15 Post(s)
    Quoted
    42 Post(s)

    Default

    Quote Originally Posted by mixster05 View Post
    Add the 2 functions to somewhere near the beginning of script (above the Script Begin)

    SCAR Code:
    Function GetBalance(): Integer;
    Var
      i: Integer;
     
    Begin
      i:= OpenFile(ScriptPath+NewFile,False);
      ReadFileString(i,Result,FileSize(i));
      CloseFile(i);
      Result:= StrToInt(Result);
    End;
    SCAR Code:
    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:
    SCAR Code:
    const
      NewFile = 'coinflipper-1';

    So is it the name of the new file to be created?
    You may contact me with any concerns you have.
    Are you a victim of harassment? Please notify me or any other staff member.

    | SRL Community Rules | SRL Live Help & Chat | Setting up Simba | F.A.Q's |

  6. #6
    Join Date
    Jun 2007
    Location
    Mianus
    Posts
    863
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

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

  7. #7
    Join Date
    Dec 2006
    Location
    Sydney, New South Wales, Australia
    Posts
    4,603
    Mentioned
    15 Post(s)
    Quoted
    42 Post(s)

    Default

    Quote Originally Posted by yanix View Post
    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.
    You may contact me with any concerns you have.
    Are you a victim of harassment? Please notify me or any other staff member.

    | SRL Community Rules | SRL Live Help & Chat | Setting up Simba | F.A.Q's |

  8. #8
    Join Date
    Jun 2007
    Location
    Wednesday
    Posts
    2,446
    Mentioned
    3 Post(s)
    Quoted
    1 Post(s)

    Default

    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.

  9. #9
    Join Date
    Dec 2006
    Location
    Sydney, New South Wales, Australia
    Posts
    4,603
    Mentioned
    15 Post(s)
    Quoted
    42 Post(s)

    Default

    Quote Originally Posted by mixster05 View Post
    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
    You may contact me with any concerns you have.
    Are you a victim of harassment? Please notify me or any other staff member.

    | SRL Community Rules | SRL Live Help & Chat | Setting up Simba | F.A.Q's |

  10. #10
    Join Date
    Jun 2007
    Location
    Wednesday
    Posts
    2,446
    Mentioned
    3 Post(s)
    Quoted
    1 Post(s)

    Default

    SCAR Code:
    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.

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Replies: 15
    Last Post: 09-22-2008, 12:32 PM
  2. Opening Programs With Scar?
    By jumbosped in forum OSR Help
    Replies: 6
    Last Post: 09-01-2008, 10:19 AM
  3. SCAR Divi 3.01 DONT associate .scar files!!!
    By chimpy in forum News and General
    Replies: 1
    Last Post: 04-21-2007, 08:49 PM

Posting Permissions

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