Page 1 of 1
Testting to see if a variable is valid
Posted: Sun Aug 17, 2008 11:18 pm
by Betelgeuse
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"))
Posted: Sun Aug 17, 2008 11:37 pm
by Periculi
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.

Posted: Mon Aug 18, 2008 12:53 am
by digdug
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)
Posted: Mon Sep 22, 2008 3:56 am
by Atarlost
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?
Posted: Mon Sep 22, 2008 4:24 am
by Betelgeuse
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.
Posted: Sun Apr 26, 2009 6:54 pm
by digdug
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.

Posted: Sun Jul 05, 2009 4:24 am
by Aury
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.