Community Forum

Help with LUA

Forum Overview >> Scripting

CategoryScripting
Created12.06.2017 17:35


Timmy Billings (Unknown) 12.06.2017 17:35
my lua shows no errors but not working correct..when i save game go back inside my odometer resets to zero.. i dont want it to reset.. i want it to continue from where i saved game .any help would be greateful thanks.. heres the lua..


Odometer = {
modDir = g_currentModDirectory;
};

function Odometer.prerequisitesPresent(specializations)
return SpecializationUtil.hasSpecialization(Drivable, specializations);
end;

local function pxToNormal(dimension, n, halfSize)
local ret = n / 1080;
if dimension == 'x' then
ret = ret / g_screenAspectRatio
end;
if halfSize then
return ret * 0.5;
end;

return ret;
end;

function Odometer:load(xmlFile)
self.Odometer = {};

self.Odometer.current = 0;
self.Odometer.total = 0;

self.Odometer.width = pxToNormal('x', 512, true);
self.Odometer.height = pxToNormal('y', 64, true);
self.Odometer.posX = Utils.getNoNil(getXMLFloat(xmlFile, 'vehicle.odometer#posX'), pxToNormal('x', 1587)); -- 0.5
self.Odometer.posY = Utils.getNoNil(getXMLFloat(xmlFile, 'vehicle.odometer#posY'), pxToNormal('y', 194)); -- 1 - self.Odometer.height * 1.25
self.Odometer.hud = Overlay:new('OdometerOverlay', Utils.getFilename('odometerHud.png', Odometer.modDir), self.Odometer.posX, self.Odometer.posY, self.Odometer.width, self.Odometer.height);

self.Odometer.textCurrentPosX = self.Odometer.posX + pxToNormal('x', 182, true);
self.Odometer.textTotalPosX = self.Odometer.posX + pxToNormal('x', 486, true);
self.Odometer.textPosY = self.Odometer.posY + pxToNormal('y', 18, true);
self.Odometer.fontSize = pxToNormal('y', 36, true);
end;

function Odometer:delete()
self.Odometer.current = 0;
if self.Odometer.hud then
self.Odometer.hud:delete();
self.Odometer.hud = nil;
end;
end;

function Odometer:readStream(streamId, connection)
self.Odometer.current = streamReadFloat32(streamId);
self.Odometer.total = streamReadFloat32(streamId);
end;

function Odometer:writeStream(streamId, connection)
streamWriteFloat32(streamId, self.Odometer.current);
streamWriteFloat32(streamId, self.Odometer.total);
end;

function Odometer:mouseEvent(posX, posY, isDown, isUp, button) end;
function Odometer:keyEvent(unicode, sym, modifier, isDown) end;

function Odometer:update(dt)
if self:getIsActive() or self.isHired or self.drive or (self.getIsCourseplayDriving and self:getIsCourseplayDriving()) then
self.Odometer.current = self.Odometer.current + self.lastMovedDistance;
self.Odometer.total = self.Odometer.total + self.lastMovedDistance;
end;
end;

function Odometer:updateTick(dt) end;

function Odometer:draw()
self.Odometer.hud:render();

local current = ('%.1f'):format(g_i18n:getDistance(self.Odometer.current * 0.001));
local total = ('%.1f'):format(g_i18n:getDistance(self.Odometer.total * 0.001));

setTextAlignment(RenderText.ALIGN_RIGHT);
setTextBold(false);
setTextColor(1,1,1, 1);

renderText(self.Odometer.textCurrentPosX, self.Odometer.textPosY, self.Odometer.fontSize, current);
renderText(self.Odometer.textTotalPosX, self.Odometer.textPosY, self.Odometer.fontSize, total);

-- reset
setTextAlignment(RenderText.ALIGN_LEFT);
end;

function Odometer:loadFromAttributesAndNodes(xmlFile, key, resetVehicles)
if not resetVehicles then
self.Odometer.total = getXMLFloat(xmlFile, key .. '#odometer') or self.Odometer.total;
end;

return BaseMission.VEHICLE_LOAD_OK;
end;

function Odometer:getSaveAttributesAndNodes(nodeIdent)
return ('odometer=%q'):format(tostring(self.Odometer.total)), nil;
end;

Bilbo Beutlin (BBeutlin) 13.06.2017 00:55
On first view: you're using obsolete functions.

"load()" now uses "savegame" as parameter. For access to your saved xml config datas use "savegame.xmlFile".

"loadFromAttributesAndNodes()" isn't used anymore. Place your code in "load()", "preLoad()" or "postLoad()" instead.

See for reference the documentation for specs.

Timmy Billings (Unknown) 13.06.2017 05:34
I did what you said i think, but its still resetting to zero after i resume a saved game.. heres latest changes

--[[
@title: Odometer
@authors: unknown (original spec), Jakob Tischler
@version: 1.2
@date:
@history: v1.0: original release spec
v1.1: * make specialization global
* fix distance ratio for regular and 4x maps
* add gui for Giants' "newGUI" (patch 2.0 and higher)
v1.11 (10 May 2014): * fix multiple savegame loading/deleting error
v1.2 (28 Nov 2014): * adapt to FS15, new hud, use Courseplay API function
]]

Odometer = {
modDir = g_currentModDirectory;
};

function Odometer.prerequisitesPresent(specializations)
return SpecializationUtil.hasSpecialization(Drivable, specializations);
end;

local function pxToNormal(dimension, n, halfSize)
local ret = n / 1080;
if dimension == 'x' then
ret = ret / g_screenAspectRatio
end;
if halfSize then
return ret * 0.5;
end;

return ret;
end;

function Odometer:load(savegame)

self.Odometer = {};

self.Odometer.current = 0;
self.Odometer.total = 0;

self.Odometer.width = pxToNormal('x', 512, true);
self.Odometer.height = pxToNormal('y', 64, true);
self.Odometer.posX = Utils.getNoNil(getXMLFloat(xmlFile, 'vehicle.odometer#posX'), pxToNormal('x', 1587)); -- 0.5
self.Odometer.posY = Utils.getNoNil(getXMLFloat(xmlFile, 'vehicle.odometer#posY'), pxToNormal('y', 194)); -- 1 - self.Odometer.height * 1.25
self.Odometer.hud = Overlay:new('OdometerOverlay', Utils.getFilename('odometerHud.png', Odometer.modDir), self.Odometer.posX, self.Odometer.posY, self.Odometer.width, self.Odometer.height);

self.Odometer.textCurrentPosX = self.Odometer.posX + pxToNormal('x', 182, true);
self.Odometer.textTotalPosX = self.Odometer.posX + pxToNormal('x', 486, true);
self.Odometer.textPosY = self.Odometer.posY + pxToNormal('y', 18, true);
self.Odometer.fontSize = pxToNormal('y', 36, true);
end;

function Odometer:delete()
self.Odometer.current = 0;
if self.Odometer.hud then
self.Odometer.hud:delete();
self.Odometer.hud = nil;
end;
end;

function Odometer:readStream(streamId, connection)
self.Odometer.current = streamReadFloat32(streamId);
self.Odometer.total = streamReadFloat32(streamId);
end;

function Odometer:writeStream(streamId, connection)
streamWriteFloat32(streamId, self.Odometer.current);
streamWriteFloat32(streamId, self.Odometer.total);
end;

function Odometer:mouseEvent(posX, posY, isDown, isUp, button) end;
function Odometer:keyEvent(unicode, sym, modifier, isDown) end;

function Odometer:update(dt)
if self:getIsActive() or self.isHired or self.drive or (self.getIsCourseplayDriving and self:getIsCourseplayDriving()) then
self.Odometer.current = self.Odometer.current + self.lastMovedDistance;
self.Odometer.total = self.Odometer.total + self.lastMovedDistance;
end;
end;

function Odometer:updateTick(dt) end;

function Odometer:draw()
self.Odometer.hud:render();

local current = ('%.1f'):format(g_i18n:getDistance(self.Odometer.current * 0.001));
local total = ('%.1f'):format(g_i18n:getDistance(self.Odometer.total * 0.001));

setTextAlignment(RenderText.ALIGN_RIGHT);
setTextBold(false);
setTextColor(1,1,1, 1);

renderText(self.Odometer.textCurrentPosX, self.Odometer.textPosY, self.Odometer.fontSize, current);
renderText(self.Odometer.textTotalPosX, self.Odometer.textPosY, self.Odometer.fontSize, total);

-- reset
setTextAlignment(RenderText.ALIGN_LEFT);
end;

function Odometer:postLoad(savegame)
if self.Odometer then
if savegame ~= nil and not savegame.resetVehicles then
self.Odometer.currentAngle = Utils.getNoNil(getXMLFloat(savegame.xmlFile, savegame.key.."#currentAngle"), self.Odometer.minAngle);
end
end;

return BaseMission.VEHICLE_LOAD_OK;
end;

function Odometer:getSaveAttributesAndNodes(nodeIdent)
return ('odometer=%q'):format(tostring(self.Odometer.total)), nil;
end;

Timmy Billings (Unknown) 16.06.2017 15:32
Still needing help please!!!!

Bilbo Beutlin (BBeutlin) 17.06.2017 11:59
I'm afraid, you won't succeed with your current level of LUA programming.
Things you had to see on first view:
- the comment of orig. author says: "use Courseplay API function"
- there are still obsolete references to "xmlFile" instead "savegame.xmlFile"
- there is a variable "lastMovedDistance" which is nowhere calculated, but required for the saved xml "Odometer.total"

If you want to port a script to FS17, you MUST have good knowledge in scripting and FS APIs.

Timmy Billings (Unknown) 17.06.2017 15:37
Well i tried is all i can say.. Any yes i dunno much or LUA programming.. But it works in game till i save game and try to continue saved game all resets to zero... so i guess i need try fix this ( there is a variable "lastMovedDistance" which is nowhere calculated, but required for the saved xml "Odometer.total") for it to correct save game stats

Bilbo Beutlin (BBeutlin) 17.06.2017 19:49
I assume, "lastMovedDistance" is/was used and set by Courseplay.
Also "getIsCourseplayDriving" links obviously to it.

So either you must re-write the code independant from Courseplay - or you need the Courseplay mod.
Certainly you can't use the FS15 version.
The FS17 vs. is afaik still in beta and it's not sure, it uses same/similiar variables and functions.

Timmy Billings (Unknown) 18.06.2017 11:05
I got it to work thanks for all the help... I Just took notes on a threshing counter for 2017 and got this to work.

Bilbo Beutlin (BBeutlin) 18.06.2017 16:46
aahh - well done :)
You've learned your lesson well: always take the documented LUAs as template and adapt the code to your requirements.
However: possibly you must keep an eye on changes after next patch.


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