View Full Version : Global functions??

10-23-2006, 03:24 PM
Just curious more than anything else...

Is it possible to compile components with _global functions? I notice that you can put them into a mc and then give the mc some component definitions which gives it the appearance of a component except it can be edited just like any other mc. However, that of course is not a true component because it is not an swc file. If you export it as a component file, _global functions no longer seem to work. Is it possible, or am I wasting my brain power thinking about it?? :rolleyes:

10-24-2006, 03:54 PM
why would you do that? it would defeat the purpose of a component. the whole reason to have a component is so the all the code necessary to make it work is all in one place, and it is portable. if it depends on global functions then when you move your component to a new project, you would need to move all of the global funcitons also....

10-25-2006, 04:53 PM
eh... just trying something.

so the all the code necessary to make it work is all in one place, and it is portable

I was under the impression that's what _global variables and functions are for. You define it once and it is accessible everywhere :p

you would need to move all of the global funcitons also....

If your talking about the fact that declaring the same function more than once would cause problems, easily can be solved by putting the following into the component script:

if(myGlobalFunction == undefined){
_global.myGlobalFunction = function(){
//bla bla bla....

Basically just think of it like this... you have a component but you don't want to have to refer to it relatively or absolutely throughout your code, (i.e. - _root.mc1.mc1_s1.myComponent.doSomething(); ), you just want to call the function from anywhere like a _global function

10-25-2006, 06:24 PM
if you make a component, then transfer it to a different project, if the new project does not have your function in it, it will return undefined. which is precisely why a component should have all code it uses defined within the scope of the component.

10-25-2006, 07:20 PM
Not sure we're on the same page. From what I gather, you think I'm talking about a component accessing _global functions from outside itself, right?

I'm actually talking about putting a _global function within a component, so wherever the component goes, the function goes with it. Does that make sense?

Let me know if I'm wrong, it is a rather confusing question.

10-28-2006, 09:40 PM
nevermind, figured it out.

Turns out it had nothing to do with the _global property or the function or the fact that it was in a component. Just forgot to add in the classpath to the component definition, so the _global functions didn't know what they were supposed to call :o