Returning an object from an expression

For developers using the Construct 2 Javascript SDK

Post » Thu Jan 07, 2016 5:56 am

Is it possible to return an object from an expression?

I tried with
Code: Select all
 ret.set_any(this.Player);
but it returns 0 (of course with ef_variadic_parameters).

If it's not possible to return objects (why?), what is the safest way to encode the object into json?

As an addendum, in case it's possible to return Objects, does the Local Storage plugin store full object or it does something to them?

Thanks
B
30
S
9
G
6
Posts: 525
Reputation: 8,329

Post » Thu Jan 07, 2016 7:53 am

C2's expressions only support numbers and strings so that's all you can return from a expression function. The variadic presumably mean either a number or a string.

Any JavaScript object can be converted to a json string. I don't recall offhand but It's done with a function built in to js. A Google search for converting an object to json in JavaScript should give you that solution.
B
91
S
31
G
103
Posts: 5,238
Reputation: 67,762

Post » Thu Jan 07, 2016 9:16 am

Thank you, I imagined this was the case.

JSON.stringify(Object)

seems the way to go..
B
30
S
9
G
6
Posts: 525
Reputation: 8,329

Post » Thu Jan 07, 2016 11:50 am

Do you mean an instance of an object from the C2 engine? Passing around UIDs is the typical way to do that.
Scirra Founder
B
387
S
230
G
88
Posts: 24,251
Reputation: 192,454

Post » Fri Jan 08, 2016 12:34 am

@Ashley I'm talking about an actual Javascript object

Code: Select all
var Object =
{
    'var': 'value',
    'function': function()
   {
   }
}
B
30
S
9
G
6
Posts: 525
Reputation: 8,329

Post » Fri Jan 08, 2016 12:51 am

Values passed in events only could be number or string, like @R0J0hound said.
B
108
S
26
G
260
Posts: 4,435
Reputation: 146,193

Post » Fri Jan 08, 2016 1:20 pm

You could use JSON, but then you can't use functions like in your example. I think it would be best to create an ID or handle to these complex objects, such as a number or string of random characters, and use the objects in terms of that. For example you could have a set of expressions to get each property from the ID.
Scirra Founder
B
387
S
230
G
88
Posts: 24,251
Reputation: 192,454


Return to Javascript SDK

Who is online

Users browsing this forum: No registered users and 0 guests