Jump to content

problem with creating a lua timer and with playing a sound


hummie

Recommended Posts

I have 2 problem as the topic title suggests.

One of them being the problem that I dont know how to create a timer in lua.

On a other project I was used to a sleep(float) function.

The other thing that I tried was with os.time but that didn't work out as well.(crashed upon using it)

 

The second problem is that I can't find a method to let me play a sound and set the radius for it.(like a weapon that was fired)

I have found the playsound() method but I dont think it will be the same as a used gun, but more as a sound effect.

 

local function soundtest()local player = getSpecificPlayer(0)local weapon = player:getPrimaryHandItem():getName()print(weapon)if weapon == "Axe" thenprint(" :D") -- testingposworld = player:getCurrentSquare()print(posworld) --again testing--timer--end--play a soundendendEvents.OnWeaponSwing.Add(soundtest)

 

sorry for the code in the spoilers it doesnt seem to like me  :shock:.

Link to comment
Share on other sites

So I am back with the results from testing:

Got the sound working, the only thing that I need to do is tweak some values.

The bad news is, is that I dindn't got my timer/delay to work.

I tried to used getMinutes() to make a delay but got a error that reads out:"Object tried to call nil in soundtest"(on line 29)

 

Here is the code on pastebin.(since it looks a little bit cleaner there)

http://pastebin.com/gEXZHst5

Link to comment
Share on other sites

Object tried to call nil is referring to the method getMinutes() which is doesn't exist in your code. The way you are calling it, you would need your own lua function called getMinutes(), wheras what I think you want to do is call:

 

GameTime:getMinutes()

 

or

 

GameTime.getMinutes()

 

I'm not sure which off the top of my head. Essentially you need to specify that you want to call the getMinutes() function on the GameTime object.

 

In short replace line 29 with 

 

mins = GameTime:getMinutes()

 

You may need to change the colon to a period... I've not used lua in ages.

Link to comment
Share on other sites

I tried wat you suggested but the error doesnt change at all.

The line is still line 29 with the same error.

 

Edit:

After giving the error some more thought and what you said.

Is it a possibillity that I need to do something with require'something'.

I looked at a example in this tutorial: http://theindiestone.com/forums/index.php/topic/61-robomats-modding-tutorials-updated-12112013/

But when searched for such a name in the javadocs I couldn't find any classes that matched.

Link to comment
Share on other sites

ok thanks for the getInstance() part that made it work.

The only problem is that it crashes ones it excutes the script.

I think it spams to much since it crashes at the "print("not equal")" part.

When I remove that it still doesnt work.

 

Edit:
At this point I think it is more of a problem that I have not a work-around for wait/delay function.

On a other project I would use sleep() function inside a for or while loop, to prevent spamming the client/server.

Or to prevent some tasks from being executed to quikly in a row.

Link to comment
Share on other sites

Sorry. I guess I should have read the problem properly....

 

When writing lua scripts loops can not be used to wait for something to happen. Nor can you tell the thread to sleep for a little bit. Essentially doing so will cause the entire game to wait for the loop to finish and since the game never changes state, the game time is never increased and your while loop will always return false, waiting for eternity.

 

What I think people tend to do is add functions into the onTick event of the game. Something like, 

local startTime = GameTime:getInstance():GetMinutes()local myFunction = function()   local currentTime = GameTime:getInstance():GetMinutes()     if(currentTime == startTime+5)     print('5 minutes has passed')   endendEvents.OnTick.Add(myFunction)

Essentially you should think of tha game as being one massive loop. Everything in the Events.OnTick collection is executed once every cycle of that loop.

 

There is a more complex solution in this thread:

 

http://theindiestone.com/forums/index.php/topic/1931-how-to-delay-processing-of-a-function/

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...