Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


Averaging values in a lua table

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting
View previous topic :: View next topic  
Author Message
MrGentle
How do I cheat?
Reputation: 0

Joined: 29 Sep 2015
Posts: 7

PostPosted: Tue Aug 23, 2016 6:12 pm    Post subject: Averaging values in a lua table Reply with quote

So what i'm trying to do is:
Whenever the player starts a stage, the value of the character object speed is added to a table each tick until the player reaches the end of the stage.
At the end of the stage i want to stop adding stuff to the table, and instead average all the values. I want to output this value to a d3d text container in-game.

The code snippet:

Code:
if asMeter~=nil then asMeter.destroy(); asMeter = nil; end
asMeter=d3dhook_createTextContainer(fontmap,-1,640,"Average speed last run: N/A")
averageSpeed = {}
asTableLoop = 1;
calculateAverage = false

--calculate average speed
--This part of the code runs inside a timer
         if (gameMode == 1) then
            if(calculateAverage == false) then
                calculateAverage = true
            end
            averageSpeed[asTableLoop] = playerSpeed
            asTableLoop = asTableLoop+1
         elseif (gameMode == 2) then
             if (calculateAverage == true) then
                local sum = 0
                local ave = 0
                local elements = #averageSpeed

                for i = 1, elements do
                    sum = sum + averageSpeed[i]
                end

                ave = sum / elements
                d3dhook_textcontainer_setText(asMeter, [[Average speed last run: ]] .. ave)
                asTableLoop = 1;
                averageSpeed = {}
                calculateAverage = false
             end
         end


The current effect:
Regardless of gameState the asMeter text container always displays: "Average speed last run: N/A"

So what am i doing wrong? Would really appreciate it if anyone could point me in the right direction!
Back to top
View user's profile Send private message
DaSpamer
Grandmaster Cheater Supreme
Reputation: 52

Joined: 13 Sep 2011
Posts: 1578

PostPosted: Wed Aug 24, 2016 6:01 am    Post subject: Reply with quote

Check PlayerSpeed, or verify that each input inside your table is actually is a number, I believe your table contains bad inputs or somehow the calculation corrupts the desired output.
Try to print all table information (for key,value in pairs..), post it here, and also how do you set playerSpeed?

_________________
HEY Hitler
Do you get lazy when making trainers?
Well no more!
My CETrainer will generate it for you in seconds, so you won't get lazy! Very Happy

http://forum.cheatengine.org/viewtopic.php?t=564919
Back to top
View user's profile Send private message
MrGentle
How do I cheat?
Reputation: 0

Joined: 29 Sep 2015
Posts: 7

PostPosted: Wed Aug 24, 2016 8:15 am    Post subject: Reply with quote

Thanks DaSpamer, but i figured it out! gameMode was stored as a string for some reason, so i had to do a tonumber operation on it.

i was already using playerSpeed for several other functions, so that couldn't have been the problem. I should've been more specific in my explanation.

You can close this thread if you want to!
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites