Manually add JS Code

0 favourites
  • 14 posts
From the Asset Store
5 levels with simple coding Source-code (.c3p) + HTML5 Exported
  • I need to manually input some JS code into the c2runtime.cs in order to comply with my customers API.

    What is the best way of doing this? The code is a simple function which will be needed when I send some AJAX calls.

    <img src="http://content.screencast.com/users/ArcadEd/folders/Jing/media/da5f5bf5-e2c0-43aa-b00e-319daca71545/2013-11-25_0858.png" border="0" />

    Thanks!

  • To make a function to call later use a "start of layout" with the "execute javascript" action in the browser object. You will have to rewrite the first line of your function like this so you can call it later:

    this.getHashFromUrl = function()

    Then you can call it later with another "execute javascript" action like this:

    this.getHashFromUrl()

    Be warned that it may not work with a minified export. Also in general it is probably preferred to just make a plugin.

  • Thank you. I haven't looked into making plugins, I might do that first.

    Appreciate the help.

  • You're welcome. On further thought "this" is the only thing that's not minified so it should work with a minified export. You only run into trouble if you try accessing thing that will be minified such as "this.runtime".

  • Gotcha! Thanks.

  • I strongly recommend using the Javascript SDK if you need to execute more than a single line of javascript.

    BTW looks like the Browser object expressions might be able to do what that javascript code does anyway!

  • Oh, good point Ashley, I hand't even thought of that.

  • R0J0hound

    Before I go jumping into the SDK, how do I take my code from above and do what you said? Do I replace all the double quotes with single quotes?

    OK, I think the answer is yes as I don't get any errors.

    Now the next issue is

    <img src="http://content.screencast.com/users/ArcadEd/folders/Jing/media/53594476-94e6-403f-8eaa-1997e0d911f5/2013-12-04_1451.png" border="0" />

    Do I use Ajax for this, or should I create functions in code to do these?

    I am really hoping this info will come in handy to others that plan on selling their games to publishers. Getting this all work out will make it much easier in the future. :).

  • Or if anyone else has a clue :)

  • K, I think the answer is yes as I don't get any errors.

    yep.

    o I use Ajax for this, or should I create functions in code to do these?

    Whichever you can use to get it to work. I'm not too familiar with ajax calls but it may be simpler to use the plugin if there are edge cases it handles. But again I have a knowledge gap about that that you can probably find alot of resources about with google.

  • ...Thinking out loud:

    If it is values you seek from the url, or any other thing that lead a user to your program, and your using a database with web resources on your own host, you could perhaps use a simple php landing page.

    Like, link to your game becomes: somewhere.com/game/game1/index.php (insteead of index.html)

    Have the PHP catch anything you need, from URL variable, to surfer details, to user login details etc, and stuff that in a database with a generated code by php. Then have PHP forward you to your index.html.

    When the game starts perform an ajax request to your server which again generates the code and fetches whatever details you had it store.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Thanks to both of you.

    I'm really trying figured out exactly what they are trying to do with the code and just reconstruct it using Construct.

    My issue right now is variables that are created in the Java Script code cannot be accessed by C2, or by other functions that I can see. Recreating what they want in C2 might be the only way.

    Or I need to go the SDK route, if that will even work with what they want.

  • If the client page generates some code in their webpage, cant you use a native javascript ajax post to a php database, and have cs2 retrieve that ?

    Major advantage of using the cs2 ajax is that it is most compatible.

  • Sure, but that is not how they have it setup :). So I am kind of stuck trying to get it to work with their code that they can't alter so it stays uniform per developer.

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