Calling one function from another - function scope

I am sure this is blindingly obvious but… How does one call a function from a second function?

If ‘nested’ within the current function it is fine - e.g.
functionmain() {
//do something;
functionsub() // call secondary function;
function functionsub() {
//do something else to call above;
}
}

How though does to run functionsub() when it is within a main function run on scene load?

The answer must be similar to the difference in scope between var myvariable (local) and window.myvariable (global), but I can’t find it in the documentation.

This is a javascript issue not Hype so if you are looking at the Hype documentation you will not fin it.

Why are you putting the sub function inside the first function?. Just put it outside and call it from within the first function.

function main() {
//do something;

sub() ;// call secondary function;

};


 function sub() {
//do something else to call above;
};

i´m not sure, if this is, what you´re looking for:
call another function within a function:

hypeDocument.functions().name(hypeDocument, element, event);//name = name of calling function
1 Like

‘put it outside’ ? How do you manage that?

In an external script file this would be fine - if using Hype javascript one can only select and type within the containing function… Unless I am missing something absolutely fundamental?

I think you need to be a bit more clear in what you are trying to do.
I do not understand " if using Hype javascript one can only select and type within the containing function "

Post an example project of what you are trying to do, My and @strmiska’s answers are correct ways of calling functions.

Also my assumption was you had your functions inside a Hype Function.

Which in Hype would look like:

// element - DOMHTMLElement that triggered this function being called
// event - event that triggered this function being called
functions untitledFunction(hypeDocument, element, event){	
	
	function main() {
//do something;
sub() ;// call secondary function;

};


function   sub() {
//do something else to call above;
};
	

}
1 Like

My code is like this

1] - triggered on scene load

// element - DOMHTMLElement that triggered this function being called
// event - event that triggered this function being called
function scenefunction(hypeDocument, element, event){

function functionsub() {
//do something to be called from different clicks on different objects
};

}

2] - triggered on a click on an object so in a separate attached function

// element - DOMHTMLElement that triggered this function being called
// event - event that triggered this function being called
function interactionfunction(hypeDocument, element, event){

//do something and then;
functionsub() ;// call secondary function initialised above;

};

strmirska

Your example could be it - I have tried apply it to the scenario just above, but failed so far…

Hi MarkHunte - I replied but failed to link to your post. You will find my rely in the thread above here

please post your document. i use this every time and it works …

Let me clear this up guys! He wants to make a call to the following function but of course it's not gonna be straight forward :slight_smile:

So, @iaeon there are ways you can do this. In fact many ways but for clarity's sake I'll show you this approach.

Store your function in a variable: (This is a New Function on Scene Load)

window.myFunction = function (text) {
    hypeDocument.getElementById('rectangle').innerHTML = text;
}

Now in another New Function ... which we use on say a mouse click we can reference the variable like a function.

myFunction("<h1>Hello</h1>");

###Example:
callingFunctions.hype.zip (14.0 KB)

4 Likes

Yep made sense after 4 attempts :joy:

1 Like

Thanks for your help and patience guys! [quote=“DBear, post:10, topic:6565”]
window.myFunction = function () { // do something }
[/quote]
was what I needed to know!

I attach a test project demonstrating changing the value of a variable from functions declared on scene load in case it helps anyone else to learn this. testfunction.hype.zip (74.7 KB)

2 Likes