[GS]Save as a text file.

Discuss the new AI features ("NoAI") introduced into OpenTTD 0.7, allowing you to implement custom AIs, and the new Game Scripts available in OpenTTD 1.2 and higher.

Moderator: OpenTTD Developers

Post Reply
User avatar
openbu.org
Engineer
Engineer
Posts: 74
Joined: 14 Nov 2014 07:40
Location: USA
Contact:

[GS]Save as a text file.

Post by openbu.org »

I need an example, the game script saves game's data as a text file.

Thank you

http://www.squirrel-lang.org/
agentw4b
Traffic Manager
Traffic Manager
Posts: 216
Joined: 14 Apr 2017 15:51
Location: Czech Republic

Re: [GS]Save as a text file.

Post by agentw4b »

openbu.org wrote: 23 Jul 2019 08:39 I need an example, the game script saves game's data as a text file.

Thank you

http://www.squirrel-lang.org/
I solved a similar question too, but without an answer :-(
there: viewtopic.php?f=65&t=85285
Owner and admin of servers with names "Experimental games" .
My heightmaps: Flat Earth Map and United nations logo
My scenarios: Game Fallout 1,2,3 Map scenario
My gamescripts: City Founder GS
User avatar
openbu.org
Engineer
Engineer
Posts: 74
Joined: 14 Nov 2014 07:40
Location: USA
Contact:

Re: [GS]Save as a text file.

Post by openbu.org »

Code: Select all

connection.prototype.send_json = function(strjson){

  var self = this;

  var bufs = Buffers();

  bufs.push(Buffer.from(strjson));

  bufs.push(zeroterm());

  self.sendpacket(adminPackets.ADMIN_GAMESCRIPT, bufs);

};

Code: Select all

  var json = {

​    "action": "call",

​    "method": "GSGoal.Question",

​    "args": [0, "GSCompany.COMPANY_INVALID", "\"Hello the admin is speaking\"", "GSGoal.QT_INFORMATION", "GSGoal.BUTTON_OK | GSGoal.BUTTON_CANCEL"]

  };

  var jsonStr = JSON.stringify(json);

  ottdConnection.send_json(jsonStr);

  ottdConnection.close();
User avatar
openbu.org
Engineer
Engineer
Posts: 74
Joined: 14 Nov 2014 07:40
Location: USA
Contact:

Re: [GS]Save as a text file.

Post by openbu.org »

Code: Select all

import("util.libservergs", "LibServerGS", 3);
import("util.superlib", "SuperLib", 36);
Log <- SuperLib.Log;
Helper <- SuperLib.Helper;

/** Import other source code files **/
require("version.nut"); // get SELF_VERSION

local myfile = file("output.txt","wb+");
ERROR:
dbg: [script] [18] Your script made an error: the index 'file' does not exist
dbg: [script] [18]
dbg: [script] [18] *FUNCTION [main()] content_download\game\ServerGS-v3\main.nut line [30]
dbg: [script] [18]
dbg: [script] [18] [this] TABLE
dbg: [misc] [squirrel] Failed to compile 'content_download\game\ServerGS-v3\main.nut'
dbg: [script] The script died unexpectedly.
User avatar
openbu.org
Engineer
Engineer
Posts: 74
Joined: 14 Nov 2014 07:40
Location: USA
Contact:

Re: [GS]Save as a text file.

Post by openbu.org »

/*

* file: textreader.nut

*/



//

// class for reading text files, v.2

//



class textreader

{

b = null



constructor(name)

{

try

{

// files must be open in binary mode ("b")

// since len() always returns actual filesize

// while readstr/readblob functions do not

// count new line characters in text mode



local f = file(name, "rb")



// file is read into a buffer then characters

// will be picked one by one from the buffer



b = f.readblob(f.len())

}

catch(e)

{

// exception will be thrown in eos() due to

// the fact exception thrown in constructor

// would display standard error message

// on the console even if catched outside

}

}



function eos()

{

try

return b.eos()

catch(e)

throw "could not open file"

}



function readln()

{

// it would be more efficient to preallocate some

// bufer for blob, however, there is a problem

// with concatenation of string returned by

// blob.readstr() with other string when the string

// stored in blob is shorter than the current blob size



local l = blob()

while(!eos())

{

local c = b.readstr(1)

if (c == "\r")

continue

if (c == "\n")

break



// if buffer is full its size is doubled

// (by squirrel virtual machine) in order

// to avoid numerous memory allocations

// this is more efficient than using

// s += c here



l.writestr(c)

}



// blob behaves like a file; we must rewind it

// in order to be able to read it



l.seek(0)



// length checking is necessary since l.readstr(0)

// throws "invalid size" error



return l.len() > 0 ? l.readstr(l.len()) : ""

}

}





/*

* file: lines.nut

*/



dofile("textreader.nut")



//

// generator yielding lines one by one

//



function lines(name)

{

local t = textreader(name)



while(!t.eos())

yield t.readln()



return ""

}



/*

* file: test2.nut

*/





dofile("lines.nut")



function printfile(filename)

{

try

{

print("/*\n * file: " + filename + "\n */\n\n")

foreach (s in lines(filename))

print(s + "\n")

}

catch(e)

print(e + "\n")

}



foreach (filename in ["textreader.nut", "lines.nut", "test2.nut"])

printfile(filename)

</code>






01.12.2005




I recently discovered Squirrel and after very short time it become my favourite scripting language in place of Lua. I find it small, elegant without clumpsy Lua syntax. Well done, Alberto!


However, I miss more examples and I would like to change it. The following is a simple class and generator for dealing with text files which I found a bit tricky in Squirrel.


trianon


<code>--[previous version of textreader removed]--

</code>
Post Reply

Return to “OpenTTD AIs and Game Scripts”

Who is online

Users browsing this forum: No registered users and 8 guests