Author Topic: File Writing - Overwrite  (Read 771 times)

Well, I made an easy Npc spawn creator. When I already have an initial spawn in the list and I attempt to create another the following happens.

Pre-Result:
Quote
1 - This is the spawncount.
Trader PosX PosY PosZ - This is normal.

After adding another spawn Result:
Quote
2
Orc PosX PosY PosZ - Trader is overwritten by Orc.
Orc PosX PosY PosZ - Orc is placed on the last line as well.

Well, I know how this happens but have no idea how to fix it without assigning a specific Global variable to each spawn, which I would rather not do.

Command:
Code: [Select]
function serverCmdNPCSpawn(%client,%type)
{
%Pos = %client.player.getPosition();
%file = new FileObject();
%file.openForWrite("Config/Server/Medieval/NPC/Spawns.txt");
$SpawnCount += 1;
%file.writeline($SpawnCount);

while(%Saved <= $SpawnCount)
{
%Saved += 1;
%file.writeline(%Type SPC %Pos);
}

%Saved = 0;
%file.close();
%file.delete();
}

Code: [Select]
$NPCSpawnCount = 0;
function serverCmdNPCSpawn(%client,%type)
{
$NPCSpawnData[$NPCSpawnCount] = %type SPC isObject(%client.player) ? %client.player.getPosition() : "0 0 0";
$NPCSpawnCount++;
writeNPCSpawn();
}
function writeNPCSpawn()
{
%file = new FileObject();
%file.openForWrite("config/server/Medieval/NPC/Spawns.txt");
%file.writeLine($NPCSpawnCount);
for(%i=0;%i<$NPCSpawnCount;%i++)
{
%file.writeLine($NPCSpawnData[%i]);
}
%file.close();
%file.delete();
}
Try using my code, there are heaps of things wrong with your code it's probably faster to use this.

Alright, thanks.

One more question though. The following code does not create a Minigame on run, do I have to initialize it or something?

Code: [Select]

function SetupRPMinigame(%client)
{
echo("Creating Minigame");
new ScriptObject($DediMini)
{
brickDamage= 0;
brickRespawnTime= 0;
class=miniGameSO;
colorIdx= 0;
EnableBuilding= 1;
EnablePainting= 1;
enableWand= 1;
fallingDamage= 1;
inviteOnly= 0;
numMembers=1;

playerDataBlock= PlayerMultiSlotNoJetArmor;
PlayersUseOwnBricks= 0;
Points_BreakBrick= 0;
Points_Die= 0;
Points_KillPlayer= 0;
Points_KillSelf= 0;
Points_PlantBrick= 0;
respawnTime= 5000;
SelfDamage=1;


StartEquip0=0;
StartEquip1=0;
StartEquip2=0;
StartEquip3=0;
StartEquip4=0;

title = "Medieval Roleplay";
useAllPlayersBricks= 0;
useSpawnBricks= 0;
VehicleDamage= 0;
vehicleReSpawnTime= 0;
weaponDamage= 1;
};
}
« Last Edit: December 25, 2009, 11:20:46 PM by Desolation »

Code: [Select]
...
$DediMini = new ScriptObject()
...

Odd, it still appears to not be working.

Also, I tried hosting a normal and a Dedicated server.


Nevermind, I forgot to add the player to the minigame.  :cookieMonster:

Thanks.
« Last Edit: December 25, 2009, 11:38:36 PM by Desolation »

%file.openForAppend("path") writes to the end of the file instead of overwriting it.

%file.openForAppend("path") writes to the end of the file instead of overwriting it.

The first line in the file is the NPC count, so appending wouldn't help much.
(Of course, he could just change it so the number of lines is the count...)