Решено Значение переменной почему-то становится нулевым внутри функции

fabio_longo

Продвинутый
13 Июн 2021
152
18
steamcommunity.com
Проект
Chen's Training, Creep Stacking Training
Lua:
require ('notifications')
require ('timers')
local score = 0
local score1
function tp_ursa1 ( event )
   local wws1 = "ursa_trigger"
   local wwss1 = "ursa"
   local ent11 = Entities:FindByName(nil, wws1)
   local ent111 = Entities:FindByName(nil, wwss1)
   local posx1 = math.random ( -1088, 1088 )
   local posy1 = math.random ( -832, 832 )
   local point11 = ent11:GetAbsOrigin()
   local npc11 = "ursa_npc"
   local urs1 = Entities:FindByName(nil,npc11)
   local point1 = urs1:GetAbsOrigin()
    if math.abs(point1.x - point11.x) <= 128 and math.abs(point1.y - point11.y) <= 128 then -- Триггер, реагирующий на приближение юнита
     ent11:SetAbsOrigin(Vector(posx1,posy1,128))
     ent111:SetAbsOrigin(Vector(posx1,posy1,128))
    score = score + 1
               Notifications:ClearTopFromAll()
        Notifications:Top( nPlayerNumber, {text="Score:", class="NotificationMessage", style={color="red", ["font-size"]="45px"}, duration=5})
     Notifications:Top( nPlayerNumber, {text=tostring(score), class="NotificationMessage", style={color="red", ["font-size"]="45px"}, duration=5, continue=true})
    end
end
function Timer (event1)
   local wws = "ursa_trigger"
   local wwss = "ursa"
   local ent = Entities:FindByName(nil, wws)
   local ent1 = Entities:FindByName(nil, wwss)
   local hero = event1.activator
   local nPlayerNumber = hero:GetPlayerOwnerID()
   local fCountdown = 10
   Timers:CreateTimer(0, function()
    if fCountdown > 0 then
        Notifications:ClearBottomFromAll()
        Notifications:Bottom( nPlayerNumber, {text=tostring(fCountdown), class="NotificationMessage", style={color="red", ["font-size"]="45px"}, duration=1})
        fCountdown = fCountdown - 1
        return 1
    else
      score1=score
      local start = "Start_training"
      local start1 = Entities:FindByName(nil, start)
      start1:Enable()
            Notifications:Bottom( nPlayerNumber, {text="Nice Job! Go to the fire to start over.", class="NotificationMessage", style={color="red", ["font-size"]="45px"}, duration=5})
            Notifications:Bottom( nPlayerNumber, {text=tostring(score1), class="NotificationMessage", style={color="red", ["font-size"]="45px"}, duration=5})
      ent:SetAbsOrigin(Vector(posx,posy,-500))
      ent1:SetAbsOrigin(Vector(posx,posy,-500))
       return nil
    end
end)
end
Почему-то, внутри функции "Timer" значение score становится nil, хотя вне функции "Timer" score всегда равен тому числу, сколько исполнялась функция "tp_ursa1". В чём может быть проблема?
 
Последнее редактирование:

fabio_longo

Продвинутый
13 Июн 2021
152
18
steamcommunity.com
Проект
Chen's Training, Creep Stacking Training
Оказывается, проблема была в том, что триггер, вызывающий "Timer", находит путь к скрипту в одном entity, а Триггер, вызывающий tp_ursa, находит путь к скрипту в другом entity. Получается, score в одном entity и score в другом - две разные переменные.
 

AceFridge

Активный
8 Мар 2021
85
15
объявляй глобальную переменную без local где нибудь вне функции, например, в addon_game_mode.lua, удобнее даже будет все глобалки и константы выделить в отдельный файл, допустим, variables.lua и прописать require("variables") в начале addon_game_mode.lua
 
  • Нравится
Реакции: fabio_longo

fabio_longo

Продвинутый
13 Июн 2021
152
18
steamcommunity.com
Проект
Chen's Training, Creep Stacking Training
объявляй глобальную переменную без local где нибудь вне функции, например, в addon_game_mode.lua, удобнее даже будет все глобалки и константы выделить в отдельный файл, допустим, variables.lua и прописать require("variables") в начале addon_game_mode.lua
Спасибо, возьму на заметку! Просто я уже закостылил всё своим способом)))
 
Реклама: