Community Forum

How to change pricePerHa

Forum Overview >> Scripting

CategoryScripting
Created04.01.2019 08:07


Kaj-aage Henneberg (kahfs) 04.01.2019 08:07
Hi, I'm in the process of creating my first mod, at set of lua scripts to set game parameters to my personal liking. I've managed to change the values of a number of parameter groups such as buy and sell price for animals. But I'm stuck with the purchase price for land.

Here are some lines from a recursive print of g-currentMission:

2018-12-23 14:19 . farmlandManager :: table: 0x0212d30a63c0
2018-12-23 14:19 . farmlandMapping :: table: 0x0212d30a64d0
2018-12-23 14:19 . loadedMapData :: false
2018-12-23 14:19 . stateChangeListener :: table: 0x0212d30a6518
2018-12-23 14:19 . localMapHeight :: 0
2018-12-23 14:19 . numberOfBits :: 8
2018-12-23 14:19 . sortedFarmlandIds :: table: 0x0212d30a6488
2018-12-23 14:19 . localMapWidth :: 0
2018-12-23 14:19 . farmlands :: table: 0x0212d30a6408

However, if I try to print recursively like this:

myTest = {};

function myTest:loadMap(name)
local myTable = {};
print("myTest-----------------------------------");

myTable = g_currentMission.farmlandManager();
--g_currentMission.farmlandManager.pricePerHa = 25000;

DebugUtil.printTableRecursively(myTable,"_",0,5)
print("myTest-----------------------------------");
end;

function myTest:keyEvent(unicode, sym, modifier, isDown)
end;
function myTest:update(dt)
end;
function myTest:draw()
end;
function myTest:deleteMap()
end;
function myTest:mouseEvent(posX, posY, isDown, isUp, button)
end;

addModEventListener(myTest);

I get the message: attempt to call field 'farmlandManager' (a nil value)

The value I want to change can be found in a map specific file: mapDE_farmlands.xml

<?xml version="1.0" encoding="utf-8" standalone="no" ?>
<map>
<farmlands densityMapFilename="$data/maps/mapDE/mapDE_farmland.grle" numChannels="6" pricePerHa="20000">

As you can see, I have tried to reduce pricePerHa from 60000 to 20000, within the file itself. Bt I would like to be able to set this value from within my lua script.

Unfortunately, I'm very inexperienced, both with modding and with LUA programming, so I would be very greatful for some suggestions.

Bilbo Beutlin (BBeutlin) 04.01.2019 15:46
1. at time of 'loadMap()' the game structures are not built completely yet. You had to use a later moment, eg. 'loadMapFinished()' or even more later.

2. you have written "farmlandManager()" - this is not a function, but a table which you have to examine first.

Jos Kuijpers (Unknown) 04.01.2019 18:18
It is g_farmlandManager

Kaj-aage Henneberg (kahfs) 04.01.2019 22:29
Thank you for your advice Bilbo. I managed to reset pricePerHa using g_farmlandManager.pricePerHa = 10000; and also to print out the new value of pricePerHa to the log file. But I had to do it within the myTest:update(dt) function. Is that what you meant by "do it at a later moment"? It seems overkill to update a value that only needs to be defined once.

To my surprise it had not effect on the field prices in the field buy and sell menu. Apparently another table is used for that.

Jos Kuijpers (Unknown) 06.01.2019 14:36
Your best bet it to override FarmlandManager:getPricePerHa() function as early as possible. Return a new pricePerHa. then you know you're not too late (which you are becase you say it is not updated) and not too early.


Kaj-aage Henneberg (kahfs) 11.01.2019 18:32
Thank you for your feedback Jos. Didn't see it as I was busy with other parts of my mod. The pricePerHa might actually have been set correct. I was judging it based on the purchase value and the arable area of the field. The total land area you buy is often much bigger, and it is only recently, I figured out how to get that area.

Brian Thompson (Unknown) 09.08.2019 12:37
Could you post the changes made? I'm trying to do something similar to reduce the price of fields to be a little more realistic.

Kaj-aage Henneberg (kahfs) 11.08.2019 08:07
Hi Brian,
Here is a piece of code to get you started.

if g_fieldManager.fields ~= nil then
g_farmlandManager.pricePerHa = RN.pricePerHa; -- set new value value
local randomness = 1; -- set to zero for no fluctuation, set to 1 for +/- 5% fluctuation
for k,field in pairs(g_fieldManager.fields) do
local farmland = field.farmland;
local farmlandArea = farmland.areaInHa;
local farmlandId = farmland.id;
local randomFieldPriceFactor = 1 + randomness * 1 * (math.random()-0.5)/10;
farmland.price = randomFieldPriceFactor * RN.fieldPriceList[farmlandId] * farmlandArea;
local fieldId = field.fieldId;
if field.fieldArea < 0.15 then
field.fieldArea = farmlandArea; -- if field area is set to a very low value it is reset to the farmland area
end;
end;
end;

The array RN.fieldPriceList[farmlandId] is defined elsewhere. The name is confusing as it is the price of farmlands not fields.

A couple of pointers. the field manager is not loaded when mods are loaded, so I run the piece of code inside an update function.
You buy areas of land, not fields.

Brian Thompson (Unknown) 13.08.2019 21:49
Thanks Kaj. I'd seen that code after I searched more and found your mod RealLifeNumbers! Do you plan to update that to work with Seasons mod?

Brian Thompson (Unknown) 13.08.2019 22:16



Note: Log in to post. Create a new account here.