Those Pesky Acloves

Ask for help about creating mods and scripts for Grimrock 2 or share your tips, scripts, tools and assets with other modders here. Warning: forum contains spoilers!
Post Reply
Azel
Posts: 808
Joined: Thu Nov 06, 2014 10:40 pm

Those Pesky Acloves

Post by Azel »

Okay so I have an Aclove, and I want to make sure that the Player has placed the right "number" of Rocks inside of it. Lets say, 3 Rocks.

First off, if the 3 Rocks are a "pile" - a single Rock Item with a Count of 3 - I had no idea how to use Lua Script to get the count. However, I was able to acquire the Weight of the Rock pile. In LoG 2, a Rock weighs 0.7 kg. Thus, I was able to say, "if RockWeight == 0.7 * 11 then DoSomething();"

This seemed to work just fine for smaller numbers, and with any combination of Rock piles. For example, if I need to check for 3 Rocks (0.7 * 3) then the Player could successfully do the following:

1) 3 different Rock piles each with a count of 1, for 3 Rocks total
2) A single Rock pile with a count of 3, for 3 Rocks total
3) 2 different Rock piles, one with a count of 2 and another with a count of 1, for 3 Rocks total.

Each time, the LUA Script properly evaluates that the accumulative Weight of the rocks within the Aclove is in fact, 0.7 * 3.

My problem? This completely fails when the Rock count is 7. If I do the following this theory falls apart:

1) 3 different Rock piles, one with a count of 5, one with a count of 1, another with a count of 1, for 7 Rocks total.

In this case, the LUA script does in fact calculate the weight as 4.9 (0.7 * 7), but if I evaluate it with an IF/Then statement, the condition fails:

if RockWeight == (0.7 * 7) then DoSomething(); end

It makes no sense. If I use a Print command, I can clearly see the RockWeight is "4.9" and the result of "0.7 * 7" is also 4.9; which means the If/Then statement in LUA is failing when it says "does 4.9 == 4.9"

If, however, the Player simply puts in a single Rock pile with a count of 7, the very same If/Then statement evaluates successfully.

Anyone else deal with something like this?

Code: Select all

	for v,item in tree_aclove_1.surface:contents() do		
      if item.go.name == "rock" then
		 rockWeight = item:getTotalWeight();

		 if rockWeight ~= nil then
		 	totalWeight = totalWeight + rockWeight;
		 end
      end
    end
minmay
Posts: 2790
Joined: Mon Sep 23, 2013 2:24 am

Re: Those Pesky Acloves

Post by minmay »

Just use ItemComponent:getStackSize().
Grimrock 1 dungeon
Grimrock 2 resources
I no longer answer scripting questions in private messages. Please ask in a forum topic or this Discord server.
Azel
Posts: 808
Joined: Thu Nov 06, 2014 10:40 pm

Re: Those Pesky Acloves

Post by Azel »

Worked like a charm. I replaced "getTotalWeight()" with "getStackSize()"

The only other part I changed is the IF/Then statement. So it went from "does 4.9 == 4.9" to "does 7 == 7" ... and suddenly everything works perfectly. Very strange. LUA no likey decimals? Anywho, thanks a ton for the help, minmay.
User avatar
JohnWordsworth
Posts: 1397
Joined: Fri Sep 14, 2012 4:19 pm
Location: Devon, United Kingdom
Contact:

Re: Those Pesky Acloves

Post by JohnWordsworth »

Due to the way computers represent numbers, it is likely that the computed value which looks like 4.9 is actually stored as 4.8999999999 or 4.9000000001 under the hood, but when you print it Lua rounds it off to a nice presentable number. See this video for more information... http://www.youtube.com/watch?v=PZRI1IfStY0

If you really wanted to use weights, you could compare them to 2 or 3 significant figures and it would work...

Code: Select all

if math.floor(totalWeight * 1000 + 0.5) == math.floor(expectedWeight * 1000 + 0.5) then
  ...
end
By multiplying by 1000 you turn 4.9kg into 4900g (say). math.floor rounds the number down, so to round to the nearest whole number we add 0.5 before calling floor. This means, any errant digits at the end get rounded away. 4.8999999 becomes 4899.9999 add the 0.5 and you get 4900.49999 and floor it and it's just 4900 (example shows why you need to add 0.5 before flooring).

Alternatively, you can measure the difference and just ensure its small enough...

Code: Select all

if math.abs(totalWeight - expectedWeight) < 0.001 then ... end
Edit; But yes, if you just want to count rocks, you are doing it the better way :).
My Grimrock Projects Page with links to the Grimrock Model Toolkit, GrimFBX, Atlas Toolkit, QuickBar, NoteBook and the Oriental Weapons Pack.
Azel
Posts: 808
Joined: Thu Nov 06, 2014 10:40 pm

Re: Those Pesky Acloves

Post by Azel »

aah that makes perfect sense, JW. Would likely explain while the weight of 7 Count resolves to 4.9 yet the weight of 4 Count + 2 Count + 1 Count resolves to something along the lines of 4.8999. Will definitely keep this in mind for future Mod'ing. For now, minmay's suggestion is definitely the smarter way to go. woohoo!
Post Reply