Old Code, Do I still need to make these checks?

Please post any questions about developing your plugin here. Please use the search function before posting!
User avatar
velocity
Senior Member
Posts: 220
Joined: Sat May 10, 2014 6:17 pm

Old Code, Do I still need to make these checks?

Postby velocity » Tue Oct 12, 2021 12:45 pm

Hi! It has been a long time, since I was active here. So I was reading through some old code and I was wondering if these try-catch cases are still necessary to prevent crashes and is this still the "correct" way to do this?


Syntax: Select all

@EntityPreHook(EntityCondition.equals_entity_classname('trigger_multiple'), 'start_touch')
def start_touch_func(args):
try:
obj1 = make_object(BaseEntity, args[0])
except:
return
try:
ob2 = make_object(BaseEntity, args[1])
except:
return
# do more here...
User avatar
L'In20Cible
Project Leader
Posts: 1533
Joined: Sat Jul 14, 2012 9:29 pm
Location: Québec

Re: Old Code, Do I still need to make these checks?

Postby L'In20Cible » Tue Oct 12, 2021 1:21 pm

velocity wrote:Hi! It has been a long time, since I was active here. So I was reading through some old code and I was wondering if these try-catch cases are still necessary to prevent crashes and is this still the "correct" way to do this?


Syntax: Select all

@EntityPreHook(EntityCondition.equals_entity_classname('trigger_multiple'), 'start_touch')
def start_touch_func(args):
try:
obj1 = make_object(BaseEntity, args[0])
except:
return
try:
ob2 = make_object(BaseEntity, args[1])
except:
return
# do more here...
This never would have prevented a crash. Worst that can happens is a RuntimeError if one of these pointer is not a valid entity. Which effectively have the same result as a return, since the execution stops anyway (minus the exception being logged depending of your logging config). However, chances the game call that function with invalid entities are next to null so I'd say these try block are redundant.
User avatar
velocity
Senior Member
Posts: 220
Joined: Sat May 10, 2014 6:17 pm

Re: Old Code, Do I still need to make these checks?

Postby velocity » Thu Oct 14, 2021 8:25 am

Alright thanks!

Return to “Plugin Development Support”

Who is online

Users browsing this forum: No registered users and 30 guests