Jump to content

setTimer and class method


Bhelic

Recommended Posts

I was making a new script with classes for mta, and i need to set a timer inside a method like this.

function WorldGrid:updatePlayerZone(player)
-- DOING NON RELEVANT SHI T HERE
      outputChatBox("worldGrid: " .. tostring(worldGrid) .. " player: " .. tostring(player))
      setTimer(self:updatePlayerZone, 4000, 1, player)
   end
end

 

This couldnt even compile, because self:updatePlayerZone is not recognized as a function.

So.. i checked setTimer mta wiki page.

it says: 

  • theFunction: The function you wish the timer to call. (Notice: Do not use a 'local' function, it must be global!)

So, i need to call a global function on setTimer.

I tryed out this:

function updatePlayerZoneWrapper(worldGrid, player)
   outputChatBox("worldGrid " .. tostring(worldGrid) .. "  player: " .. tostring(player))
   worldGrid:updatePlayerZone(player)
end
function WorldGrid:updatePlayerZone(player)
      outputChatBox("worldGrid before: " .. tostring(self) .. " player before: " .. tostring(player)
      setTimer(updatePlayerZoneWrapper, 4000, 1, self, player)
   end
end

But it didnt work neither..

I get: 

Attempt to call method 'updatePlayerZone' (a nil value)

I continued reading setTimer wiki page, and i found this:

  • arguments: Any arguments you wish to pass to the function can be listed after the timesToExecute argument. Note that any tables you want to pass will get cloned, whereas metatables and functions/function references in that passed table will get lost. Also changes you make in the original table before the function gets called won't get transferred.

So, im coming to you guys, hoping you have a possible solution for this.

Thanks for your time

Edited by Bhelic
Link to comment

You can use anynomous functions inside the setTimer like:

setTimer(function()
   updatePlayerZoneWrapper(self, player) 
end, 4000, 1) 

But the way you tried is also a working method, so the problem is probaly the variable "player" is not accessible by the setTimer. Try to reference it before setting the setTimer like: 

local player = player

setTimer( ... )

Link to comment

I dont think its the variable player

On the outputChatBox i placed, i get:

worldGrid before: table: 06294738 player: userdata: 00000A93

worldGrid after: table: 024BA560 player: userdata: 00000A93

 

But with your idea i used:

setTimer(function() self:updatePlayerZone(player) end, 4000, 1)

and it worked.

Thank you! :)

  • Like 1
Link to comment

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...