Ok. Something is driving me crazy here. If someone can help.
Background:
- in my script we have a GoalTown class, with an instance per town. For each instance there are several data, such as GoalTown.tgr_array (an array containing 8 integer elements).
- for some reason (mainly for save purpose) there also is a global table (::TownDataTable). In this table I store some of the GoalTown data. In this general table, for each town we have: key = town id; element = table with different data (such as tgr_array).
Here is how ::TownDataTable is updated each month:
Code: Select all
/* Main town management function. Called each month. */
function GoalTown::MonthlyManageTown()
{
[...]
::TownDataTable[this.id] = this.CollectingTownData();
}
/* Function called to collect town data. */
function GoalTown::CollectingTownData()
{
local town_data = {};
town_data.sign_id <- this.sign_id;
town_data.is_monitored <- this.is_monitored;
town_data.last_pax_delivery <- this.last_pax_delivery;
town_data.town_supplied <- this.town_supplied;
town_data.town_goals_cat <- this.town_goals_cat;
town_data.town_supplied_cat <- this.town_supplied_cat;
town_data.town_stockpiled_cat <- this.town_stockpiled_cat;
town_data.tgr_array <- this.tgr_array;
return town_data;
}
Note that ::TownDataTable is built a first time when initializing GoalTown instances the first time. After this, each time I reload a savegame, is it rebuilt from saved data, like that:
Code: Select all
foreach (townid, town_data in saved_data.town_data_table) {
::TownDataTable[townid] <- town_data;
}
Now the problem. At some point, the content of ::TownDataTable changes without me understanding why. Follows an illustration code. The relevant line is the one in the middle (this.tgr_array...). To understand what happens there: here, the GoalTown.tgr_array is updated and its 8th element (i=7), whose value is 0 until there, receives a positive value (> 0). For some reason that I don't get, the array which is stored in ::TownDataTable also changes. All the code which is before and after the central line is only there to put that in evidence in the Debug window.
Code: Select all
function GoalTown::MonthlyManageTown()
{
[...]
// For debug purpose
Log.Info("MAIN ARRAY TEST 1", Log.LVL_DEBUG);
local array_text = " ";
foreach (i, val in ::TownDataTable[this.id].tgr_array) {
array_text = array_text+"i"+i+"="+val+" ";
}
Log.Info(GSTown.GetName(this.id)+": "+array_text+"Average="+this.tgr_average, Log.LVL_DEBUG);
this.tgr_array[i] = new_town_growth_rate;
// For debug purpose
Log.Info("MAIN ARRAY TEST 2", Log.LVL_DEBUG);
local array_text = " ";
foreach (i, val in ::TownDataTable[this.id].tgr_array) {
array_text = array_text+"i"+i+"="+val+" ";
}
Log.Info(GSTown.GetName(this.id)+": "+array_text+"Average="+this.tgr_average, Log.LVL_DEBUG);
[...]
}
Here is the result in my Debug window. As you can see, the 8th element of the array changed value:
Code: Select all
dbg: [script] [18] [I] [1940-07-01] MAIN ARRAY TEST 1
dbg: [script] [18] [I] [1940-07-01] Filzbach: i0=851 i1=10000 i2=217 i3=217 i4=217 i5=217 i6=217 i7=0 Average=(null : 0x(nil))
dbg: [script] [18] [I] [1940-07-01] MAIN ARRAY TEST 2
dbg: [script] [18] [I] [1940-07-01] Filzbach: i0=851 i1=10000 i2=217 i3=217 i4=217 i5=217 i6=217 i7=10000 Average=(null : 0x(nil))
What am I missing here ? Probably something completely stupid/evident... But I can't figure out. I attach here a version of the entire script if someone wants have a look.
Cheers.