ncsoft / Unreal.js

Unreal.js: Javascript runtime built for UnrealEngine

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Getting datas from HTML - JS console : ReferenceError: document is not defined

medarezki opened this issue · comments

Hi,
I'm trying to get some datas presented in an .html document from a .js file in a document.getElementById('') way but it didn't work.

The JS Console outputs the following error (ReferenceError) : document is not defined
(Remark: the Object is displayed on its default state, it shows "Text" instead of the innerHTML )

Taking this following example

JS side

//JS file (JavaScriptFile.js) - Displaying a text in World
"use strict"

function main() {

    /*Something like :
     var value= document.getElementById('thisone').innerHTML;*/
    
    let actor = new TextRenderActor(GWorld,{X:100,Y:100,Z:100},{Yaw:0})

    actor.TextRender.SetHorizontalAlignment('EHTA_Center')
    actor.TextRender.SetText(value) //Use it here

    return function () {
        actor.DestroyActor()
    }
}

try {
    module.exports = () => {
        let cleanup = null
        process.nextTick(() => cleanup = main());

        return () => cleanup()
    }
}
catch (e) {
    require('bootstrap')('JavaScriptFile')
}

HTML side

<!--HTML File (HTMLfile.html)-->
<!DOCTYPE html>
<html>

<head>
<script src="./JavaScriptFile.js"></script>
</head>
<body>

<div id="thisone">ThisTextValue</div>
</body>
</html>

Is there a way (or a possibility by Node or other) to link the HTML and JS files to get the datas that can be manipulated in Unreal World ?
Thanks.

With document.getElementById('') there will be a ReferenceError, the object refers to a undefined variable, hence, an exception is thrown.

One of the solutions is to use Node's file system module to R/W files.

//Accessing physical file system
const fs = require('fs'); 


// Reading the file (Synchronous function)
/* const data = fs.readFileSync(file, char encoding); */
const data = fs.readFileSync('./whereDatasAreStored.txt', { encoding: 'utf8', flag: 'r' }); 

Starting from this basic example, we can work with other file extensions (txt, csv, et cetera).

Note : Node.js Should be installed