Matrix Games Forums

Forums  Register  Login  Photo Gallery  Member List  Search  Calendars  FAQ 

My Profile  Inbox  Address Book  My Subscription  My Forums  Log Out

SafeGetUnit (pcall example)

 
View related threads: (in this forum | in all forums)

Logged in as: Guest
Users viewing this topic: none
  Printable Version
All Forums >> [New Releases from Matrix Games] >> Command: Modern Operations series >> Mods and Scenarios >> Lua Legion >> SafeGetUnit (pcall example) Page: [1]
Login
Message << Older Topic   Newer Topic >>
SafeGetUnit (pcall example) - 3/13/2021 11:52:43 AM   
jkgarner

 

Posts: 174
Joined: 4/30/2020
Status: offline
OK,
Apparently, Lua code execute by events will terminate the event execution if there is an error, like ScenEdit_GetUnit fails...

I was seeing this on the forums, but did not pay much attention to it...to my regret, as I am now writing substantial event driven Lua code...

Since most of my events deal with a unit, I though a SafeGetUnit function might be helpful (too prevent repeatedly writing pcall, and potential errors with each instance...)

The function is provided in the attachment, along with test code that proves that the function behaves as described.

If you find it useful, use it.

Attachment (1)
Post #: 1
RE: SafeGetUnit (pcall example) - 3/13/2021 1:44:04 PM   
Parel803

 

Posts: 579
Joined: 10/10/2019
From: Netherlands
Status: offline
thank you,
with regards GJ

(in reply to jkgarner)
Post #: 2
RE: SafeGetUnit (pcall example) - 3/13/2021 7:40:25 PM   
michaelm75au


Posts: 13500
Joined: 5/5/2001
From: Melbourne, Australia
Status: offline
As mentioned before, most functions will return an error when they are run in the console. It is up to the user to check outcomes of function calls during events as the functions are set to run non-interactively.
If there is a specific case where the function call does not return something to test, please give me an example save file showing it to investigate.

_____________________________

Michael

(in reply to Parel803)
Post #: 3
RE: SafeGetUnit (pcall example) - 3/14/2021 1:22:38 AM   
jkgarner

 

Posts: 174
Joined: 4/30/2020
Status: offline
OK, so a little more testing shows that a safe GetUnit is not necessary, except as instruciton on using pcall to trap errors.

The following code works for events:
Tool_EmulateNoConsole(true)
ScenEdit_AddSide({name='test'})
local u = ScenEdit_GetUnit({side='test', name='asdf'})
if(u ~= nil) then
    --print('u was defined')
    ScenEdit_MsgBox ('u was defined',1)
else
    --print('u not defined')
    ScenEdit_MsgBox ('u not defined',1)
end
ScenEdit_RemoveSide({name='test'})
Tool_EmulateNoConsole(false)
The Tool_EmulateNoCosole, sets the console to execution as though it were executing from an event, and swallows print statments and errors.

As a word of caution, you MUST check the return value of all your calls, because they may have failed, and returned nil.

Tracking down errors in event code is tedious business, as any error will terminate the script. (often without notice)

I am trying to find a way to build automated unit tests for my event code. I have not figured it out yet. If anybody has ideas on how to do that, I'd appreciate a discussion on the topic.



(in reply to michaelm75au)
Post #: 4
Page:   [1]
All Forums >> [New Releases from Matrix Games] >> Command: Modern Operations series >> Mods and Scenarios >> Lua Legion >> SafeGetUnit (pcall example) Page: [1]
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts


Forum Software © ASPPlayground.NET Advanced Edition 2.4.5 ANSI

0.703