Trying to integrate ethereum with C3, but get js error

0 favourites
From the Asset Store
[C2] [C3] Support C3 build service and Android 14
  • To use ethereum, I need the web3 library, I import it and put it under the "Scripts" folder, but when I start the game I get the error:

    "Failed to load project script 'web3.min.js'. Check all your JavaScript code has valid syntax."

    Any ideas what can I do? Is it even possible what I'm trying?

    Thanks

    Tagged:

  • This is possible in Phaser, but would love to be able to do this in C3 instead.

  • Likely due to 'worker' mode.

    Project->Properties->Advanced, uncheck 'Use Worker'.

    I am curious, how are you going to use ethereum in your C3 project?

    In the future, you can press F12 (on Windows) to see the console during the preview and there you would have seen that the error was that 'window' was defined, which is an indication that the JS is not compatible to run in a worker (window is not defined in the worker space.) The C3 manual and some of the C3 blogs have details on worker mode.

  • I want to make a game, where at the end of the level I count how many coins the player has grabbed, and then mint that number of my custom token and award them to the player. I know there are security issues (a player might hack the game and spawn as many coins he wants, or modify the variable, but I'll tackle one issue at a time).

    Turning off worker mode fixed the load script issue. But when I try to use web3 on my custom js script, I get the error:

    "ReferenceError: web3 is not defined"

    This is the line:

    web3 = new web3(new web3.providers.HttpProvider("http://localhost:7545"));
    

    Both scripts are inside the "Scripts" folder, which I have understood are loaded automatically and the start.

    I've been a Construct user for years, but this is my first time using js inside C3, thanks for the help!

  • [It looks like the post I was replying to was deleted, so I hope you already solved the issue]

    I am not exactly sure why you are seeing that error, but I remember a discussion about the order of loading and execution of the script files may be a bit difficult to control.

    construct.net/en/make-games/manuals/construct-3/scripting/using-scripting/script-files

    One simple way around this is to put all your scripts in one big file, but I think one of your files is big and minified.

    So another way to do this is to load the script files in the order you want by putting the files in the Project Files folder and then in the Project Scripts folder use the default script and load them in the order you want:

    runOnStartup(async runtime =>

    {

    runtime.assets.loadScripts("script1.js", "script2.js", "script3.js")

    // This code runs on startup with

    // the 'runtime' variable available

    });

    See: construct.net/en/make-games/manuals/construct-3/scripting/scripting-reference/interfaces/iassetmanager

    Interesting project, I also thought about doing something similar with a custom token, I was going to do the token issuing on a Playfab server to have a little more control (e.g. player registration, limits on tokens created, a little authentication, etc.)

  • I have no idea why my post was deleted... maybe the forums are bugged.

    So, I moved the web3 file to the project files folder, and keep my main js file in the scripts folder. I wrote this:

    runOnStartup(async runtime =>
    
    {
    
    runtime.assets.loadScripts("web3.min.js");
    
    // This code runs on startup with
    
    // the 'runtime' variable available
    
    });
    
    web3 = new web3(new web3.providers.HttpProvider("http://localhost:7545"));

    But still get the "web3 is not a constructor" error... sigh

  • loadScripts is an async function. You need to await it, otherwise you'll try to use the things it's declared before it's loaded.

  • Ashley made some good comments about using await.

    If you need another possibility you could put your code that calls web3 into another script and have it load after the web3.min.js.

    For example, if you create a script called web3Example.js with

    web3Example.js:

    web3 = new web3(new web3.providers.HttpProvider("http://localhost:7545"));
    

    Then your other script would look like:

    runOnStartup(async runtime =>
    
    {
    
    await runtime.assets.loadScripts("web3.min.js", "web3Example.js:");
    
    // This code runs on startup with
    
    // the 'runtime' variable available
    
    });
    
    
  • Thanks for the suggestions, but I get the same "not constructor" error. I know there are other libraries for ethereum besides web3, I will try those, really want to make this work.

  • Ah! It's because of a typo I think, I just got this to work in a project. Check your capitalization (for 'Web3'.)

    Script.js:

    // Put any global functions etc. here
    
    runOnStartup(async runtime =>
    {
    	// Code to run on the loading screen.
    	// Note layouts, objects etc. are not yet available.
    	await runtime.assets.loadScripts("web3.min.js","a.js");
    
    	
    	runtime.addEventListener("beforeprojectstart", () => OnBeforeProjectStart(runtime));
    });
    
    function OnBeforeProjectStart(runtime)
    {
    	// Code to run just before 'On start of layout' on
    	// the first layout. Loading has finished and initial
    	// instances are created and available to use here.
    	
    	runtime.addEventListener("tick", () => Tick(runtime));
    }
    
    function Tick(runtime)
    {
    	// Code to run every tick
    }
    

    Under project files, I have web3.min.js and a.js

    a.js:

    console.log("a script")
    var web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:7545"));
    console.log(web3)
    
  • YOU ARE AN ABSOLUTE GENIUS!

    Thanks, it is working, now I'm going to integrate the token into the gameplay...

  • I realize this post is rather old, but can you either of you walk through how you got this working?

    When I tried to follow this I get the same web3 error. I did double check the case.

    OK, I got it working pretty much. But I had to go into classic mode for script type. How do we get it working in module mode as classic will be removed at some point

  • I realize this post is rather old, but can you either of you walk through how you got this working?

    When I tried to follow this I get the same web3 error. I did double check the case.

    OK, I got it working pretty much. But I had to go into classic mode for script type. How do we get it working in module mode as classic will be removed at some point

    This is a question for Ashley

    A robust support for web3 and crypto would be a homerun for C3.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Agree, especially right now with Crypto blowing up.

    Come on Ashley :)

  • You need to contact the library developer about this. They need to make sure the library supports JavaScript Modules.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)