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

Sun Aug 17, 2008 11:18 pm

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: 1259
Joined: Sat Oct 13, 2007 7:48 pm
Location: By the beach

Sun Aug 17, 2008 11:37 pm

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: 2607
Joined: Mon Oct 29, 2007 9:23 pm
Location: Decoding hieroglyphics on Tan-Ru-Dorem

Mon Aug 18, 2008 12:53 am

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

Mon Sep 22, 2008 3:56 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

Mon Sep 22, 2008 4:24 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: 2607
Joined: Mon Oct 29, 2007 9:23 pm
Location: Decoding hieroglyphics on Tan-Ru-Dorem

Sun Apr 26, 2009 6:54 pm

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
Wolfy
Fleet Admiral
Fleet Admiral
Posts: 5353
Joined: Tue Feb 05, 2008 1:10 am
Location: Somewhere in the Frontier on a Hycrotan station, working on new ships.

Sun Jul 05, 2009 4:24 am

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 'hycrotan 'nonBinary))
Image

Post Reply