By Reworld write a timer in the time stamp, so that more accurate timing

Operating Environment

Win7,Win8,Win10 win64

Reworld version trial version

 

For zero-based supplementary readers

Download and install the corresponding version Reworld

Reworld official website link: http://www.reworlder.com/

After you install after downloading registered account open air map

Ideas analysis

    Timestamp realization of ideas divided into three parts: First, to get the current time and the time until the timing To, and then converted into a time stamp; secondly, to do with the arrival time difference at the current time, the time stamp obtained CD time difference; and finally , the time difference obtained is converted to the date and time format, calculating time assignment text characters in the Update function. There are two difficulties: First, the conversion date and time stamp, the time stamp is converted to a particular day, a time of conversion. Second, the difference between date formats into timestamps should understand os.time () and os.date () usage.

Functional effects show

 

Introduced by Example

    I believe that through instructional videos and some items are already inside to find out about Lua coroutines (coroutine), simply waiting for a trigger is wait () to control the period of time before continuing execution. Here is an example of a coroutine:

function TestFun()

    print ( "I'm a little heavy Kai")

end

 

Players.PlayerAdded:Connect(function(Uid)

    coroutine.start(function()

        print ( "Role generated")

    wait(3)

    TestFun()

    end)

end)

 

   The above Players There is a PlayerAdded event is generated when the role calls execute, overall logic output is generated in the role "character generation," and after 3 seconds, perform TestFun method, output: I am a little heavy start.

But when multiple Ctrip occurs in a program time, stop coroutine may cause a program error. Therefore, the above applies to timer relatively short time , short cycle countdown. Then I was introduced timestamp countdown .

 

First, the time stamp Introduction

What is it time stamp? Before writing the production steps, first a brief introduction about the timestamp: Baidu to is defined as: "a copy of the data can be represented in a particular time before that already exists, complete, verifiable data, typically a sequence of characters, uniquely identify a certain moment of time. "

Then the timestamp is a sequence of characters, let's explore together timestamp! First, I look at print time stamp:

Players.PlayerAdded:Connect(function(Uid)

    player = Players:GetPlayerByUserId(Uid)

    print (os.time ()) - the local system time stamp

end)

Here are the results of running the editor:

No contact with the timestamp of people have questions about what (1560501534). This string is? Represents what time? In fact, we can Baidu search for " time stamps online conversion tool to convert" (open Conversion Tool -> Input timestamp -> get specific date and time).

The above process is converted to a specific timestamp time. 1560501534 = 2019-06-14 16:38:54

In this way, we all get the point! But every time to get through a browser, that's too much trouble! I tell you get the date time stamp inside through the code:

Players.PlayerAdded:Connect(function(Uid)

    player = Players:GetPlayerByUserId(Uid)

    print("这是年: " .. os.date("%y",os.time()) .. 

    "This is the month:" .. os.date ( "% m", os.time ()) .. 

    "This is the day:" .. os.date ( "% d", os.time ()))

    print ( "That is:" .. os.date ( "% H", os.time ()) .. 

    "这是分: " .. os.date("%M",os.time()).. 

    "这是秒: " .. os.date("%S",os.time())) 

end)

The above code, os.date () function is a timestamp to date method, the following are the results:

The exact date of printing down, we know how to get the current timestamp timestamp it tomorrow? Surely we already know, it is the date by +1, get tomorrow's stamp. The following test:

Players.PlayerAdded:Connect(function(Uid)

    player = Players:GetPlayerByUserId(Uid)

    print(os.date("%d",os.time()))

    local CurrentTimer = os.time({day=os.date("%d",os.time())+1,         --日期+1操作

    month=os.date("%m",os.time()), year=os.date("%Y",os.time()), 

    hour=os.date("%H",os.time()), min=os.date("%M",os.time()), 

    sec=os.date("%S",os.time())});

    print(os.date("%d",CurrentTimer))

end)

Here are the results

可以看到,两个日期相差一天。所以终于给倒计时提供了一个思路:用时间戳的差值得到相差的时间戳,再转换为具体时间。

好了时间戳介绍完了,下面是时间戳倒计时的具体步骤:

二、创建服务器脚本并定义变量

首先在服务器逻辑里面创建服务器脚本:

然后打开脚本,创建倒计时所需要的变量:

local workMin = 0            --分钟

local workSec = 0            --秒钟

local CurrentTimer = 0            --未来时间的倒计时

local TotalTime = 5            --CD总时间

local StartTimeCount = 0        --开始计时的时间戳

local Hero                    --角色

PlayerInfo = {}

PlayerInfo.PlayerID = 0            --角色Id

 

 

Players.PlayerAdded:Connect(function(Uid)

    PlayerInfo.PlayerID = Uid

    Hero = Players:GetPlayerByUserId(PlayerInfo.PlayerID)

 

三、编写倒计时逻辑

倒计时是需要实时去执行的,所以写在update里面,下面是代码:

GameRun.Update:Connect(function()

        --判断时间戳是否已经赋值

    if StartTimeCount ~= 0 then

            --设置将来的时间

        CurrentTimer = os.time({day=os.date("%d",StartTimeCount), month=os.date("%m",StartTimeCount), year=os.date("%Y",StartTimeCount), hour=os.date("%H",StartTimeCount), min=os.date("%M",StartTimeCount), sec=os.date("%S",StartTimeCount)+TotalTime});     

        local PeriordTimer = CurrentTimer - StartTimeCount

        workMin = os.date("%M",CurrentTimer - os.time())

        workSec = os.date("%S",CurrentTimer - os.time())

        --做差值,得到要倒计时的时间与现在时间戳的差

        local alltime = PeriordTimer - ( os.time()-StartTimeCount)

        if alltime <= 0 then

            alltime = 0

            a= a+1

            if a == 1 then

                PlayerInfo.MatchState = false

                PlayerInfo.HideState = true

            end

            if a == 2 then

                PlayerInfo.HideState = false

                PlayerInfo.StartGame = true

            end

            if a == 3 then

                PlayerInfo.StartGame = false

            end

            if a == 4 then

                print("----end----")

            end

        end

        --将时间戳格式化为时间格式

        local second = tonumber(workSec)

        local mint = tonumber(workMin)

        local hour = math.floor(alltime / 60 /60);

        if alltime <= 0 then            

            hour = 0;

            mint = 0;

        end

        if  mint < 10 then

            mint = '0'..mint

        end

        if hour < 10 then

            hour = '0'..hour

        end

        if second < 10 then

            second = '0'..second

        end

        --得到时间格式的倒计时(我们要显示的倒计时文字)

        local temptime = tostring(hour).. ":" ..tostring(mint).. ":" ..tostring(second)

        if PlayerInfo.MatchState then

                --发送消息给客户端进行显示

            MessageEvent.FireClient(PlayerInfo.PlayerID,"Timer",temptime)

        end

    end

end)

 

--匹配设置开始时间

MessageEvent.ServerEventCallBack("StartMatch"):Connect(function()

    StartTimeCount = os.time()

    PlayerInfo.MatchState = true

end)

上面的时间戳倒计时中 temptime就是一串字符:00:00:05 ,可以直接赋值到Text文本中。当然如果想让倒计时更加的精准,不受本地时间的影响就要获取到服务器的时间戳,这样就能保证客户端与服务器的计时是一致的。

四、创建客户端脚本,给UI赋值

我用到的UI是编辑器上给定的UI,下面是我的UI层级逻辑和客户端脚本创建位置:

 

UI的“具体时间”文本框就是我们要显示的倒计时文字,刚刚在服务器脚本已经得到倒计时的文字了,只要在客户端脚本中接收然后给文本复制就可以了,下面是代码:

local MoonPic = GameUI.时间显示.时间.月亮

local SunPic = GameUI.时间显示.时间.太阳

 

 

--匹配倒计时

MessageEvent.ClientEventCallBack("Timer"):Connect(function(time)

    GameUI.时间显示.IsVisable = true

    if tonumber(os.date("%H",os.time())) >= 17 then

        MoonPic.IsVisable = true

        SunPic.IsVisable = false

    else

        MoonPic.IsVisable = false

        SunPic.IsVisable = true

    end

    GameUI.时间显示.时间.时间文字.具体时间.Text = time

    GameUI.时间显示.时间.时间文字.文本控件.Text = "匹配倒计时"

end)

所以,只要把temptime赋值给具体时间的文本控件即可。

五、设置开始倒计时的时间

我们是点击匹配按钮,开始倒计时,所以只要在点击按钮的时候,把StartTimeCount赋值为当前时间的时间戳即可,下面是匹配按钮UI层级和客户端脚本的代码:

local MatchBtn = GameUI.MatchingPanel.MatchBtn

 

 

--点击匹配

MatchBtn.OnClick:Connect(function()

        --发送给服务器消息

    MessageEvent.FireServer("StartMatch")

    MatchBtn.IsVisable = false

    GameUI.跳跃.IsVisable = false

end)

大功告成,运行后显示结果如下:

上面的月亮图片是根据时间的小时来判断的,如果当前时间的小时>17,也就是晚于晚上5点,我就判断为晚上,显示月亮反之则显示太阳。“00:00:04”就是我们要得到的倒计时,很精准。

如果想重置计时时间可以把StartTimeCount重新赋值为os.time(),这样就能重新去CD了。改动CD时间的话就改动TotalTime。

以上便是我的分享,希望时间戳倒计时可以帮到各位朋友,喜欢的童鞋记得关注哦~

六、补充说明

1.什么是客户端脚本?

只会在客户端执行的脚本,执行的逻辑和表现也只会在本地客户端展现;可在以下几个文件目录下自动执行,客户端脚本在工作区下不会自动执行,需要放在以下对象里面: 

1. 客户端最先加载 。
2. 工作区中的角色模型玩家初始化中的角色初始化脚本,在运行后会自动移动到角色模型下。 
3. 玩家列表中的玩家玩家初始化中的玩家初始化脚本,在运行后会自动移动到玩家下 
4. 玩家玩家界面界面初始化的脚本,在运行后会自动移动到玩家界面下。 
5. 玩家的背包,例如工具里面的

2.为什么要使用客户端脚本?

2D平面UI是只在客户端存在并加载的,在服务器是没有2D平面UI的实体对象的,所以只能客户端脚本来对2D平面UI的信息进行更新修改。其他这种只在客户端存在的实体对象还有摄像机、鼠标、键盘等。

3.什么是服务器逻辑?

服务对象。 

此服务下的脚本会在服务器上运行,用于放置服务器端游戏逻辑 。

不可创建。

不能用RWObject.Create()函数创建此对象。

不可删除。 

不能用Destroy()函数删除此对象。

不可复制。 

不能用Clone()函数复制此对象。

4.什么是服务器脚本?

   只会在服务器运行的Lua脚本代码,用于编写服务器逻辑。

5.为什么要使用服务器脚本?

   计时功能适用于单人和多人等不同环境,所以采用通用的服务器脚本。

  1. 服务器脚本与客户端脚本不同,客户端执行的操作只有本地客户端,也就是玩家自己有效。而服务器执行的操作不仅针对单人有效,还针对与服务器相连的所有客户端同步生效。
  2. 在多人游戏中,如果这个对象的变化是针对一个人的,必须在客户端脚本进行编写;如果这个对象的变化是针对所有人的,那就必须在服务器脚本进行编写。
  3. 对于只能在客户端脚本修改的对象,如何让服务器知晓变化结果是很重要的。这里采用传统游戏的制作流程,也就是在客户端进行修改,把修改后的结果通过与服务器通信的方式发送到服务器,再通过服务器进行逻辑运算,把执行结果再同步给所有客户端。

6.什么是文本控件?

图像控件显示非交互图像,经常用于装饰或者图标使用。

7.什么是按钮控件?

按钮控件用于响应来自用户的事件,经常用于启动或者确认某项操作使用。

发布了35 篇原创文章 · 获赞 2 · 访问量 5840

Guess you like

Origin blog.csdn.net/weixin_41987154/article/details/100143227