Testting to see if a variable is valid

This is a moderated forum that collects tutorials, guides, and references for creating Transcendence extensions and scripts.
Post Reply
User avatar
Betelgeuse
Fleet Officer
Fleet Officer
Posts: 1920
Joined: Sun Mar 05, 2006 6:31 am

This only applies to local or global variables. Type variables are always valid.

to test you just put the variable in a isError function like this

(isError myVar)

this will return true if that is not a valid variable and nil if it is valid.

to test if a string names a valid variable just put a eval in there

(isError (eval "myVar"))
Crying is not a proper retort!
User avatar
Periculi
Fleet Officer
Fleet Officer
Posts: 1282
Joined: Sat Oct 13, 2007 7:48 pm
Location: Necroposting in a forum near you

oh, ok great!

So now I can see if a remote extension exists.

For instance- I have the nav computer mod and the event manager mod. If the event manager mod is loaded, I want the nav computer to display the timestamps for system visits such as "You first visited St Katharine's on [date]". The date is only available if the event manager mod is running, if not I don't want to have any time-based information showing.

So now I can see if certain globals have been set up to configure one mod to work with another without needing to actually build them together!

In the <Globals> of the mod to be checked I added a simple value:

Code: Select all

<Globals>
 (block nil
 
  (setq testMod "test mod is loaded!")


  )
</Globals>
In the checking mod I now have a code block like this:

Code: Select all

	(if (eq (iserror testMod) True)
		(setq desc "No Other Mods Loaded")
	 	(setq desc testMod)
	)
If the extension holding the variable 'testMod' is loaded, then testMod is set and (setq desc testMod) returns what is in testMod from the mod being checked.

If not, then the error is ignored and the description is set to "No Other Mods Loaded"

Before I was getting the error, but with the use of (iserror I can simply check for the error rather than have it effect my script in an undesired way.

Thanks Betel. :)
User avatar
digdug
Fleet Admiral
Fleet Admiral
Posts: 2620
Joined: Mon Oct 29, 2007 9:23 pm
Location: Decoding hieroglyphics on Tan-Ru-Dorem

Can isError be used to check if a int Function from the Globals has been loaded ?

like (isError intMyGlobalFunction) ?


I was thinking about a mod that contains a number of helper functions that will facilitate coding for other mods for everyone. (math helper functions, string manipulating functions, item filtering functions, topology helper functions, just to say a few)
User avatar
Atarlost
Fleet Admiral
Fleet Admiral
Posts: 2391
Joined: Tue Aug 26, 2008 12:02 am

I'm not sure what is meant by "type variable". Can I use this to check if something is defined inthe staticData for a given object?
User avatar
Betelgeuse
Fleet Officer
Fleet Officer
Posts: 1920
Joined: Sun Mar 05, 2006 6:31 am

a type variable is any data belonging to a ship, station, or item that is accessed through the various data functions.

For example all static data is valid. If not defined in the xml it is assumed to be nil. The isn't a way of telling the difference between nil in the static data and it not being there but that doesn't matter in the case of static data because you can't change it anyway. So if you want to see if something is in it just get it and check if it is nil or not.
Crying is not a proper retort!
User avatar
digdug
Fleet Admiral
Fleet Admiral
Posts: 2620
Joined: Mon Oct 29, 2007 9:23 pm
Location: Decoding hieroglyphics on Tan-Ru-Dorem

let's say you have a mod with external "modules" in various xml files.
In these modules there are lambda functions that you want to run, skipping the missing ones and without giving errors.

So I wrote this little piece of code, it loops over a number of lambda funtions, if it exists, it's run, if not, it's skipped.

Code: Select all

;call all the other external modules
						(setq HookCounter 1)
						(loop (ls HookCounter 20)
						 (Block Nil
							
							(if
							 (eq (isError (eval (cat "Hook" HookCounter))) True)
							 (block Nil
								(dbgLog (cat "No Other Mods Loaded: " HookCounter))
								
								)
							 (block (tempCalledHook)
								
								(dbgLog (cat "we reached the mod: " HookCounter))
								(setq tempCalledHook (cat "Hook" HookCounter))
								((eval tempCalledHook))
								)
							 )
							(setq HookCounter (add HookCounter 1))
							)
						 )
I will use it for calling external user-made functions for my mod.
isError and eval were tricky to use. :P
User avatar
Aury
Fleet Admiral
Fleet Admiral
Posts: 5421
Joined: Tue Feb 05, 2008 1:10 am
Location: Somewhere in the Frontier on a Hycrotan station, working on new ships.

Oh just FYI, there was a bug found with this function, where one particular setup caused it to crash, regardless of it being there. I didn't see it on the bug tracker, so it must've been here on OTF.

It'll be fixed in 1.0 though.
(shpOrder gPlayership 'barrelRoll)
(plySetGenome gPlayer (list 'Varalyn 'nonBinary))
Homelab Servers: Xeon Silver 4110, 16GB | Via Quadcore C4650, 16GB | Athlon 200GE, 8GB | i7 7800X, 32GB | Threadripper 1950X, 32GB | Atom x5 8350, 4GB | Opteron 8174, 16GB | Xeon E5 2620 v3, 8GB | 2x Xeon Silver 4116, 96GB, 2x 1080ti | i7 8700, 32GB, 6500XT
Workstations & Render machines: Threadripper 3990X, 128GB, 6900XT | Threadripper 2990WX, 32GB, 1080ti | Xeon Platinum 8173M, 48GB, 1070ti | R9 3900X, 16GB, Vega64 | 2x E5 2430L v2, 24GB, 970 | R7 3700X, 32GB, A6000
Gaming Systems: R9 5950X, 32GB, 6700XT
Office Systems: Xeon 5318Y, 256GB, A4000
Misc Systems: R5 3500U, 20GB | R5 2400G, 16GB | i5 7640X, 16GB, Vega56 | E5 2620, 8GB, R5 260 | P4 1.8ghz, 0.75GB, Voodoo 5 5500 | Athlon 64 x2 4400+, 1.5GB, FX 5800 Ultra | Pentium D 3.2ghz, 4GB, 7600gt | Celeron g460, 8GB, 730gt | 2x Athlon FX 74, 8GB, 8800gts 512 | FX 9590, 16GB, R9 295x2 | E350, 8GB | Phenom X4 2.6ghz, 16GB, 8800gt | random core2 duo/atom/i5/i7 laptops
Post Reply