Running Hype code returned from PHP


#1

I’m running an AJAX call which, using PHP, returns some hype code to change the scene. How do I execute the returned hype code to change the scene?


(Mark Hunte) #2

Can you post an example…


#3

What code is it? Is it one of the functions because you can manipulate Hype scenes and documents outside of Hype using the global syntax.

D


#4

I hope this makes sense. And important to note that the returned scene change code will change which is why I can’t run it directly in the page code. So the code that I’m getting back from PHP is:

{"Error":"Yes","eval":"hypeDocument.showSceneNamed('scene2', hypeDocument.kSceneTransitionPushRightToLeft, 1.1"}

I’m getting this returned from a standard AJAX call:

$.ajax({

url: './gta_getdata.php',
type: 'POST',
dataType: 'JSON',
data: {
      d1: data1,
      d2: data2,
      d3: data3
},

success: function (res) {
	$.each(res,function(key,value){

			//check for error
			if (res[key].Error == "Yes"){
				alert(res[key].eval);
                            //EXECUTE RETURNED SCENE CHANGE CODE HERE

			}

#5

Hi @3saul,

Outside of the Hype document nothing is going to understand “hypeDocument.showSceneNamed …”

Your probably better off using the HYPE.documents[’…document name … '].showSceneNamed … like below.

Not 100% sure how you would implement it in your situation but I think this would be a start.

Here is the post.

D


#6

Just to clarify Hype is calling the AjAX function and receiving the response so I would have thought it would be relatively straight forward??


#7

The ajax call is being made from a function or the Head html in Hype?

D


(Mark Hunte) #8

If I do understand you right and you are getting the return in a hype function.

Then some thing like this may work. But you would need to format the string as I have here.
Also using eval may not be safe and I would advise you check the security risks first.

 var jasonReturn =   "{\"Error\":\"Yes\",\"eval\":\"hypeDocument.showSceneNamed('scene2', hypeDocument.kSceneTransitionPushRightToLeft, 1.1)\"}" 
 var text = '{ "scenes" : [' + jasonReturn +  ']}';
 var obj = JSON.parse(text);
  eval (obj.scenes[0].eval);

#9

Perhaps using JSON.parse might be an option. Rather than eval.

D


#10

Mark you are correct, I am getting the return in a hype function


(Mark Hunte) #11

This is all a little bit new to me. I thought that JSON.parse() would run the code too. But it does not.?


#12

OK. The information being returned is JSON so surely

var text = '{ "Error":"Yes","eval":"hypeDocument.showSceneNamed(\'scene2\', hypeDocument.kSceneTransitionPushRightToLeft, 1.1)"}';
var obj = JSON.parse(text);

It’s new to me also. So I’m no expert. I’m just thinking out loud. :smile:

D


(Mark Hunte) #13

That what I first tried. But it does not run the code. Even do it returns an object…

Also I get an error if I try and parse the return with out putting it in an array like object .i.e { “scenes” : [’…

Which had me thinking I could just do:

 var realReturn =  {"Error":"Yes","eval":"hypeDocument.showSceneNamed('scene2', hypeDocument.kSceneTransitionPushRightToLeft, 1.1)"}

  theScenes.scenes = [realReturn];
  
   console.log(theScenes);
 var obj = JSON.parse(theScenes);

But I get the error:
[Log] Error in untitledFunction: SyntaxError: JSON Parse error: Unexpected identifier “object” (HYPE-466.thin.min.js, line 14)

Even though the object looks ok.


#14

:frowning:

I’m stumped

D


(Mark Hunte) #15

Just update my last post with some more info.


#16

what about trying

theScenes.scenes = '[{"Error":"Yes","eval":"hypeDocument.showSceneNamed('scene2', hypeDocument.kSceneTransitionPushRightToLeft, 1.1)"}]';

D


(Mark Hunte) #17

Nope.

That just sets the array to

[Log] Object
scenes: "[{"Error":"Yes","eval":"hypeDocument.showSceneNamed('scene2', hypeDocument.kSceneTransitionPushRightToLeft, 1.1)"}]"
__proto__: Object

The working one looks like:

[Log] Object
scenes: Array[1]
0: Object
Error: "Yes"
eval: "hypeDocument.showSceneNamed('scene2', hypeDocument.kSceneTransitionPushRightToLeft, 1.1)"
__proto__: Object
length: 1
__proto__: Array[0]
__proto__: Object

The one that looks like it should work but does not :smile:

[Log] Object
scenes: Array[1]
0: Object
Error: "Yes"
eval: "hypeDocument.showSceneNamed('scene2', hypeDocument.kSceneTransitionPushRightToLeft, 1.1)"
__proto__: Object
length: 1
__proto__: Array[0]
__proto__: Object

#18

Does the single quote make any difference. It’s just I read somewhere in another forum post that Hype accepts it this way.

D


(Mark Hunte) #19

It may do I was just looking at that


(Mark Hunte) #20

So the problem was the ‘’ surrounding the scene name…

This works also…

 var scene2 = "'scene2'"
	var json = '{"Error":"Yes","eval":"hypeDocument.showSceneNamed(' + scene2 + ', hypeDocument.kSceneTransitionPushRightToLeft, 1.1)"}';
    obj = JSON.parse(json);
  	eval (obj.eval);