Er... Global Dilemma. :(

Post ideas & suggestions you have pertaining to the game here.
Post Reply
User avatar
Periculi
Fleet Officer
Fleet Officer
Posts: 1282
Joined: Sat Oct 13, 2007 7:48 pm
Location: Necroposting in a forum near you

Could someone walk me through "my first global function" please?

I don't understand the process very well. What is needed in a global? What needs to be lambda'ed? Why?

Here's the situation: I think that it would be easier to do some of the things I am trying to do from a global rather than from each little entry. This is particularly for mods such as the random equipment mod I am building- where lots of ships are doing the same kind of code on create- but slightly differently.

rather than <onCreate> (and a whole table of weapons and shields) </onCreate> for each ship

I want <onCreate> (intChooseYourWeapon) </onCreate>

or even maybe <OnCreate> (block (setShip) (setq setShip "the ship")(intChooseMyEquipment)) or some other means to pass the type of ship from the on create to the global equipment function.

Then I can put the tables together in one place and adjust or add to them. Also this would allow different ships to use parts of one table- adjusting the random number selector instead of the individual tables for ship types.

Oh.. uh, wrong forum, sorry
User avatar
Betelgeuse
Fleet Officer
Fleet Officer
Posts: 1920
Joined: Sun Mar 05, 2006 6:31 am

You need a tag called Globals.
That tag takes one expression so for our case lets make it a block. (make sure the block has no temp variables.)

then in the block you would make your function

they follow this form

Code: Select all

(setq nameOfFunction (lambda (argumentList)
	;code here
))
So all in all you have

Code: Select all

<Globals>
	(block Nil
		(setq nameOfFunction (lambda (argumentList)
			;code here
		))
	)
</Globals>
Now you have a global function named nameOfFunction. Global variables can be made in the same fashion, just skip the lambda part)

for example

Code: Select all

<Globals>
	(block Nil
		(setq squared (lambda (number)
			(multiply number number)
		))

		(setq timesTwo (lambda (number)
			(multiply number 2)
		))
	)
</Globals>
This will give a you two functions called squared and timesTwo. You would use them just like any other functions. For example (squared 5) would give you 25.

if you have any more questions feel free to join the irc channel and ask there.
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

Thanks, Betel. My biggest problem had been figuring out how the global was getting passed data- the lambda part.

But I think I understand it better now. :)
Post Reply