multitheftauto / mtasa-blue

Multi Theft Auto is a game engine that incorporates an extendable network play element into a proprietary commercial single-player game.
https://multitheftauto.com
GNU General Public License v3.0
1.42k stars 437 forks source link

Can't get timer details from other resource #827

Open MrDadosz opened 5 years ago

MrDadosz commented 5 years ago

Describe the bug You can't check timer details from timer, that was created in other resource. isTimer function is working, but getTimerDetails isn't.

To reproduce

  1. create resource "test" with serverside file
  2. create resource "test2" with serverside file
  3. paste this code inside resource "test" and run it:
    function outputSomething()
    outputChatBox("I'm working, you can get my details!")
    end
    local timer = setTimer(outputSomething, 1000, 0)
    setElementData(root, "myTimer", timer)
  4. paste this code inside resource "test2" and run it:
    local timerIsWorking = isTimer(getElementData(root, "myTimer"))
    local timeLeft, amout, executions = getTimerDetails(getElementData(root, "myTimer"))
    outputChatBox(tostring(timerIsWorking)..", "..tostring(timeLeft)..", "..tostring(amount)..", "..tostring(executions))
  5. resource "test2" will show info on chat: true, false, nil, nil

Expected behaviour You could check timer details even if they're created in other resource.

Version Server: Ubuntu 14, MTA:SA Server v1.5.6-release-14688 Client: Windows 10, Multi Theft Auto v1.5.6-release-16068.8.014

Lpsd commented 5 years ago

I notice in your "test2" resource, it's returning true for isTimer. With the following code I'm returning false for both isTimer and getTimerDetails

resource 1:

addEvent("onTimerFire", true)

timer = false

function init()
    timer = setTimer(timerFunc, 10000, 0)
end
addEventHandler("onResourceStart", resourceRoot, init)

function timerFunc()
  triggerEvent("onTimerFire", root, timer)
end

resource 2

function checkTimerDetails(timer)
    print(isTimer(timer), toJSON(getTimerDetails(timer)))
end
addEventHandler("onTimerFire", root, checkTimerDetails)

outputs: false [ false ]

patrikjuvonen commented 5 years ago

Is this an old or recent bug?

MrDadosz commented 5 years ago

Sorry, I spent a lot of time figuring out why my script isn't working and I'm wrong about isTimer. Script returns: false, false, nil, nil You can't use getTimerDetails and isTimer but you can check type of the variable and it returns wrong data. In the resource where you created timer it returns lua-timer02000288. In other resource when you're using getElementType on the timer it returns userdata02000288. In my opinion you should be able to check if variable is a timer and get details from it etc. or update info about it on wiki:

https://wiki.multitheftauto.com/wiki/Timer "Also timers are not under the resource hierarchy, because they are not elements, for instance, if you create a timer, it will not be destroyed when the resource in which it was created is stopped, so in this case you should kill the timer manually." - stopping the resource kills the timer (or you can't access it from other resources).

resources.zip Same resource: true, -5, nil, 1000, lua-timer02000288 - everything is okay Other resource: false, false, nil, nil, userdata02000288 - shows timer as userdata, can't check if it's a timer and can't get details of it.

moon91210 commented 5 years ago

I can confirm isTimer and isElement both return false on a timer from another resource. The timer is passed through an exported function. You can't destroy it or interact with it at all. It's a real bummer cause I have this system in my gamemode where it destroys elements from certain resources.