Jump to content
Sign in to follow this  
Linker Split

Export text via script to a .txt file

Recommended Posts

Hello everybody.

I'm making an MP mission for my friends.

Basically, I have some cars, and they spawn randomly in the map.

What I want to do is that even after restarting the server, we could find the cars in the place where we left them.

So I thought to create a script that loops every 2 minutes that:

1) in the first moment it's running, creates a "spawn_memory.txt" file;

2) checks whatever the .txt has something written inside it, if so, load the data;

3) checks for cars if they are destroyed or not, then spawn the cars;

4) every 2 minutes wipes and re-writes in this .txt file some info about the cars (like position, status, fuel etc.);

I know that points 2 and 3 are quite easy to make, the difficult part is to find a solution to 1 and 4.

Anybody got something?

Much appreciated

Linker Split

Share this post


Link to post
Share on other sites

I think it would be easier to save all this info the the server's profileNamespace.

Share this post


Link to post
Share on other sites

I have no experience with iniDB, but that creates a dependency, so it's up to you.

Share this post


Link to post
Share on other sites

I'm getting this problem: it gives me any as argument passed.

_namespace = profileNamespace;
_nmbr = 0;

_milVeh = [
"datsun1_civil_2_covered"
];

diag_Log "start spawning cars with car.sqf";
_vehicle1 = objNull;
if (true) then
{
_nmbr = floor(random(count _milVeh));
_veh = (_milVeh select _nmbr);
diag_log format["%1",_veh];
_vehicle1 = createVehicle [_veh, [4818.0776, 2565.5906, -2.0027161e-005], [], 0, "CAN_COLLIDE"];
diag_log format["PERSISTANCE: car is %1",_vehicle1];
_vehicle1 setfuel (random 0.4);
_vehicle1 setDir -241.18762;
_vehicle1 setPos [4818.0776, 2565.5906, -2.0027161e-005];
diag_log format["PERSISTANCE: %1",PositionCar];
_namespace setVariable ["PositionCar",(position _this)];
diag_log format["PERSISTANCE: %1",PositionCar];
saveProfileNameSpace;
sleep 1;
//	_vehicle1 call fn_LoopSaveDB;
};

It returns me:

"start spawning cars with car.sqf"

"datsun1_civil_2_covered"

"PERSISTANCE: car is datsun1_civil_2_covered:2:29755"

"PERSISTANCE: any"

Share this post


Link to post
Share on other sites

You have to use getVariable:

_namespace getVariable "PositionCar"

Share this post


Link to post
Share on other sites

Man Ok I figured out to write the correct info in the .vars profile.

Now I just derapped the .vars and I found the correct info. :)

One thing now that I don't know why it's not working.

I've now created a loop function that saves every 20 seconds in the .vars file.

It's still not finished and just for testing purpose, I'm just using the diag_log function inside.

Here is fn_LoopSaveDB:

//Loop_SaveDB.sqf
diag_log "PERSISTANCE:Compiling Loop_SaveDB.sqf";
fn_LoopSaveDB = 
{
private ["_car"];
_car = _this select 0;

diag_log format["PERSISTANCE: %1",_car];

While {checkCarState} do
{
               diagLog "PERSISTANCE: saving new position";
	sleep 20;
};
};

1) this function is compiled into the init.sqf via

[] execVM "Scripts\Loop_SaveDB.sqf"

Should I use the execVM, or the compile preprocessFileLineNumbers?;

-------------------------------

2) checkCarState is a variable I set true into the init.sqf: should I save it with profileNameSpace too?

-------------------------------

3) Function is called via:

_vehicle1 call fn_LoopSaveDB

Is it right?

-------------------------------

4) It doesn't load the diagLog "PERSISTANCE: saving new position"; which is inside the brackets. what's wrong?

Share this post


Link to post
Share on other sites

1. I think with how you have it setup execVM should be fine.

2. I don't think it needs to be saved to profileNamespace?

3. I would spawn it, not call it, since you have a loop within.

4. I would check and make sure that the checkCarState variable is actually true when the function is spawned, I think that would be the only issue for that text not showing up in the logs.

Share this post


Link to post
Share on other sites

It seems that it stops at the line

_car = _this select 0;

.

before this line, diag_log shows up in the RPT, after that line, not anymore.

I'm calling the function fn_LoopSaveDB via spawn command like this:

_vehicle1 spawn fn_LoopSaveDB;

Still no results, so I simply switched to a classic function like this:

//Loop_SaveDB.sqf
diag_log "PERSISTANCE: Compiling Loop_SaveDB.sqf";
diag_log format["PERSISTANCE: Loop_SaveDB car is %1",_car];
_car = _this select 0;
ProfileNameSpace getVariable "PositionCar1";
while {CheckCarState} do
       {
      diag_log format["PERSISTANCE: Position: %1, Damage: %2",PositionCar,DamageCar];
      profileNameSpace setVariable ["PositionCar1",(position _car)];
      profileNameSpace setVariable ["DamageCar",damage _car];
      saveProfileNameSpace;
      sleep 20;
};

1) this function is called when creating for the first time a car (via script car.sqf) and it's called like this: [_vehicle1] execVM "Scripts\Loop_SaveDB.sqf";

2) should I use private ["_car"]; ? this variable _car, along with the script, is also used for other cars when updating their position.

---------------------------------

With this new initialization of the script, it works (.vars is updated every 20 seconds)

Share this post


Link to post
Share on other sites

You shouldn't have to privatize any variables because I'm assuming that your executing the script for each vehicle, which creates a new thread for the script to run on for each of those vehicles, so your local variable should be fine I would think.

Share this post


Link to post
Share on other sites
You shouldn't have to privatize any variables because I'm assuming that your executing the script for each vehicle, which creates a new thread for the script to run on for each of those vehicles, so your local variable should be fine I would think.

Yeah basically the scripts continues with other cars (I'm modifying the structure using the switch....do to check every type of car and saving their position with different variables poscar1, poscar2 etc).

Oh btw, is there a way to use setVariable and format togheter?

I mean for example I would like to create a variable _number = 0 that increases till the total number of spawned cars (30).

While checking this variable with if....then structure, the function assigns a variable called PositionCar(_number) where _number identifies the different spawned cars. this variable is linked obv to the position of the _car.

Is it possible?

I don't get how to use togheter setVariable and format command

Share this post


Link to post
Share on other sites
profileNamespace setVariable [format ["PositionCar(%1),_number], someValue];

Share this post


Link to post
Share on other sites

Ok, once I saved the position (ARRAY) to a variable called PositionCar1, I have to call it using this line?

profileNameSpace getVariable "PositionCar1";

And then, to spawn the car in that position, should I write:

_vehicle1 setPos PositionCar1;

or

_vehicle1 setPos [PositionCar1];

Share this post


Link to post
Share on other sites
_carPos = profileNamespace getVariable "PositionCar1";

_vehicle1 setPos _carPos;

Share this post


Link to post
Share on other sites

mmm man it's strange.

I inserted this:

_newpos = profileNameSpace getVariable "PositionCar1";

to take the info from the .vars file.

then an if...exitwith structure to check if the PositionCar1 is present in the .vars file:

if (isNil {_newpos select 0}) exitWith 
	{
		diag_log "PERSISTANCE: _newpos is nil! SPAWNING AT ORIGINAL POSITION THE NEW CAR-----------------------------------------";
		_vehicle1 setPos [8408, 5080, 2.5];
		_pos = position _vehicle1;
		_dam = damage _vehicle1;
		profileNameSpace setVariable ["PositionCar1",_pos];
		profileNameSpace setVariable ["DamageCar1",_dam];
		saveProfileNameSpace;
		[_vehicle1] execVM "DBSave\fn_LoopSaveDB.sqf";
	};

But even if the .vars is there with PositionCar1 saved (x,y,z), everytime I restart the server it exits with the code in the if....exitwith while it should continue loading the script which is:

_vehicle1 setPos _newpos;
_pos = position _vehicle1;
_dam = damage _vehicle1;
diag_log format["PERSISTANCE: position is %1, damage is %2",_pos,_dam];
profileNameSpace setVariable ["PositionCar1",_pos];
profileNameSpace setVariable ["DamageCar1",_dam];
saveProfileNameSpace;
[_vehicle1] execVM "DBSave\fn_LoopSaveDB.sqf";

#EDIT

I'm checking if the if...then...else works instead of the if...exitWith

#EDIT 2

I thought about a workaround.

Since _newpos returns a 3 element array, I'm trying adding:

_cnt = count _newpos;

Then the condition of the if...then structure is:

if (_cnt < 3) then

Checking now....

---------- Post added at 20:40 ---------- Previous post was at 19:36 ----------

Ok, first I changed the string to

_cnt = count [_newpos];

Because with _cnt = count _newpos; it was returning me "SCALAR".

Now it returns me always 1 even if this is not true (positioncar1 is an Array of 3 scalar items).

How is this possible?

How to modify it to count correctly the number of objects in the positioncar1 array?

Edited by Linker Split

Share this post


Link to post
Share on other sites

Doesn't the dedicated server profileNameSpace get deleted every restart / new mission? Or was that fixed?

Share this post


Link to post
Share on other sites

It's not deleting upon server restart :D

also, I'm talking about dayZ SA, but it can fits ArmA2 and 3 as well

Edited by Linker Split

Share this post


Link to post
Share on other sites

Ok, so, it seems that

	_newpos = profileNameSpace getVariable "PositionCar1";

doesn't load from my .vars profile even if in my .vars there's the item called "PositionCar1".

Damn, I get the saving process, I don't get the loading one...

Basically, even if there's an already created .vars with all the variables inside, the getVariable doesn't load them...

my .vars file is this:

version = 2;
class ProfileVariables
{
items = 5;
slots = 15;
class Item0
{
	name = "damagecar2";
	class data
	{
		class type
		{
			type[] = {"SCALAR"};
		};
		value = 0.3;
	};
	readOnly = 0;
};
class Item1
{
	name = "checkcarstate";
	class data
	{
		class type
		{
			type[] = {"BOOL"};
		};
		value = 1;
	};
	readOnly = 0;
};
class Item2
{
	name = "positioncar1";
	class data
	{
		class type
		{
			type[] = {"ARRAY"};
		};
		class value
		{
			items = 3;
			class Item0
			{
				class data
				{
					class type
					{
						type[] = {"SCALAR"};
					};
					value = 8410.954;
				};
			};
			class Item1
			{
				class data
				{
					class type
					{
						type[] = {"SCALAR"};
					};
					value = 5098.3584;
				};
			};
			class Item2
			{
				class data
				{
					class type
					{
						type[] = {"SCALAR"};
					};
					value = 0.49023438;
				};
			};
		};
	};
	readOnly = 0;
};
class Item3
{
	name = "damagecar1";
	class data
	{
		class type
		{
			type[] = {"SCALAR"};
		};
		value = 0.2;
	};
	readOnly = 0;
};
class Item4
{
	name = "positioncar2";
	class data
	{
		class type
		{
			type[] = {"ARRAY"};
		};
		class value
		{
			items = 3;
			class Item0
			{
				class data
				{
					class type
					{
						type[] = {"SCALAR"};
					};
					value = 8407.711;
				};
			};
			class Item1
			{
				class data
				{
					class type
					{
						type[] = {"SCALAR"};
					};
					value = 5079.4766;
				};
			};
			class Item2
			{
				class data
				{
					class type
					{
						type[] = {"SCALAR"};
					};
					value = 2.9933777;
				};
			};
		};
	};
	readOnly = 0;
};
};
//};

SOLVED:

I was calling the variable but I need to call the array, so I changed to:

_newpos = profileNameSpace getVariable ["positioncar1", [item0,Item1,Item2]]

So now I got with diag_log:

"PERSISTANCE: _newpos is (any),(any),(any)];"

At least a good move :D

---------- Post added at 01:18 ---------- Previous post was at 23:28 ----------

Question:

if I don't define a variable (for example checkCarState), and I then use as condition:

if (!checkCarState) then {command};

Will it return true?

Edited by Linker Split

Share this post


Link to post
Share on other sites

If you don't define it, it doesn't exist, so I think the statement will either error out, or just be skipped, now if you did a null/nil check instead, then yes it would return true.

Share this post


Link to post
Share on other sites
If you don't define it, it doesn't exist, so I think the statement will either error out, or just be skipped, now if you did a null/nil check instead, then yes it would return true.

Yeah just checked and I'll use the isNil command in the if...then code.

What I don't get is how to use getVariable and setVariable because in the Biki there's no explanation about the combination with profileNameSpace.

Do you have any hint?

I know you already helped me a lot, but really it's a set of scripts I'm trying to make since 1 week damn lol

for example, [EVO] Dan wrote in a thread about a script to spawn a box with EVH:

ArrayResp = profileNamespace getVariable ["respawnablesArray", [box1, box2]];

I don't get the array inside the first array ([box1, box2]) which I know are the name of the objects, but I don't know why he called them with getVariable.

Are box1 and box2 defined inside the profile.vars.file?

Edited by Linker Split

Share this post


Link to post
Share on other sites

For getVariable, when using its array format, the first element is the variable name, the second is the default value of that variable if it isn't defined already.

So...

profileNamespace getVariable ["CarPos", [0,0,0]];

Will check the variable "CarPos", if it is undefined, set the value of CarPos to "[0,0,0]".

Share this post


Link to post
Share on other sites

Ok, so if I've saved into .vars file the variable CheckCarState, and I put these lines in my sqf:

_checkCarState = profileNameSpace getVariable ["CheckCarState",nil];
if (isNil "_checkCarState") then

it should get its value right?

If not, it will set to nil the variable and execute the if...then code.

The point is that seems like checkCarState is not found inside the .vars profile...

As you can see, the checkcarstate is defined inside the .vars file, still the script executes the if...then code

[size=1]#define _ARMA_

version = 2;
class ProfileVariables
{
items = 3;
slots = 15;
class Item0
{
	name = "checkcarstate";
	class data
	{
		class type
		{
			type[] = {"STRING"};
		};
		value = "15761";
	};
	readOnly = 0;
};
class Item1
{
	name = "positioncar1";
	class data
	{
		class type
		{
			type[] = {"ARRAY"};
		};
		class value
		{
			items = 3;
			class Item0
			{
				class data
				{
					class type
					{
						type[] = {"SCALAR"};
					};
					value = 8407.421;
				};
			};
			class Item1
			{
				class data
				{
					class type
					{
						type[] = {"SCALAR"};
					};
					value = 5078.9536;
				};
			};
			class Item2
			{
				class data
				{
					class type
					{
						type[] = {"SCALAR"};
					};
					value = 158.56027;
				};
			};
		};
	};
	readOnly = 0;
};
class Item2
{
	name = "damagecar1";
	class data
	{
		class type
		{
			type[] = {"SCALAR"};
		};
		value = 0.3;
	};
	readOnly = 0;
};
};
[/size]

Edited by Linker Split

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
Sign in to follow this  

×