ProjectsWhat's NewDownloadsCommunitySupportCompany
Forum Index » S.T.A.L.K.E.R.: Shadow of Chernobyl Forum » Mod discussion
Deliberate script class init failure?

Posted by/on
Question/AnswerMake Newest Up Sort by Descending
  23:49:24  21 April 2016
profilee-mailreply Message URLTo the Top
TKGP
Senior Resident
 

 
On forum: 01/25/2013
Messages: 454
Deliberate script class init failure?

I would like to be able to have the initialization of a script-defined class fail gracefully (and return nil). Something like this:

class "VideoGames"
function VideoGames:__init()
	return false
end
function VideoGames:__finalize() end

local test = VideoGames()
if not test then
	-- This doesn't work, but I want it to
end


Is there a way to do this? I could just set a failure flag on the object and check for it afterwards, but I would prefer this way.
  09:55:33  24 April 2016
profilee-mailreply Message URLTo the Top
NatVac
Senior Resident
 

 
On forum: 06/15/2007
Messages: 4302
My brain is furry at the moment (gypsum drywall/cement dust and sleep deprivation), but I don't know any way without experimenting.

As you've figured out, what one gets is the class instance object, not the __init() return value. If the object is not instantiated the 'test' variable is nil (which can be different from false if one explicitly tests for false, I vaguely recall -- default 'if not test' should be true for either nil or false).

Some sort of "delete self on fail" probably would not be processed in time for the object check to show nil in the routine just after the instantiation. Or would it?

You could look into the C++/Lua interface support, maybe query an upvalue or even rewrite the constructor code to permit return values. (I really am tired.)

Maybe passing in a reference as an argument to the constructor and testing that would not be that much of a burden on processing.

The rest of my thinking is nonsense even to me, so I'll just wish you well in your search and go get some rest.
  01:51:35  25 April 2016
profilee-mailreply Message URLTo the Top
TKGP
Senior Resident
 

 
On forum: 01/25/2013
Messages: 454
It's purely a convenience issue, so bin mods would probably be a bit overkill. At the moment I'm just using a wrapper function, which works fine if there's no built-in way to do it.

function GetVideoGames()
	local videoGames = VideoGames()
	return videoGames.success and videoGames or nil
end

 
Each word should be at least 3 characters long.
Search:    
Search conditions:    - spaces as AND    - spaces as OR   
 
Forum Index » S.T.A.L.K.E.R.: Shadow of Chernobyl Forum » Mod discussion
 

All short dates are in Month-Day-Year format.


 

Copyright © 1995-2022 GSC Game World. All rights reserved.
This site is best viewed in Internet Explorer 4.xx and up and Javascript enabled. Webmaster.
Opera Software products are not supported.
If any problem concerning the site functioning under Opera Software appears apply
to Opera Software technical support service.