Jump to content
dupa1

profileNameSpace shenanigans

Recommended Posts

I am trying to save player scores into profileNamespace, so they can be restored after server restart. 

The main loop constantly updates scoresArr with player scores. The saveScores function saves is into profileNameSpace. 

The main loop seems to work fine. When I call the saveScores function, any subsequent updates to scoresArr seem to affect the saved data in the profileNameSpace. 

 

Here is a video of this:

 

And the code:

//initServer.sqf
scoresArr = [];

saveScores =
{
  profileNameSpace setVariable ["saveData", scoresArr];
  saveProfileNameSpace;
  "Done!";
};

while {true} do
{
  {
    _scoreInfo = getPlayerScores _x;
    _uid = getPlayerUID _x;
    _uids = scoresArr apply {_x select 0};
    _i = _uids find _uid;
    if (_i == -1) then {scoresArr pushBack [_uid, _scoreInfo]} else {scoresArr set [_i, [_uid, _scoreInfo]]};
  } forEach allPlayers;
  sleep 1;
};

 

Share this post


Link to post
Share on other sites

Update:

This happens when I use the "set" command, this must be some kind of a bug. I rewrote the main loop without using the "set" command and it seems to work... for now...

//initServer.sqf
scoresArr = [];

saveScores =
{
  profileNameSpace setVariable ["saveData", scoresArr];
  saveProfileNameSpace;
  "Done!";
};


while {true} do
{
  _tempArr = [];
  {
    _uid = getPlayerUID _x;
    _scoreInfo = getPlayerScores _x;
    _found = false;
    {if (_x#0 == _uid) then {_tempArr pushBack [_uid, _scoreInfo]; _found = true} else {_tempArr pushBack _x}} forEach scoresArr;
    if !_found then {_tempArr pushBack [_uid, _scoreInfo]};
  } forEach allPlayers;

  scoresArr = _tempArr;
  sleep 1;
};

 

Share this post


Link to post
Share on other sites
22 hours ago, dupa1 said:

When I call the saveScores function, any subsequent updates to scoresArr seem to affect the saved data in the profileNameSpace. 

 

This has nothing to do with profileNamespace specifically.

 

This is cause you are setting the profileNamespace-variable "saveData" to the same data in memory as scoresArray points to. Remember: array variables are just reference holders to sequences of data. set updates the data inplace.

 

In your second version your are creating a new array in memory using _tempArr = [];

and then you essentially copy the data to it, hence the problem disappears.

 

An example:

a = [1]; // "[]" creates the array in memory and returns the address to it,  then "a =" defines the variable "a" and assigns the address to it.

b = a; // "b" now contains the same adress as "a", both variables point to the same data

b pushback 2; // adds an element to the data, "a select 1" would return 2.

 

 

Edit: Note that since profileNamespace is essentially a loaded copy of the contents of the profile-file on the harddrive you would still have to use saveProfilenamespace again after you update the scores to save the changes to harddrive.

  • Like 2

Share this post


Link to post
Share on other sites

Thank you for replying. I am not certain you are correct.

a = 1;
b = a;
b = b + 1;

The above code should not modify A. There should be 2 instances of the same data, one of each is later modified. I would expect the same from profileNamespace variables. 

I did quite a lot of testing on this. I tried using a temporary variable, with the same result. From my observations, using the set command specifically causes this problem.

I managed to build the array without using the set command and now it seems to work OK.... (the second version of the code has some mistakes)

 

Edit:

I just tried your code in the console, and it did what you said.

I tried my code in the console, and it did what I said.

 

So, it seems that arrays and scalar values are treated differently. This is new to me. Thanks for the help.

 

Share this post


Link to post
Share on other sites
2 hours ago, dupa1 said:

So, it seems that arrays and scalar values are treated differently. This is new to me. Thanks for the help.

 

 

https://community.bistudio.com/wiki/Array#Reserved_variable_in_expression

 

The reason why you need to add a plus (+) if you don't want to modify the former array, or fail by reserved variable.

  • Like 1

Share this post


Link to post
Share on other sites

And again, big thanks!

Share this post


Link to post
Share on other sites
12 hours ago, dupa1 said:

, it seems that arrays and scalar values are treated differently

 

Correct, the described behaviour is the main difference between reference types (arrays and hashmaps) and value types (scalars). This actually a pretty common feature in many languages and is not unique to SQF.

Share this post


Link to post
Share on other sites

Well, I've been messing around with ArmA coding for a long time, since SQS days. The mission I'm making now is my first serious project. I feel as if I just discovered what my schlong is for at the age of 30.

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

×