Jump to content

Recommended Posts

Hi people
I need some enlightment with this topic as I could not found any information on it.

I'm trying to create some dynamic variables on the fly in a preInit function, regarding location names, building positions for each location, road positions, etc etc. to be able to access this information later when I needed and the game is running.
Problem is, even if I define a global variable in a preInit file, I'm not being able to access that variable from the init. Also tried store variables in missionNameSpace without luck.

So, how can I grab data stored in vars created in preInit functions?
Thanks!!

Share this post


Link to post
Share on other sites

can you show us your preinit file? are you sure it's executed?

 

Share this post


Link to post
Share on other sites
3 hours ago, Niktus Acid said:

even if I define a global variable in a preInit file, I'm not being able to access that variable from the init. Also tried store variables in missionNameSpace

Global and missionNamespace variables are the same thing.

 

As @gc8 says, are you sure your function is being executed? show us your code and your functions library definition.

There should be no problem accessing variables initialised from a preInit function.

  • Like 1

Share this post


Link to post
Share on other sites

This is my function loaded in a preInit

private ["_all_locations"];
_all_locations = nearestLocations [[worldSize/2,worldSize/2], ["NameCity","NameCityCapital","NameVillage"], worldSize];

// PARSE LOCATIONS AND COMPOSE VARIABLE
{
    // NAME
    _name = text _x;
    // UNIFY CHARACTERS FOR TWO WORD LOCATIONS
    _split = _name splitString " "; _name = _split joinString "";
    systemChat format["... parsing %1 ...",_name];
    // CENTER - insert in final var
    _center = locationPosition _x;

    // FIND BUILDINGS
    _buildings = _center nearObjects ["building", 500];
    // FILTER BUILDINGS ENTERABLE - insert in final var
    _buildings_enterable = [];
    { if ( [_x] call BIS_fnc_isBuildingEnterable ) then { _buildings_enterable pushBack _x; }; } forEach _buildings;
    // REDUCE BUILDINGS NUMBER IF THERE ARE MORE THAN 50
    if (count _buildings_enterable > 50) then {
        _work = _buildings_enterable;
        _buildings_enterable = [];
        _buildings_enterable = [_work,count _work,["RANDOM_REMOVE",[0.5]]] call eze_fnc_math_array_filter;
    };
    // GRAB BUILDINGS POSITIONS
    _buildings_positions = [];
    { _getPos = getPos _x; _buildings_positions pushBack _getPos; } forEach _buildings_enterable;

    // PEDESTRIANS PART ////////////////////////////////////////////////////////////////////////////// 

    // FILTER BUILDING DISTANCES FOR PEDESTRIANS
    _buildings_filtered = [_buildings_positions,count _buildings_positions,["MIN_DISTANCE",[50]]] call eze_fnc_math_array_filter;
    // PEDESTRIAN POSITIONS - insert in final var
    _positions_pedestrians = [];
    { 
        _findPos = _x findEmptyPosition [0,30]; 
        if (count _findPos > 0) then { _positions_pedestrians pushBackUnique _findPos; }; 
    } forEach _buildings_filtered;

    // VEHICLES PART ////////////////////////////////////////////////////////////////////////////// 

    // FILTER BUILDING DISTANCES FOR CARS
    _buildings_filtered = [_buildings_positions,count _buildings_positions,["MIN_DISTANCE",[100]]] call eze_fnc_math_array_filter;
    // FIND NEAR ROADS  - insert in final var
    roads_positions = [];
    // ADD AT LEAST ONE ROAD FOR CITY CENTER
    _near_roads = _center nearRoads 50; 
    if (count _near_roads > 0) then {
        _select = selectRandom _near_roads;
        _roadPos = getPos _select;
        roads_positions pushBackUnique _roadPos;
    };
    // ADD ROADS NEAR BUILDINGS	
    {
        _thisPos = _x; 
        _near_roads = _thisPos nearRoads 50;
        if (count _near_roads > 0) then {
            _select = selectRandom _near_roads;
            _roadPos = getPos _select;
            roads_positions pushBackUnique _roadPos;
        };
    } forEach _buildings_filtered;

    // COMPOSE FINAL VARIABLE //////////////////////////////////////////////////////////////////////////////

    _var_name = "eze_fw_" + _name;
    _compose = [_center,_buildings_enterable,_positions_pedestrians,roads_positions];

    missionNamespace setVariable [_var_name,_compose];
} forEach _all_locations;

systemChat "... parsing finished...";

Basically I'm finding buildings around each map location, and composing different position elements in an array that is called "eze_fw_" + location name
End result in one chernarus location end like this: eze_fw_zelenogorsk = [ [CENTER POS],[BUILDING OBJECTS],[PEDESTRIAN POSITIONS],[ROAD POSITIONS] ];

When I run this after init, everything works perfect and I can access all my variables, from preInit, those variables doesn't exist.
If you wonder about the call function eze_fnc_math_array_filter, I'm also adding that in the same file, so all the code needed is right there.

But what annoys me is that even if I declare in preInit something like
dummy_var = "Hello";
I can't call it from the init, doesn't exists.

I'm not using the functions library, I have created my own framework of function which I compile in the first lines of the init to gain access accross all my needed functions.
I'm still thinking about the PROS and CONS to mount everything in a functions library, but at the moment, my description.ext looks like this

 

#include "eze_fw\DIALOG\defines.hpp"
#include "eze_fw\DIALOG\dialog.hpp"

class CfgFunctions {
	class eze {
		class world {
			class treesRemove {
				file = "eze_fw\FNC\eze_fnc_world_pre.sqf";
				preInit = 1;
			};
			class parseLocations {
				file = "eze_fw\FNC\eze_fnc_locations_pre.sqf";
				preInit = 1;
			};
		};
	};
};

 

My function to massively reduce or add trees and other map elements before mission starts works perfectly fine, but I'm solving everything right there, no need to create anything that I will call later.
I can't say I'm sure that is running because I can't see anything, not even an error pop up,  but my other preInit function runs perfect, and I'm following same process.

Thanks for your time to give this a look guys! Really appreciate your comments

 

 

 

 

 

Share this post


Link to post
Share on other sites

 

9 hours ago, Niktus Acid said:

This is my function loaded in a preInit

 

On quick view of your code I I noticed only one global variable "roads_positions" in there. Is the posted code only thing what's in the sqf?

you should be able to put roads_positions in init fields...

  • Thanks 1

Share this post


Link to post
Share on other sites

Oh, looks like a typo, it is supposed to be a private variable for internal use (_roads_positions), it works anyway of course
Final variables are constructed here:
missionNamespace setVariable [_var_name,_compose];

Share this post


Link to post
Share on other sites
1 hour ago, Niktus Acid said:

Oh, looks like a typo, it is supposed to be a private variable for internal use (_roads_positions), it works anyway of course
Final variables are constructed here:
missionNamespace setVariable [_var_name,_compose];

 

actually nevermind I was wrong

 

 

I don't really understand what you are trying to do. what do you put in the units init line?

Share this post


Link to post
Share on other sites

This whole thing is about to have really well organized, secured and filtered data variables with map information, and most of all, gain performance... because I'm always using the engine on the limit of what FPS allows me, so, if a I can grab all this data before mission starts is even better... so I don't need to bother to find buildings, or roads or any secure position when game is running as I already have anything that any unit could need.

 

For this particular case, I'm going to use this data to populate civilian life, people and vehicles moving around, randomly. Each agent has a loop function that checks periodically its current position, and when they reach their destination, they choose another one randomly from an array of destinations available to that particular unit.


I just need one example of a variable created in a function triggered in preInit, that I could later retrieve from the init.
That's all I need, and I'm sure this could be perfectly doable, I just don't know what am I missing here.



 

Share this post


Link to post
Share on other sites

Works fine for me from preInit if I do a variation on your code( as I don't have your eze_fnc_math_array_filter function ).

TEST_MISSION

 

Test_mission is on Stratis.

In debug console using...

allVariables missionNamespace select{ _x select[0,3] == "eze" }

...returns...

["eze_fw_kaminofiringrange","eze_fw_girna","eze_fw_stratisairbase","eze_fw_camprogain","eze_fw_agiamarina","eze_fw_airstationmike-26","eze_fw_camptempest","eze_fw_campmaxwell"]

...where each variable holds...

//eze_fw_kaminofiringrange
[
	[6401.97,5427.13,-7.4887],
	[
		1b306dd8b80# 28932: cargo_patrol_v1_f.p3d,
		1b33151f580# 29134: cargo_house_v1_f.p3d,
		1b33201ab00# 29460: cargo_house_v1_f.p3d,
		1b33151c100# 29135: cargo_house_v1_f.p3d,
		1b33201a080# 29461: miloffices_v1_f.p3d,
		1b332cc1600# 29658: cargo_patrol_v1_f.p3d,
		1b306d17580# 29664: i_house_small_03_v1_f.p3d,
		1b306d16080# 29717: cargo_house_v1_f.p3d,
		1b306d4ab00# 39251: u_addon_02_v1_f.p3d,
		1b306d48b80# 39383: lighthouse_f.p3d,
		1b32abc5600# 27166: unfinished_building_02_f.p3d,
		1b32abc7580# 26959: d_stone_shed_v1_f.p3d,
		1b33edb2b00# 39317: slum_house01_f.p3d,
		1b33edb3580# 39316: i_stone_shed_v1_f.p3d,
		1b3294b6b00# 27208: unfinished_building_01_f.p3d,
		1b32b1c2b00# 27122: d_stone_housebig_v1_f.p3d,
		1b33ee90b80# 40379: slum_house02_f.p3d,
		1b32adb1600# 26962: d_stone_housesmall_v1_f.p3d,
		1b32b1c2080# 27132: metal_shed_f.p3d,
		1b33ee90100# 40390: i_stone_housesmall_v1_f.p3d
	],
	[],
	[]
]

Two blank arrays at the end are just where I do not have your function so I just push a blank array into the data for pedestrians and roads.

  • Like 2
  • Thanks 1

Share this post


Link to post
Share on other sites

Woow, that's awesome Larrow... it works pitch perfect
And I'm checking your code changes too, need to study those techniques... I'm not a pro, just a big enthusiast
I would buy you a beer but I don't have my paypal with funds from a long time


I've learned a lot from your forum comments in many different topics, thank you so much!

Share this post


Link to post
Share on other sites
1 hour ago, Niktus Acid said:

so I don't need to bother to find buildings, or roads or any secure position when game is running as I already have anything that any unit could need.

 

I understand. I was just curious of what you put in the init line.. but Larrow already probably helped you

Share this post


Link to post
Share on other sites

He did, I still don't completely understand what did the trick. I suppose you need to properly declare your function in the library

My init line is quite complex. What I do basically in this civilian module is activate locations by player's distance, and then moving agents around the player all the time, so you can simulate like the whole world is more or less alive, without killing your FPS.
In my old FX8350, looks like 60 pedestrians and 20 cars allows me to barely keep 30 FPS.

Then you have to take care the whole time that no car kill the smart agents that loves to walk at roads center... another story 🙂

Share this post


Link to post
Share on other sites
14 minutes ago, Niktus Acid said:

My init line is quite complex.

 

To have simple init line I usually do this:

 

in preinit script I have this function declared:

initGuard =
{
params ["_man"];


_man setVariable ["isGuard", true]; // Just an example


}:

 

And in init field of the man I put this:

 

_this call initGuard;

 

It's a bit different way on what function scripts are supposed to be used but it works

  • Like 1

Share this post


Link to post
Share on other sites

That's a nice approach, it should work perfectly
I'm just starting to playing around with preInit functions, never needed to do so, but I discovered that if you really want to parse and process data in massive arrays from a map, it is the way to go.

 


 

Share this post


Link to post
Share on other sites
1 hour ago, Niktus Acid said:

I discovered that if you really want to parse and process data in massive arrays from a map, it is the way to go

Or postInit behind a loading screen, where scripts will run full speed, also due to initialisation order it means all objects have already been initialised if you need access to them or their data.

Quote

While loading screen is shown, simulation and scene drawing is disabled, user control is disabled, mouse cursor is hidden, scripts run at full speed (50ms per frame instead of 3ms per frame for Scheduled Scripts).

 

  • Like 1

Share this post


Link to post
Share on other sites

Another great advise, I didn't knew about the loading screen command... I'm gonna use it
Thanks again Larrow, you are the man!

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

×