helpful code tricks

This is a moderated forum that collects tutorials, guides, and references for creating Transcendence extensions and scripts.
User avatar
Periculi
Fleet Officer
Fleet Officer
Posts: 1282
Joined: Sat Oct 13, 2007 7:48 pm
Location: Necroposting in a forum near you

You know, I was thinking we could add sample scripts to the Sources section on xelerus and compile a sort of library for people to look through to find examples of things- I posted a sample of how to get the <OnAttackedByPlayer> event to work as a starter, and I think I could put together a bunch more little samples like that.

Do you think this would be a good resource to build?
User avatar
Betelgeuse
Fleet Officer
Fleet Officer
Posts: 1920
Joined: Sun Mar 05, 2006 6:31 am

sounds good can you also make one for custom picker screens? To tell the truth I am not very good at the various screens and would love some explanations of them. (at least until we get around to making the element doc :D)
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

Absolutely! I was already thinking about doing that. :)

I think that the customPicker is my new favorite dockscreen, it has made everything I want to do for mapping mods possible and I am really glad that George let me know it was in there.
User avatar
Betelgeuse
Fleet Officer
Fleet Officer
Posts: 1920
Joined: Sun Mar 05, 2006 6:31 am

Recursion:
Recursion is simply a function that calls itself directly (as in within the function) or indirectly (it calls a function that calls it). Very powerful and very easy to mess up.

anyway I mostly wanted to show off an useful example of recursion. Most things will not be effected when passed into a function (no matter what they do to the numbers and strings you passes it will not effect you) lists are a different matter. Such as if the function calls lnkRemove on the passed in list it will effect your list. Sometimes you don't want that so what do you do?

The simple answer is to make a copy and pass that in but making a copy isn't as simple as it sounds. Making a temp variable and setq doesn't have the effect that you want and if you just copy the top layer of elements lnkRemove could still mess up your list by changing the more inner elements (changing the elements in the lists within your list).

Here is some code that copies a list and I will describe what it is doing.

Code: Select all

(setq listCopy (lambda (inputlist)
	(block (outputList)
		(enum inputlist element
			(if (isAtom element)
				;if the element is a atom just append
				(setq outputList (lnkappend outputList element))

				;if not (ie its a list) copy then append
				(setq outputList (lnkappend outputList (listCopy element)))
			)		

		)
		outputList
	)
))
Not too complex of a function. First it makes a temp variable called outputList.
Now for every element of that list it asks is it an atom or not (an atom is anything that is nil or isn't a list).
If it is a atom then it just appends that on to the output list. (easy right?)
If not then it is a list that needs coping (the exact problem we are making this function for) so lets just use this function to make a copy of that list and append it on (this is the recursion).
Now all that is left is to return the outputList and you are done.

Any questions? (I recommend walking through the code with some lists to get a better idea how recursion works ie the list ((a b) (c d) e) is a good example)
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

Recursion:
Recursion is simply a function that calls itself directly
is that really working? self calling funtions can be very powerful :)
User avatar
Betelgeuse
Fleet Officer
Fleet Officer
Posts: 1920
Joined: Sun Mar 05, 2006 6:31 am

yep works fine 8)
Crying is not a proper retort!
Post Reply