Page 1 of 1

Spring.GetGameSeconds question

Posted: 23 Jan 2010, 22:21
by Voidranaut
I am currently working on the following script

Code: Select all

-- $Id: snd_noises.lua 3171 2008-11-06 09:06:29Z det $ 
 -------------------------------------------------------------------------------- 
 -------------------------------------------------------------------------------- 
 -- 
 --  file:    tests I need to run
 --  brief:   will be my holly grail of lua scripting 
 -- 
 --  Copyright (C) 2007. 
 --  Licensed under the terms of the GNU GPL, v2 or later. 
 -- 
 -------------------------------------------------------------------------------- 
 -------------------------------------------------------------------------------- 
  
 function widget:GetInfo() 
   return { 
     name      = "tests", 
     desc      = "me messing around", 
     author    = "voidranaut", 
     date      = "blaaa", 
     license   = "GNU GPL, v2 or later", 
     layer     = -10, 
     enabled   = true  --  loaded by default? 
   } 
 end 
  
 local PlaySoundFile    = Spring.PlaySoundFile
 local GSU = Spring.GetSelectedUnits 
 local echo = Spring.Echo
 local lastbuilt = {}
 local cost = 100
 local sound_dir = 'Sounds/'
 --local path = sound_dir..filename..".WAV"   
 local timer = Spring.GetGameSeconds
 local time = timer()
 local lastPlayed = 0
 
function widget:CommandNotify(cmdID) 

	
	local unitID = Spring.GetSelectedUnits()[1]

	if(not unitID) then
		echo( "oops" )
		return
	end

	local defID = Spring.GetUnitDefID(unitID)
	local Nature = UnitDefs[ defID ]["customParams"]["merciless"]

	if( "1" == Nature ) then
		local rand = math.random(1,1)
		if( rand == 1) then
			echo( time )
			echo( lastplayed )
			if ( (time - lastPlayed) >= 10 ) then 

     				Spring.PlaySoundFile("sounds/Regis_ultamate_fight_full.wav")
     				lastPlayed = time 
   			end 

		end
		
		

		
	end
	

end 
   	 
what I was hopeing to do was use Spring.GetGameSeconds to get the current game time and compare it to lastPlayed and only play another sound if 10 full seconds have passed

Probleam: the echo command echo( time ) gives back 0 every time and echo( lastplayed ) gives back nil...now this segjests that My primary probleam is I missinterprited what exactly Spring.GetGameSeconds does

Question: is there a way I can get the current time in seconds that have passed since the game began? and if I did get that value would I be able to use it as I intended Spring.GetGameSeconds to be used in the script above? or am I barking up the wrong tree?

oh one more thing.. I have been making alot of threads over the last few days and I dont want to clutter the forum ... is it ok that I have been making so many? or is there a better way for me to post that would be less annoying then seeing threads started by voidranaut everywere? any feed back is welcome

thank you for your time

Re: Spring.GetGameSeconds question

Posted: 23 Jan 2010, 22:54
by jK
lastplayed

versus

lastPlayed

Re: Spring.GetGameSeconds question

Posted: 23 Jan 2010, 23:06
by Voidranaut
jK wrote:lastplayed

versus

lastPlayed

sorry for my ignorence but I dont fallow

are you saying I should change the if() stamtent to include something more to do with lastplayed?

sorry but I really dont fallow your remark

EDIT: also as I have played with it I found that

Code: Select all

if ( ( Spring.GetGameSeconds() - lastPlayed) >= 10 ) then

works as an if stament... but if I do something like

Code: Select all

local time = Spring.GetGameSeconds() 
if ( ( time - lastPlayed) >= 10 ) then
it does NOT work ... I guess its how I satted it that is wrong but I am wondering why that is?

Re: Spring.GetGameSeconds question

Posted: 23 Jan 2010, 23:34
by lurker
Slow. Down.



And read your code again. You're not echoing lastPlayed, you're echoing lastplayed, which is empty.



Both of those ifs look fine, are you sure you didn't make a typo?

Re: Spring.GetGameSeconds question

Posted: 23 Jan 2010, 23:37
by zwzsg
Voidranaut wrote:
jK wrote:lastplayed

versus

lastPlayed
sorry for my ignorance but I dont follow
Capitalisation matters.

Re: Spring.GetGameSeconds question

Posted: 24 Jan 2010, 00:09
by Voidranaut
aaa I see

wow I must be excited or I would have cought that

will try to slow down

thanks for the help