Independant function calls?
#2
The entire server is event-driven and the plugins are synchronous - while plugin code is executing, most of the rest of the server is waiting for it to finish. This means that you cannot use any kind of a sleep function, you need to make use of the scheduler instead. There's a cWorld:ScheduleTask() function that basically says "call this function after this many ticks". Additionally, you need to take care of the situation that your plugin is using a note entity and a player destroys the block during that time - the note entity object becomes invalid. Therefore, the code should look something like this:
--- Schedules the specified note block to play the specified melody
-- a_Melody is an array-table of notes, each note being a table with two members, delay (in ticks) and pitch
function StartPlaying(a_World, a_NoteBlockX, a_NoteBlockY, a_NoteBlockZ, a_Melody)
  for _, note in ipairs(a_Melody) do
    a_World:ScheduleTask(note.delay,
      function (a_CBWorld)
        a_CBWorld:DoWithNoteAt(a_NoteBlockX, a_NoteBlockY, a_NoteBlokZ,
          function (a_CBNoteBlock)
            a_CBNoteBlock:SetPitch(note.pitch)
            a_CBNoteBlock:MakeSound()
          end
        )
      end
    )
  end
end

-- Example usage:
local Melody =
{
  {0, F5},
  {4, F5},
  {8, F5},
  {12, D5},
  {14, F5},
}
StartPlaying(World, 100, 100, 100, Melody)
Reply
Thanks given by: Seadragon91 , DiamondToaster


Messages In This Thread
Independant function calls? - by DiamondToaster - 01-17-2015, 12:59 PM
RE: Independant function calls? - by xoft - 01-17-2015, 05:23 PM
RE: Independant function calls? - by xoft - 01-17-2015, 05:27 PM
RE: Independant function calls? - by sphinxc0re - 01-17-2015, 10:07 PM
RE: Independant function calls? - by xoft - 01-17-2015, 11:08 PM
RE: Independant function calls? - by Seadragon91 - 01-17-2015, 11:57 PM
RE: Independant function calls? - by xoft - 01-18-2015, 03:51 AM
RE: Independant function calls? - by tonibm19 - 01-18-2015, 04:27 AM
RE: Independant function calls? - by tigerw - 01-18-2015, 09:09 AM



Users browsing this thread: 1 Guest(s)