LUA timed text popups [Solved]

In this forum you will find and post information regarding the modding of Star Wars Battlefront 2. DO NOT POST MOD IDEAS/REQUESTS.

Moderator: Moderators

Post Reply
User avatar
Fiodis
Master of the Force
Master of the Force
Posts: 4145
Joined: Wed Nov 12, 2008 9:27 pm
Projects :: Rannoch + Tientia + Tools Programming

LUA timed text popups [Solved]

Post by Fiodis »

I'm trying to create a LUA code that will display a text countdown to a specific event. To this aim, I composed the following code:
Hidden/Spoiler:
[code]textspawn = OnObjectInit(
function(object)
if GetEntityClass(object) == FindEntityClass("rep_bldg_inf_texttest") then
ShowMessageText("level.FTG.conversations.test")
KillObject(object)
onesecondtimer = CreateTimer("onesecondtimer")
SetTimerValue(onesecondtimer, 1)
twosecondtimer = CreateTimer("twosecondtimer")
SetTimerValue(twosecondtimer, 2)
threesecondtimer = CreateTimer("threesecondtimer")
SetTimerValue(threesecondtimer, 3)
foursecondtimer = CreateTimer("foursecondtimer")
SetTimerValue(foursecondtimer, 4)
fivesecondtimer = CreateTimer("fivesecondtimer")
SetTimerValue(fivesecondtimer, 5)
StartTimer(onesecondtimer)
StartTimer(twosecondtimer)
StartTimer(threesecondtimer)
StartTimer(foursecondtimer)
StartTimer(fivesecondtimer)
combinedsequence = OnTimerElapse(
function(timer)
if GetTimerName(timer) == "onesecondtimer" then
ShowMessageText("level.FTG.conversations.one")
elseif
GetTimerName(timer) == "twosecondtimer" then
ShowMessageText("level.FTG.conversations.two")
elseif
GetTimerName(timer) == "threesecondtimer" then
ShowMessageText("level.FTG.conversations.three")
elseif
GetTimerName(timer) == "foursecondtimer" then
ShowMessageText("level.FTG.conversations.four")
elseif
GetTimerName(timer) == "fivesecondtimer" then
ShowMessageText("level.FTG.conversations.five")
end, --<== This is line 144.
"timer"
end
)
end
end
)[/code][/size]
Unfortunately, it returns the following error:
Hidden/Spoiler:
[code]C:\BF2_ModTools\data_FTG\_BUILD\Common\..\..\..\ToolsFL\Bin\luac.exe: ..\..\common\scripts\FTG\FTGc_con.lua:144: unexpected symbol near `,'
ERROR[scriptmunge scripts\FTG\FTGc_con.lua]:Could not read input file.ERROR[scriptmunge scripts\FTG\FTGc_con.lua]:Could not read input file. [continuing]
2 Errors 0 Warnings[/code]
My question is in two parts, then. What's wrong with my current code, and how could I rewrite it to make it more streamlined? The current "elseif" pattern seems somewhat inefficient.
Last edited by Fiodis on Sat Nov 27, 2010 2:47 pm, edited 1 time in total.
User avatar
Firefang
Major
Major
Posts: 518
Joined: Mon Nov 15, 2010 8:55 pm
Location: California

Re: LUA timed text popups

Post by Firefang »

Try looking at the lua from kamino campaign and see how they show the five minute timer on the screen.
User avatar
Maveritchell
Jedi Admin
Jedi Admin
Posts: 7366
Joined: Mon Aug 21, 2006 11:03 pm

Re: LUA timed text popups

Post by Maveritchell »

Drop your comma. Use a single, one-second timer that displays a different message each time it expires.
User avatar
Fiodis
Master of the Force
Master of the Force
Posts: 4145
Joined: Wed Nov 12, 2008 9:27 pm
Projects :: Rannoch + Tientia + Tools Programming

Re: LUA timed text popups

Post by Fiodis »

Alright, I revised the code to use one timer, and to reset and restart the timer each time it expires, also showing a different message each time:
Hidden/Spoiler:
[code]speakcount = 1

textspawn = OnObjectInit(
function(object)
if GetEntityClass(object) == FindEntityClass("rep_bldg_inf_texttest") then
ShowMessageText("level.FTG.conversations.test")
KillObject(object)
onesecondtimer = CreateTimer("onesecondtimer")
SetTimerValue(onesecondtimer, 1)
StartTimer(onesecondtimer)
repeatingsequence = OnTimerElapse(
function(timer)
if GetTimerName(timer) == "onesecondtimer" then
SetTimerValue(onesecondtimer, 1)
StartTimer(onesecondtimer)
if speakcount = 1 then -----------------<== This is line 121.
ShowMessageText("level.FTG.conversations.one")
speakcount = speakcount+1
elseif
speakcount = 2 then
ShowMessageText("level.FTG.conversations.two")
speakcount = speakcount+1
elseif
speakcount = 3 then
ShowMessageText("level.FTG.conversations.three")
speakcount = speakcount+1
elseif
speakcount = 4 then
ShowMessageText("level.FTG.conversations.four")
speakcount = speakcount+1
elseif
speakcount = 5 then
ShowMessageText("level.FTG.conversations.five")
speakcount = speakcount+1
end
end,
onesecondtimer
end
)
end
end
)[/code][/size]
I kept the comma since removing it gave me another error and the stock campaign scripts I looked at used the comma in the same place. This time the error made a bit less sense:
Hidden/Spoiler:
[code]C:\BF2_ModTools\data_FTG\_BUILD\Common\..\..\..\ToolsFL\Bin\luac.exe: ..\..\common\scripts\FTG\FTGc_con.lua:121: `then' expected near `='
ERROR[scriptmunge scripts\FTG\FTGc_con.lua]:Could not read input file.ERROR[scriptmunge scripts\FTG\FTGc_con.lua]:Could not read input file. [continuing][/code]
It's telling me to add a "then" near that = mark, but the "then" is there already. I have a nagging feeling I'm going about this the wrong way, but I can't think of any stock campaign scripts with repeating timers to look at for guidance.

When it come's to that, I just don't know that much about timers. What happens when a timer elapses? Is it automatically reset, and I can just use StartTimer to kick it back into motion? And what's the difference, exactly, between a timer reaching zero and me using DestroyTimer? I assume it complete removes the timer from memory, so I have to recreate it if I want to use it again, but I'm not sure, since the stock documentation says not to use a timer at all after it's destroyed.
Firefang wrote:Try looking at the lua from kamino campaign and see how they show the five minute timer on the screen.
That won't quite work for what I have in mind, since I want to display a text message rather than a number.
User avatar
Maveritchell
Jedi Admin
Jedi Admin
Posts: 7366
Joined: Mon Aug 21, 2006 11:03 pm

Re: LUA timed text popups

Post by Maveritchell »

Now you're getting an error because your "if" statements are equations and not equalities: "=" =/= "==".
=
User avatar
Fiodis
Master of the Force
Master of the Force
Posts: 4145
Joined: Wed Nov 12, 2008 9:27 pm
Projects :: Rannoch + Tientia + Tools Programming

Re: LUA timed text popups

Post by Fiodis »

So the if statements are saying "if speakcount is equivalent to #" rather than "if speakcount is equal to #"? Adding in == instead of = gave me the original comma error again, so I took out the comma and got something very strange:
Hidden/Spoiler:
[code]C:\BF2_ModTools\data_FTG\_BUILD\Common\..\..\..\ToolsFL\Bin\luac.exe: ..\..\common\scripts\FTG\FTGc_con.lua:139: `=' expected near `end'[/code]
Hidden/Spoiler:
[code] elseif speakcount == 5 then
ShowMessageText("level.FTG.conversations.five")
speakcount = speakcount +1
end
end
onesecondtimer
end -- Here is line 139.
)[/code][/size]
I can't see how an = anywhere near that particular "end" would make any sense at all. :?
User avatar
Maveritchell
Jedi Admin
Jedi Admin
Posts: 7366
Joined: Mon Aug 21, 2006 11:03 pm

Re: LUA timed text popups

Post by Maveritchell »

Fiodis wrote:So the if statements are saying "if speakcount is equivalent to #" rather than "if speakcount is equal to #"? Adding in == instead of = gave me the original comma error again, so I took out the comma and got something very strange:
Hidden/Spoiler:
[code]C:\BF2_ModTools\data_FTG\_BUILD\Common\..\..\..\ToolsFL\Bin\luac.exe: ..\..\common\scripts\FTG\FTGc_con.lua:139: `=' expected near `end'[/code]
Hidden/Spoiler:
[code] elseif speakcount == 5 then
ShowMessageText("level.FTG.conversations.five")
speakcount = speakcount +1
end
end
onesecondtimer
end -- Here is line 139.
)[/code][/size]
I can't see how an = anywhere near that particular "end" would make any sense at all. :?
Now you are missing a comma. In your first posted code, the comma was incorrect, but in your most recently posted code (just above my last post) the comma was in the correct place, which is why I didn't mention it.
User avatar
Fiodis
Master of the Force
Master of the Force
Posts: 4145
Joined: Wed Nov 12, 2008 9:27 pm
Projects :: Rannoch + Tientia + Tools Programming

Re: LUA timed text popups

Post by Fiodis »

I added a final elseif statement to set the function = to nil after it's done, to save CPU power. Adding in the comma and that new section, the updated code is:
Hidden/Spoiler:
[code]speakcount = 1

textspawn = OnObjectInit(
function(object)
if GetEntityClass(object) == FindEntityClass("rep_bldg_inf_texttest") then
ShowMessageText("level.FTG.conversations.test")
KillObject(object)
onesecondtimer = CreateTimer("onesecondtimer")
SetTimerValue(onesecondtimer, 1)
StartTimer(onesecondtimer)
repeatingsequence = OnTimerElapse(
function(timer)
if GetTimerName(timer) == "onesecondtimer" then
SetTimerValue(onesecondtimer, 1)
StartTimer(onesecondtimer)
if speakcount == 1 then
ShowMessageText("level.FTG.conversations.one")
speakcount = speakcount +1
elseif speakcount == 2 then
ShowMessageText("level.FTG.conversations.two")
speakcount = speakcount +1
elseif speakcount == 3 then
ShowMessageText("level.FTG.conversations.three")
speakcount = speakcount +1
elseif speakcount == 4 then
ShowMessageText("level.FTG.conversations.four")
speakcount = speakcount +1
elseif speakcount == 5 then
ShowMessageText("level.FTG.conversations.five")
speakcount = speakcount +1
elseif speakcount == 6 then
textspawn = nil
end
end, -- This is troublesome line 139.
onesecondtimer
end
)
end
end
)[/code][/size]
The place around the comma gives me another error:

Code: Select all

C:\BF2_ModTools\data_FTG\_BUILD\Common\..\..\..\ToolsFL\Bin\luac.exe: ..\..\common\scripts\FTG\FTGc_con.lua:139: unexpected symbol near `,'
I'm not sure what the symbol could be unless it's the comma itself. For future reference, what is the use of the comma there for?
User avatar
[RDH]Zerted
Gametoast Staff
Gametoast Staff
Posts: 2982
Joined: Sun Feb 26, 2006 7:36 am
Projects :: Bos Wars AI - a RTS game
Games I'm Playing :: SWBF2 and Bos Wars
xbox live or psn: No gamertag set
Location: USA
Contact:

Re: LUA timed text popups

Post by [RDH]Zerted »

No, your comma is in the wrong place, along with the variable after it. The below code has been corrected:

Code: Select all

               elseif speakcount == 5 then
                  ShowMessageText("level.FTG.conversations.five")
                  speakcount = speakcount +1
               elseif speakcount == 6 then
                  textspawn = nil
               end
            end -- This is troublesome line 139. 
         end,  --This end is the end of the function
        onesecondtimer    --This is the second parameter/argument to OnTimerElapse
      )  ---this is the end of OnTimerElapse
      end
   end
)
Also, use the ReleaseXXXX methods to clean up callbacks. They tell the C++ to forget about it. Just doing textspawn = nil only makes Lua forget about it. I don't have the modding docs with me, so the name might be slightly off: ReleaseObjectinit(textspawn) then textspawn = nil and don't forget to kill your timer too.
User avatar
Fiodis
Master of the Force
Master of the Force
Posts: 4145
Joined: Wed Nov 12, 2008 9:27 pm
Projects :: Rannoch + Tientia + Tools Programming

Re: LUA timed text popups

Post by Fiodis »

Ah, thanks Zerted and Mav, that last modification worked! :)
Post Reply