Help creating variables and functions on javascript

0 favourites
  • 12 posts
From the Asset Store
Game with complete Source-Code (Construct 3 / .c3p) + HTML5 Exported.
  • Hi everyone!

    I'm having some issues creating an array using JavaScript and modifying it with a function. For some reason the array keeps getting initialized again and again. I believe it is because JS code works as a tick, and gets called every frame, so all that JS code is being called every frame. But if so, then how should I do this?

    I'm loving Construct, mainly because of the portability and the ability to develop on the go with my phone. But I truly believe there is not enough information out there about Construct 3.

    I'm trying to create a simple blackjack game to start dipping my toes on this. And the amount of times I hit a wall due to lack of information has been impressive.

    Like... I get using JS is hard, and I{m not the best at it, but come on.

    Let me know if any of you know more about good learning resources.

  • Your script block is at the same level as the on start layout. So it's not part of that event. It gets called every ticket of the game.

    You need to either add it directly to on start layout or to a new sub event of the on start for it to only run once/

    If you want to call it from the event sheet as a function you should move it to a separate js file and import it into the events js file so it's available to call in the event sheet

  • See the manual section on Scripts in event sheets, in particular:

    Remember that the event sheet runs all events in top-to-bottom order every tick, so a script in a block at the top level of an event sheet is run every tick (as if it was an action in an Every tick event). Often it is more useful to use script blocks as sub-events, which only run if their parent event block was true.

  • Thanks everyone. I hadn't think about placing the script on the "on layout start" block, but it makes perfect sense.

    I was wondering, is there a way to create a global variable using JS?

  • If you place a variable defined in a JS script in the global scope it will be available in all other scripts.

    You can do that by writing something like this.

    	let myLocalVariable = 100;
    	
    	// This places the local variable above, into the global scope
    	globalThis.myGlobalVariable = myLocalVariable;
    

    You can try it out very quickly by running a script similar to that one on start of layout, then try adding a different script that runs, let's say, on a mouse click and check if the variable exists in globalThis.

  • If you place a variable defined in a JS script in the global scope it will be available in all other scripts.

    You can do that by writing something like this.

    > 	let myLocalVariable = 100;
    	
    	// This places the local variable above, into the global scope
    	globalThis.myGlobalVariable = myLocalVariable;
    

    You can try it out very quickly by running a script similar to that one on start of layout, then try adding a different script that runs, let's say, on a mouse click and check if the variable exists in globalThis.

    THANKS!! This helped me a lot.

    But now I have another issue.

    I want to access one of these global variables on an action, but I don't know how to.

    I thought about using globalThis.globalDealtCard or globalDealtCard but none seems to work.

    I'm sorry if I'm asking things already explained on the documentation, but this all seems really confusing to me still

  • I would recommend taking the Learn JavaScript in Construct tutorial series which covers many of these questions.

  • This is a bit off topic..

    I see you are creating a random deck of cards. This can easily be done by the Advanced Random and Permutation table functionality, without any JS. Just fyi

  • This is a bit off topic..

    I see you are creating a random deck of cards. This can easily be done by the Advanced Random and Permutation table functionality, without any JS. Just fyi

    That sounds great, could you share a tutorial or the documentation on how to use that?

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • I would recommend taking the Learn JavaScript in Construct tutorial series which covers many of these questions.

    Thanks, I will do it

  • > This is a bit off topic..

    > I see you are creating a random deck of cards. This can easily be done by the Advanced Random and Permutation table functionality, without any JS. Just fyi

    That sounds great, could you share a tutorial or the documentation on how to use that?

    I dont have any tutorial. But it is very easy to use.

    construct.net/en/make-games/manuals/construct-3/plugin-reference/advanced-random

  • > This is a bit off topic..

    > I see you are creating a random deck of cards. This can easily be done by the Advanced Random and Permutation table functionality, without any JS. Just fyi

    That sounds great, could you share a tutorial or the documentation on how to use that?

    The manual has everything well documented. Here's an general example dropbox.com/s/rq28y5pqhdh143b/Advanced_Random_Permutation_Example.c3p

    The permutation table size would be your deck size, and the value would be the ID of each card.

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