Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: FanOfHumor on Sat 30/07/2022 06:10:38

Title: (solved)run script that comes from txt file.
Post by: FanOfHumor on Sat 30/07/2022 06:10:38
I have raw script that I put into a txt file for opening in another ags game and I need to run that script or move it to somewhere in the game script to be run.Can this be done?
Title: Re: run script that comes from txt file.
Post by: Crimson Wizard on Sat 30/07/2022 18:38:07
I removed the previous answer, because I've got a feeling that you are asking not what I thought you are asking.

So, first of all, to clarify: are you asking about actual AGS script, or are you asking about some "custom" script, where you have your own commands?

Is this related to the text you were constructing in another question here (https://www.adventuregamestudio.co.uk/forums/index.php?topic=60117.0)?
Title: Re: run script that comes from txt file.
Post by: FanOfHumor on Sat 30/07/2022 18:46:19
No this is not related to that.Its ags script.I just don't know how to run the ags script that was written to the text file.
Title: Re: run script that comes from txt file.
Post by: Crimson Wizard on Sat 30/07/2022 18:54:10
Quote from: Pajama Sam on Sat 30/07/2022 18:46:19
No this is not related to that.Its ags script.I just don't know how to run the ags script that was written to the text file.

So in that case, this is not possible, as AGS engine cannot run text, it runs compiled script (this is called "bytecode"). Also it cannot run any arbitrary script, only if it's one of the modules.
So the script has to be compiled, and registered as a part of the game to run (be one of the modules that is defined in the game).

There is technically a way to recompile scripts and repack the game using special programs, but that's rather a solution for updating the game (for example when you want to patch your game on player's computer).

Could you tell more about your situation, why do you want to do this?
Title: Re: run script that comes from txt file.
Post by: FanOfHumor on Sat 30/07/2022 19:08:14
I have  a little tile drawing system made up that creates a room overlay in a grid space as a tile .This part already works fine but my problem is getting the information of what tiles were drawn used by another game.
I have this get written to file everytime a tile is drawn.
Code (ags) Select

if(mouse.IsButtonDown(eMouseLeft))
{
if(pt!=null)
{
tile[tilenum]=Overlay.CreateRoomGraphical(pt.x, pt.y, dfg);
File* tilemap;
tilemap=File.Open("tilemap.txt", eFileAppend);
String s;
s=String.Format("tile[tilenum]=Overlay.CreateRoomGraphical(%d,%d,%d);",pt.x, pt.y, dfg);
tilemap.WriteRawLine(s);
tilemap.Close();
}


}

It would be nice if I could run the script that was put into a txt file but I suppose the only way is to move the numbers to integers in the other game and place them in a loop that runs tile[tilenum]=Overlay.CreateRoomGraphical(x,y,dfg); till it reaches the end of the txt file.
Title: Re: run script that comes from txt file.
Post by: Crimson Wizard on Sat 30/07/2022 21:55:30
Quote from: Pajama Sam on Sat 30/07/2022 19:08:14but I suppose the only way is to move the numbers to integers in the other game and place them in a loop that runs tile[tilenum]=Overlay.CreateRoomGraphical(x,y,dfg); till it reaches the end of the txt file.

Yes, that's a normal way to do this.
Title: Re: run script that comes from txt file.
Post by: FanOfHumor on Sat 30/07/2022 23:19:12
I'm having trouble getting the integers to change to the next values in the text file.
Code (ags) Select
 
        File* loadmap = File.Open("$INSTALLDIR$/tilemap.txt",eFileRead);
tilex=loadmap.ReadInt();
tiley=loadmap.ReadInt();
tilesprite=loadmap.ReadInt();

The txt file has just the three integers but with several different values(in order).I need to have those integers equal the next values that are written.

The first few lines of the text file:
Code (ags) Select

I  Iº  I   I  Iº  I   IÃŒ  IØ  I   IÃŒ  IØ  I   IÃŒ  IØ  I   I  Iö  I   I  Iö  I   I  Iö  I   IT  I  I   I  I2  I   IÜ  IP  I   IÜ  IP  I   IÜ  IP  I   I   In  I   Id  IP  I   I   I2  I   Id  I  I   Id  IØ  I   Id  IÅ"  I   Id  I`  I   I(  IB  I   Id  I$  I   I   I  I   IÜ  Iè  I   IÜ  Iè  I   IT  Iè  I   IT  Iè  I   IT  Iè  I   I  I  I   I  IB  I   IÃŒ  I`  I   IÃŒ  IÅ"  I   IÃŒ  IØ  I   IÃŒ  I  I   

I've tried using seek to choose the next line but that doesn't seem to work either. I probably am using seek wrong so if you could please show me how I would appreciate it.
Title: Re: run script that comes from txt file.
Post by: Crimson Wizard on Sun 31/07/2022 02:52:52
Quote from: FanOfHumor on Sat 30/07/2022 23:19:12
I'm having trouble getting the integers to change to the next values in the text file.

I've tried using seek to choose the next line but that doesn't seem to work either. I probably am using seek wrong so if you could please show me how I would appreciate it.

You do not need to use Seek at all, just like when you are using WriteInt and similar, ReadInt will automatically advance further in file. The use of Seek is for special cases only, like when you need to return back in file to edit something you've already written, or read a file in a different order.

If the values you read back are not what you expect, then either writing was incorrect, or you read in a wrong order. Please post a code that writes the file?
Title: Re: run script that comes from txt file.
Post by: FanOfHumor on Sun 31/07/2022 22:16:19
Here is the code that writes to the txt file.
Code (ags) Select

pt=Screen.ScreenToRoomPoint(tileguide.X, tileguide.Y);

if(beforetilenum<100000)
{
beforetilenum+=1;
}
if(beforetilenum==100000)
{
beforetilenum=0;
}
if(tile[beforetilenum]==null)
{
tilenum=beforetilenum;
}





if(tileguide.Y<mouse.y&ileguide.X<mouse.x)
  {
    //bottom right

    tileguide.X+=60;
    tileguide.Y+=30;
  } 
  if(tileguide.Y>mouse.y&ileguide.X>mouse.x)
  {
    //topleft
    tileguide.X-=60;
    tileguide.Y-=30;
  } 
 
  if(tileguide.Y<mouse.y&ileguide.X>mouse.x)
  {
    //bottomleft
    tileguide.X-=60;
    tileguide.Y+=30;

  }   

  if(tileguide.Y>mouse.y&ileguide.X<mouse.x)
  {
    //top right
    tileguide.X+=60;
    tileguide.Y-=30;

  } 
int dfg=3;
if(mouse.IsButtonDown(eMouseLeft))
{
if(pt!=null)
{
tile[tilenum]=Overlay.CreateRoomGraphical(pt.x, pt.y, dfg);
File* tilemap;
tilemap=File.Open("tilemap.txt", eFileAppend);
tilemap.WriteInt(pt.x);
tilemap.WriteInt(pt.y);
tilemap.WriteInt(dfg);
//String s;
//s=String.Format("tile[tilenum]=Overlay.CreateRoomGraphical(%d,%d,%d);",pt.x, pt.y, dfg);
//tilemap.WriteRawLine(s);
tilemap.Close();
}


}

Reading the code does work in the write order but the next integers after the first three aren't accessed by the reading script.
What is meant to be is that the three integers load from the txt file then in the next game loop the same integers load from the next three that are after that in the txt file.
So to put it simply.I want to read the first three integers then read the next three integers and continue reading the next three integers.
Title: Re: run script that comes from txt file.
Post by: Crimson Wizard on Sun 31/07/2022 22:56:52
Quote from: FanOfHumor on Sun 31/07/2022 22:16:19
Reading the code does work in the write order but the next integers after the first three aren't accessed by the reading script.
What is meant to be is that the three integers load from the txt file then in the next game loop the same integers load from the next three that are after that in the txt file.

Are you maybe reopening File each loop iteration? Is the reading code (https://www.adventuregamestudio.co.uk/forums/index.php?topic=60124.msg636648380#msg636648380) you posted above fully inside a  loop? Each time you reopen a file it is read from the start, so that would explain the problem.

The idea is to open a file once before the loop, then read inside a loop, then close a file.

Something like
Code (ags) Select

File* loadmap = File.Open("$INSTALLDIR$/tilemap.txt",eFileRead);

while (!loadmap.EOF)
{
        tilex=loadmap.ReadInt();
        tiley=loadmap.ReadInt();
        tilesprite=loadmap.ReadInt();
        // create overlay etc
}

loadmap.Close();

Title: Re: run script that comes from txt file.
Post by: FanOfHumor on Mon 01/08/2022 00:37:09
Oh. Well then yes that definitely explains why it wasn't working.I didn't know that it starts at the beggining everytime you reopen the file.Thanks for clearing that up. :)
Title: Re: run script that comes from txt file.
Post by: Khris on Wed 03/08/2022 09:35:10
Just two notes:

1) there are professional tile editors (https://www.mapeditor.org/) you can use. You do have to write code to read their map format though.
Or use my module: https://www.dropbox.com/s/z6elv1lf7y46cs3/XMLParse.scm?dl=1

2) overlays are used to display graphics on top of the background (and use up extra memory). To draw to the room background you need to grab the room's drawing surface and draw to that instead.
Title: Re: run script that comes from txt file.
Post by: FanOfHumor on Thu 04/08/2022 19:02:36
Thanks for the tip but I already made what I think is a very useful way of making tiles.By using overlays I can access the particular position of any tile existing.I have looked at tile modules before but they don't give you interactivity with any singular tile.With being able to interact with a particular tile I can have an object on top and detect what tile its on.In the editor the position of an overlay is based on the tileguide but in the game of which I load the tiles to can detect what tile is under the mouse and where.Thanks anyways. :)
Title: Re: run script that comes from txt file.
Post by: Crimson Wizard on Thu 04/08/2022 19:40:24
Quote from: FanOfHumor on Thu 04/08/2022 19:02:36By using overlays I can access the particular position of any tile existing.I have looked at tile modules before but they don't give you interactivity with any singular tile.With being able to interact with a particular tile I can have an object on top and detect what tile its on.In the editor the position of an overlay is based on the tileguide but in the game of which I load the tiles to can detect what tile is under the mouse and where.

That is quite possible to do with drawn tiles too if you store tile data in arrays: then you can find a tile in array by converting from screen to map coordinates, and read or change its data.
(I don't know how the aforementioned modules do that)
Title: Re: run script that comes from txt file.
Post by: Khris on Sun 07/08/2022 13:21:02
Indeed, even a scrolling room is no obstacle to this. You can easily convert mouse coords into tile coords.