I need an example, the game script saves game's data as a text file.
Thank you
http://www.squirrel-lang.org/
[GS]Save as a text file.
Moderator: OpenTTD Developers
- openbu.org
- Engineer
- Posts: 74
- Joined: 14 Nov 2014 07:40
- Location: USA
- Contact:
Re: [GS]Save as a text file.
I solved a similar question too, but without an answeropenbu.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/

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
My heightmaps: Flat Earth Map and United nations logo
My scenarios: Game Fallout 1,2,3 Map scenario
My gamescripts: City Founder GS
- openbu.org
- Engineer
- Posts: 74
- Joined: 14 Nov 2014 07:40
- Location: USA
- Contact:
Re: [GS]Save as a text file.
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();
- openbu.org
- Engineer
- Posts: 74
- Joined: 14 Nov 2014 07:40
- Location: USA
- Contact:
Re: [GS]Save as a text file.
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+");
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.
- openbu.org
- Engineer
- Posts: 74
- Joined: 14 Nov 2014 07:40
- Location: USA
- Contact:
Re: [GS]Save as a text file.
/*
* 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>
* 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>
Who is online
Users browsing this forum: No registered users and 12 guests