Author Topic: How would I add the contents of a file into the code itself?  (Read 1872 times)

Quote from: Questioned code
function Disease_spawnHelicopter()
{
   if(isObject(helicopter))
   {
      return;
   }
   %bot = new AIPlayer(helicopterBot)
   {
      datablock = BlockheadHoleBot;
   };
   %bot.schedule(100, applyPilotAvatar);
   
   %filename = $Disease::Map[$Disease::CurrentMap];
   %spawnFilename = filePath(%fileName) @ "/helispawns.txt";
   if(isFile(%spawnFilename))
   {
      %file = new FileObject();
      %file.openforRead(%spawnFilename);
      
      %file.close();
      %file.delete();
   }
   else
   {
      %xy[%xyc = 1] = "0 0";
      echo("Couldn't find helicopter spawn file! Spawning helicopter at 0 0.");
   }

   
   %trans = %xy[getRandom(1,%xyc)] SPC 69;
   %trans = VectorAdd(%trans, "31 31 0");
   %heli = new FlyingVehicle(helicopter)
   {
      datablock = CityHeliMedVehicle;
   };
   %heli.setNodeColor("ALL", "0.38 0.57 0.49 1.00");
   %heli.setTransform(%trans);
   %heli.mountobject(%bot, 0);
   %heli.landingTick();
   %heli.pushAwayTick();
   
   MessageAll('', "\c0The \c3Helicopter \c0is here! All living survivors must get on it!");
   ServerPlay2D(Disease_Sound_note_haunt);
   
   schedule(getRandom(1000, 2000), 0, ServerPlay2D, Disease_Sound_note_page);
}
Code: (Contents of helispawns.txt) [Select]
%xy[%xyc = 1] = "-19.867 -80.5971";
%xy[%xyc++] = "-42.7127 -82.5906";
%xy[%xyc++] = "-67.1489 -52.2427";
%xy[%xyc++] = "-75.6646 -18.1384";
%xy[%xyc++] = "-11.7638 12.18";
%xy[%xyc++] = "2.68198 -83.9179";
%xy[%xyc++] = "-47.2515 -0.576858";
%xy[%xyc++] = "60 -50";
%xy[%xyc++] = "15 -30";
%xy[%xyc++] = "15 -5";

I want the entire contents of helispawns.txt to be shoved into the code. What do I do to the bolded area to achieve this?

Why not have the file contain this:
Code: [Select]
-19.867 -80.5971
-42.7127 -82.5906
-67.1489 -52.2427
-75.6646 -18.1384
-11.7638 12.18
2.68198 -83.9179
-47.2515 -0.576858
60 -50
15 -30
15 -5

And your script this:
Code: [Select]

   %filename = $Disease::Map[$Disease::CurrentMap];
   %spawnFilename = filePath(%fileName) @ "/helispawns.txt";
   if(isFile(%spawnFilename))
   {
      %file = new FileObject();
      %file.openforRead(%spawnFilename);
      %xyc=0;
      while(!isEOF(%file)) {
      %xy[%xyc] = %file.readLine();
      %xyc++;
      }
      %file.close();
      %file.delete();
   }
   else
   {
      %xy[%xyc = 1] = "0 0";
      echo("Couldn't find helicopter spawn file! Spawning helicopter at 0 0.");
   }

"Unable to find function isEOF"

%file.isEOF(), not isEOF(%file)


To answer your original question, you would simply exec(%spawnFileName);
But you'd want the variables in the file contents to be global variables

They don't need to be global if he's executing a file within the function like that. It's the same concept of that person that wanted to execute a bunch of messageClient(%client,'',"blah"); within a server command.

Oh, right
It shouldn't work, but it does


Actually best solution is to define them as global variables at the top of the file, that way you don't have the overhead of reading files every time

OH MY GOD IT WORKS

Thanks so much. I had to change the .txt to a .cs but that's not any problem at all.

Final code:
Code: [Select]
%filename = $Disease::Map[$Disease::CurrentMap];
%spawnFilename = filePath(%fileName) @ "/helispawns.cs";
if(isFile(%spawnFilename))
{
exec(%spawnFileName);
}
else
{
%xy[%xyc = 1] = "0 0";
echo("Couldn't find helicopter spawn file! Spawning helicopter at 0 0.");
}

OH MY GOD IT WORKS

Thanks so much. I had to change the .txt to a .cs but that's not any problem at all.

Final code:
Code: [Select]
%filename = $Disease::Map[$Disease::CurrentMap];
%spawnFilename = filePath(%fileName) @ "/helispawns.cs";
if(isFile(%spawnFilename))
{
exec(%spawnFileName);
}
else
{
%xy[%xyc = 1] = "0 0";
echo("Couldn't find helicopter spawn file! Spawning helicopter at 0 0.");
}
Yes it works; no that's not how you should do that. Global variables are appropriate here.
Actually best solution is to define them as global variables at the top of the file, that way you don't have the overhead of reading files every time

If they have to be in a separate file just make them global variables and exec it once.

Using local scope within executions is entirely fine in nature. The reason this isn't okay is because files should only ever be executed once -- compilation of code is slow and definitely shouldn't be repeated every time a helicopter is spawned.