Community Forum

player.currentTool and player.usesChainsaw

Forum Overview >> Scripting

CategoryScripting
Created05.05.2020 13:50


Bert Bkp (Bkp666) 05.05.2020 13:50
Hi, I am trying to convert a mod from FS17, I'm not very experienced when it comes to scripting but I am slowly figuring things out.
If I leave the lua as it was in FS17 I am not getting any errors in the game log but it's not working either so I added prints after every function.
For example:
if g_currentMission.player then
print("======== Mission.player ============");
end
and I would get it in the log, I did this for g_currentMission.player.currentTool aswell and I wouldn't get anything so I replaced it with g_currentMission.player.hasHandtoolEquipped (is it the right replacement though?) and I got the print in the log.

Now I'm trying to print the player.usesChainsaw but I can't seem to find the correct replacement, I tried player.isCutting, player.isUsingChainsaw, player.isUsingChainsawHorizontal but nothing seems to work. Am I doing something wrong or I didn't look deep enough?

Bilbo Beutlin (BBeutlin) 05.05.2020 16:37
The LUA code of 'player' as well as 'handtool' has changed massively vs. FS17, see Documentation/LUADOC.

Bert Bkp (Bkp666) 05.05.2020 17:20
That's where I'm looking but can't really find a replacement for "usesChainsaw"
The closest thing I found is "isCutting" that says "true if player is cutting", exactly what I need but I'm not getting a print like I'm getting for "hasHandtoolEquipped"
https://gdn.giants-software.com/documentation_scripting_fs19.php?version=script&category=102&class=10366#setCuttingAnim164220

Bilbo Beutlin (BBeutlin) 05.05.2020 21:06
You should improve your reseach skills, so not other people must research for you. *g*
Search in Player LUA for "handtool". You'll get a bunch of valuable infos.

To test for handtool use "player:hasHandtoolEquipped()" or directly "player.baseInformation.currentHandtool".
For further infos you must probably examine the used/active handtool.

In certain cases you must also check whether the code runs as client or as server.

Bert Bkp (Bkp666) 05.05.2020 22:25
Believe me, I read pretty much everything in the LUADOC, but it is of no use if I don't understand what goes where because what seems very obvious it's apparently not so

As I said in my first post, I am already using "player.hasHandtoolEquipped" as a replacement for "player.currentTool" I don't know if it is the right replacement but for now it is doing what I need.(checking if player has a handtool equipped, no?)

What I can't find is a replacement for "player.usesChainsaw",(as in player is cutting with the chainsaw, not just having it equipped) reading trough the LUADOC I found different options but none seem to work for me.(maybe because they don't go together with the "g_currentMission.player"?)

For example I found "function Chainsaw:isBeingUsed() return self.isCutting" Someone like me would think this is what they need(and logically it should be that way, but it's not) so I did:
if g_currentMission.player.isCutting then
print("======== player.isCutting ============");
end
It's probably wrong because I didn't get anything, no print no error
I also tried:
if g_currentMission.player.currentHandtool.isCutting then
print("========player.currentHandtool.isCutting ============");
end
And I am getting "Error: Running LUA method 'update'. attempt to index field 'currentHandtool' (a nil value)"

And what is the difference between "player:hasHandtoolEquipped()" and "player.baseInformation.currentHandtool" ?
And how do I know if the code runs as client or as server? I doesn't have the "isServer" or "isClient" and it doesn't uses readStream and writeStream, or those are not for that

Bilbo Beutlin (BBeutlin) 05.05.2020 23:43
*sigh* Honestly: before you begin fiddling around with LUA, you must at least have basic knowledges about LUA.

"player.hasHandtoolEquipped" is NOT a variable, but a function.
So you must use "isEquipped = player:hasHandtoolEquipped()" what gives true|false.

The difference between "player:hasHandtoolEquipped()" and "player.baseInformation.currentHandtool":
1st gives true|false
2nd gives the handtool table (or nil case nothing equipped).

Each handtool has the function "HandTool:isBeingUsed()".
For chainsaw you must at first test whether the handtool is of type chainsaw.

Example:
local hTool = g_currentMission.player.baseInformation.currentHandtool
if hTool ~= nil then
local htInUse = hTool:isBeingUsed()
local htType = tostring(hTool)
...

Anyway, better put the project on hold for now and learn LUA at first.

Bert Bkp (Bkp666) 07.05.2020 21:13
I was hoping I don't have to learn a whole programming language to just convert a mod(not create a new one) from FS17 to FS19

Bilbo Beutlin (BBeutlin) 08.05.2020 01:06
Converting a script mod is even more difficult than creating a mod from scratch.
You need to understand the code thoroughly before you change anything.

Bert Bkp (Bkp666) 12.05.2020 14:56
I think I did it,

local hTool = g_currentMission.player.baseInformation.currentHandtool
if hTool ~= nil then
if hTool.isCutting then
print("hTool.isCutting");
end
end

and I changed a few other things and now the mod is working

Thanks for pointing me in the general area :P


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