Spring.GetGameSeconds question

Spring.GetGameSeconds question

Discuss Lua based Spring scripts (LuaUI widgets, mission scripts, gaia scripts, mod-rules scripts, scripted keybindings, etc...)

Moderator: Moderators

Post Reply
Voidranaut
Posts: 47
Joined: 20 Dec 2009, 21:16

Spring.GetGameSeconds question

Post 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
User avatar
jK
Spring Developer
Posts: 2299
Joined: 28 Jun 2007, 07:30

Re: Spring.GetGameSeconds question

Post by jK »

lastplayed

versus

lastPlayed
Voidranaut
Posts: 47
Joined: 20 Dec 2009, 21:16

Re: Spring.GetGameSeconds question

Post 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?
User avatar
lurker
Posts: 3842
Joined: 08 Jan 2007, 06:13

Re: Spring.GetGameSeconds question

Post 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?
User avatar
zwzsg
Kernel Panic Co-Developer
Posts: 7052
Joined: 16 Nov 2004, 13:08

Re: Spring.GetGameSeconds question

Post by zwzsg »

Voidranaut wrote:
jK wrote:lastplayed

versus

lastPlayed
sorry for my ignorance but I dont follow
Capitalisation matters.
Voidranaut
Posts: 47
Joined: 20 Dec 2009, 21:16

Re: Spring.GetGameSeconds question

Post 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
Post Reply

Return to “Lua Scripts”