How to change "global variables" externally?

0 favourites
  • 5 posts
  • Hello,

    I am currently in the process of exploring the possibilities of Construct 2 for us. Figuring out if we can do with it what we need but i have hit a problem.

    The problem is i am trying to change one of the global variable's values through an external javascript.

    This is mainly because we want to create a game that more or less has an infinite amount of levels. So instead of letting construct 2 create levels we have decided to do this with PHP (?level=2 etc.) What i need now is a way to adjust the values of the global variables with information retrieved from the database.

    But i cannot seem to be able to figure out how to change the values of the global variables within the index.php page without using ajax/xml/json request within construct 2.

    Hopefully somebody is able to assist me with my issue. Or provide me with information if there are better/easier ways to achieve this.

    PS: Sorry for any english writing mistakes.

  • Sry for my bad English.

    I think you misunderstand something, in every programming language you need to make database request to get data out of your Database. In Javascript(This is what Construct generate) it?s not directly possible. You could only make an Ajax request to a PHP file which get you the data out of the database.

    So the logic is:

    1. Ajax Request on a Php file with some Parameters

    2. Php file makes the database request and some magic stuff with the data

    3. Ajax on complete, save the result in your global variables.

    You need to understand that between Javascript and Php must be any connection, mostly is ajax used.

    The other Problem is that Php is a server side language, it just makes something if you say "Hei Php go" and than it will execute your code one time, not more.

    No other tool like Gamesalad, Gamemaker and so on can work with Php files without Ajax, Json and so on. Not even native code!

  • Yes i fully understand that but my question is more:

    <?php

    $a = 'hello';

    ?>

    var a = <?php echo json_encode($a); ?>;

    But then apply that for the global variables in construct 2. So i could change the information in the variables based on the level dynamically. Simply by encoding the raw string data retrieved from the database.

    But i would like to prevent editing the runtime.js and rather be able to do this on the index.html(.php) page.

  • This is possible but no good idea because variables can be changed anytime you change your construct code.

    But if you think you will never update your game, you could do this with JQuery or PhpQuery.

    if you have a real example I could tell you how to do it easily.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Thank you for replying, i have found a way to get variables into the game now through a different route. The callJS plugin did it.

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