Global Array undefined with FontLoader Function











up vote
0
down vote

favorite












Im pretty new, hope someone can help with this trivial question :)
The array is globaly defined var textMeshes = ; and a function to generate the text at a certain position The function works but i cannot access the array by index, I get a console error that textMeshes is not defined. But if i log textMeshes console.log(textMeshes) as hole array i see the array is filled. console.log(textMeshes[0]) results in undefined. I guess the issue is because fontLoader loads asyncron but please correct me if im wrong =). I really appreciate your help!



function addTextToScene (posX, posY, posZ, i){



                var fontLoader = new THREE.FontLoader();
fontLoader.load( 'fonts/droid/droid_sans_regular.typeface.json', function ( font ) {

var textGeometry = new THREE.TextGeometry( '#' + i, {
font: font,
size: 16,
height: 5,
curveSegments: 12,
bevelEnabled: true,
bevelThickness: 1,
bevelSize: 0,
bevelSegments: 5
} );


textMesh = new THREE.Mesh( textGeometry, textMaterial );

var textMaterial = new THREE.MeshPhongMaterial({ color: 0xff0000, specular: 0xffffff, shininess: 100, emissive: 0xffffff});

textMesh.position.x = posX;
textMesh.position.y = posY+50;
textMesh.position.z = posZ-50;

textMeshes.push( textMesh );
scene.add( textMesh );


} );




}









share|improve this question






















  • Please try to share your code as live demo: jsfiddle.net/f2Lommf5
    – Mugen87
    Nov 8 at 16:44










  • @Mugen87 Thanks Mugen :) jsfiddle.net/f2Lommf5/15882
    – notsureiscorrect
    Nov 9 at 20:54















up vote
0
down vote

favorite












Im pretty new, hope someone can help with this trivial question :)
The array is globaly defined var textMeshes = ; and a function to generate the text at a certain position The function works but i cannot access the array by index, I get a console error that textMeshes is not defined. But if i log textMeshes console.log(textMeshes) as hole array i see the array is filled. console.log(textMeshes[0]) results in undefined. I guess the issue is because fontLoader loads asyncron but please correct me if im wrong =). I really appreciate your help!



function addTextToScene (posX, posY, posZ, i){



                var fontLoader = new THREE.FontLoader();
fontLoader.load( 'fonts/droid/droid_sans_regular.typeface.json', function ( font ) {

var textGeometry = new THREE.TextGeometry( '#' + i, {
font: font,
size: 16,
height: 5,
curveSegments: 12,
bevelEnabled: true,
bevelThickness: 1,
bevelSize: 0,
bevelSegments: 5
} );


textMesh = new THREE.Mesh( textGeometry, textMaterial );

var textMaterial = new THREE.MeshPhongMaterial({ color: 0xff0000, specular: 0xffffff, shininess: 100, emissive: 0xffffff});

textMesh.position.x = posX;
textMesh.position.y = posY+50;
textMesh.position.z = posZ-50;

textMeshes.push( textMesh );
scene.add( textMesh );


} );




}









share|improve this question






















  • Please try to share your code as live demo: jsfiddle.net/f2Lommf5
    – Mugen87
    Nov 8 at 16:44










  • @Mugen87 Thanks Mugen :) jsfiddle.net/f2Lommf5/15882
    – notsureiscorrect
    Nov 9 at 20:54













up vote
0
down vote

favorite









up vote
0
down vote

favorite











Im pretty new, hope someone can help with this trivial question :)
The array is globaly defined var textMeshes = ; and a function to generate the text at a certain position The function works but i cannot access the array by index, I get a console error that textMeshes is not defined. But if i log textMeshes console.log(textMeshes) as hole array i see the array is filled. console.log(textMeshes[0]) results in undefined. I guess the issue is because fontLoader loads asyncron but please correct me if im wrong =). I really appreciate your help!



function addTextToScene (posX, posY, posZ, i){



                var fontLoader = new THREE.FontLoader();
fontLoader.load( 'fonts/droid/droid_sans_regular.typeface.json', function ( font ) {

var textGeometry = new THREE.TextGeometry( '#' + i, {
font: font,
size: 16,
height: 5,
curveSegments: 12,
bevelEnabled: true,
bevelThickness: 1,
bevelSize: 0,
bevelSegments: 5
} );


textMesh = new THREE.Mesh( textGeometry, textMaterial );

var textMaterial = new THREE.MeshPhongMaterial({ color: 0xff0000, specular: 0xffffff, shininess: 100, emissive: 0xffffff});

textMesh.position.x = posX;
textMesh.position.y = posY+50;
textMesh.position.z = posZ-50;

textMeshes.push( textMesh );
scene.add( textMesh );


} );




}









share|improve this question













Im pretty new, hope someone can help with this trivial question :)
The array is globaly defined var textMeshes = ; and a function to generate the text at a certain position The function works but i cannot access the array by index, I get a console error that textMeshes is not defined. But if i log textMeshes console.log(textMeshes) as hole array i see the array is filled. console.log(textMeshes[0]) results in undefined. I guess the issue is because fontLoader loads asyncron but please correct me if im wrong =). I really appreciate your help!



function addTextToScene (posX, posY, posZ, i){



                var fontLoader = new THREE.FontLoader();
fontLoader.load( 'fonts/droid/droid_sans_regular.typeface.json', function ( font ) {

var textGeometry = new THREE.TextGeometry( '#' + i, {
font: font,
size: 16,
height: 5,
curveSegments: 12,
bevelEnabled: true,
bevelThickness: 1,
bevelSize: 0,
bevelSegments: 5
} );


textMesh = new THREE.Mesh( textGeometry, textMaterial );

var textMaterial = new THREE.MeshPhongMaterial({ color: 0xff0000, specular: 0xffffff, shininess: 100, emissive: 0xffffff});

textMesh.position.x = posX;
textMesh.position.y = posY+50;
textMesh.position.z = posZ-50;

textMeshes.push( textMesh );
scene.add( textMesh );


} );




}






three.js






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 8 at 13:18









notsureiscorrect

83




83












  • Please try to share your code as live demo: jsfiddle.net/f2Lommf5
    – Mugen87
    Nov 8 at 16:44










  • @Mugen87 Thanks Mugen :) jsfiddle.net/f2Lommf5/15882
    – notsureiscorrect
    Nov 9 at 20:54


















  • Please try to share your code as live demo: jsfiddle.net/f2Lommf5
    – Mugen87
    Nov 8 at 16:44










  • @Mugen87 Thanks Mugen :) jsfiddle.net/f2Lommf5/15882
    – notsureiscorrect
    Nov 9 at 20:54
















Please try to share your code as live demo: jsfiddle.net/f2Lommf5
– Mugen87
Nov 8 at 16:44




Please try to share your code as live demo: jsfiddle.net/f2Lommf5
– Mugen87
Nov 8 at 16:44












@Mugen87 Thanks Mugen :) jsfiddle.net/f2Lommf5/15882
– notsureiscorrect
Nov 9 at 20:54




@Mugen87 Thanks Mugen :) jsfiddle.net/f2Lommf5/15882
– notsureiscorrect
Nov 9 at 20:54












1 Answer
1






active

oldest

votes

















up vote
0
down vote













You are correct: the FontLoader.load() callback is asynchronous, so textMeshes going to be empty until after its content is loaded. Only then will this line take place:



textMeshes.push( textMesh );


before that, textMeshes[0] is going to be undefined because texMeshes has nothing inside it; it has a length of 0.



This is not much of a Three.js question, but more of an AJAX question. You should read this discussion to get more acquainted with the topic: How do I return the response from an asynchronous call?






share|improve this answer





















  • Thanks for the link i will read it asap, but this seems a good hint.
    – notsureiscorrect
    Nov 9 at 20:55











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53208582%2fglobal-array-undefined-with-fontloader-function%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote













You are correct: the FontLoader.load() callback is asynchronous, so textMeshes going to be empty until after its content is loaded. Only then will this line take place:



textMeshes.push( textMesh );


before that, textMeshes[0] is going to be undefined because texMeshes has nothing inside it; it has a length of 0.



This is not much of a Three.js question, but more of an AJAX question. You should read this discussion to get more acquainted with the topic: How do I return the response from an asynchronous call?






share|improve this answer





















  • Thanks for the link i will read it asap, but this seems a good hint.
    – notsureiscorrect
    Nov 9 at 20:55















up vote
0
down vote













You are correct: the FontLoader.load() callback is asynchronous, so textMeshes going to be empty until after its content is loaded. Only then will this line take place:



textMeshes.push( textMesh );


before that, textMeshes[0] is going to be undefined because texMeshes has nothing inside it; it has a length of 0.



This is not much of a Three.js question, but more of an AJAX question. You should read this discussion to get more acquainted with the topic: How do I return the response from an asynchronous call?






share|improve this answer





















  • Thanks for the link i will read it asap, but this seems a good hint.
    – notsureiscorrect
    Nov 9 at 20:55













up vote
0
down vote










up vote
0
down vote









You are correct: the FontLoader.load() callback is asynchronous, so textMeshes going to be empty until after its content is loaded. Only then will this line take place:



textMeshes.push( textMesh );


before that, textMeshes[0] is going to be undefined because texMeshes has nothing inside it; it has a length of 0.



This is not much of a Three.js question, but more of an AJAX question. You should read this discussion to get more acquainted with the topic: How do I return the response from an asynchronous call?






share|improve this answer












You are correct: the FontLoader.load() callback is asynchronous, so textMeshes going to be empty until after its content is loaded. Only then will this line take place:



textMeshes.push( textMesh );


before that, textMeshes[0] is going to be undefined because texMeshes has nothing inside it; it has a length of 0.



This is not much of a Three.js question, but more of an AJAX question. You should read this discussion to get more acquainted with the topic: How do I return the response from an asynchronous call?







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 9 at 0:16









Marquizzo

5,16331942




5,16331942












  • Thanks for the link i will read it asap, but this seems a good hint.
    – notsureiscorrect
    Nov 9 at 20:55


















  • Thanks for the link i will read it asap, but this seems a good hint.
    – notsureiscorrect
    Nov 9 at 20:55
















Thanks for the link i will read it asap, but this seems a good hint.
– notsureiscorrect
Nov 9 at 20:55




Thanks for the link i will read it asap, but this seems a good hint.
– notsureiscorrect
Nov 9 at 20:55


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53208582%2fglobal-array-undefined-with-fontloader-function%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Guess what letter conforming each word

Port of Spain

Run scheduled task as local user group (not BUILTIN)