Help with Function Parameters.

For questions about using Classic.

Post » Sun Oct 30, 2011 7:18 am

In my .cap, I have a lot of objects whose properties I need to store in a array, something like Set Array(1) to ObjectA.X.
Right now for each object, I have a long list of these and the only difference is the object they reference.

What I'm trying to do, without luck, is to call a single function which picks these parameters and to change the object called using function parameters. Something like:

Function: Add Parameter "ObjectA"
Call Function "Properties"

On Function "Properties"
Set Array(1) Function.Param(1).X
Set Array(2) Function.Param(1).Y


So that Function.Param(1).X becomes ObjectA.X

I'm guessing there a problem with the syntax, but I can't figure it out.
Help would be most appreciated.
B
8
S
2
G
2
Posts: 85
Reputation: 1,218

Post » Sun Oct 30, 2011 1:11 pm

A function can be set to "ignore" or "remember" picked objects.

When working with just one object type but many instances, just pick the proper instances and call the function set to "and remember picked objects"

example:
+ Function: On function "props"
-> myText: Set text to myObject.X

+ System: For each myObject
-> Function: Call function "props" (and Remember picked objects)

When working with different objects but the same object type (e.g. several different sprites) put them in a family.

example (sprites put into family Blue):
+ Function: On function "props"
-> myText: Set text to Blue.X

+ System: For each Blue
-> Function: Call function "props" (and Remember picked objects)
Image
B
23
S
8
G
10
Posts: 1,820
Reputation: 8,242

Post » Mon Oct 31, 2011 6:47 pm

Thanks for the reply.

Yes, I'm aware of how functions work. What I have are multiple objects with multiple instances - that's why I thought of this kind of substitution. Using families does not help - in fact, it crashes Construct randomly...

In general, I find families quite finnicky for more "complex" operations - I mainly use them to assign private vars to many objects...
B
8
S
2
G
2
Posts: 85
Reputation: 1,218

Post » Mon Oct 31, 2011 7:58 pm

If not using families you'll have to do it like this for each object type:

On Function "Properties"
System Compare: Function.Param(1) equal to "ObjectA"
----Set Array(1) ObjectA.X
----Set Array(2) ObjectA.Y
System Compare: Function.Param(1) equal to "ObjectB"
----Set Array(1) ObjectB.X
----Set Array(2) ObjectB.Y

But bear in mind that only the first instance of of a object type will be used. To get around that pass a second function parameter with the UID of the particular instance you want saved, and pick it with the "Pick by UID" condition.
B
79
S
24
G
52
Posts: 4,725
Reputation: 39,713

Post » Tue Nov 01, 2011 6:25 am

A little off-topic advice: Be careful not to have more than 15 (20?) function calls per tick/second, my game got a bit too heavy with them trying to keep code modular/seperate, and I ended up causing crashes every 5 minutes in my game.Jayjay2011-11-01 06:25:38
"Construct 4 lets YOU make advanced games! (maybe)" Construct Classic - Examples Kit
B
86
S
28
G
13
Posts: 2,092
Reputation: 15,009

Post » Tue Nov 01, 2011 8:35 am

Jayjay > sorry mate but I have to disagree with you.

I made a maze generator 2 month ago and it's a recursive algorithm.
I have the same function calling itself over and over.
Yes it's buggy, but not because of so many function call, but because I call it inside a loop and CC seems to have hard time keeping track of them and doesn't end them properly.
So I had to name the loops with a unique name. But even so, there can rarely be some random crash.

Yann2011-11-01 08:35:44
B
60
S
22
G
14
Posts: 1,479
Reputation: 16,346

Post » Tue Nov 01, 2011 8:51 am

[QUOTE=Funky Koval]
Function: Add Parameter "ObjectA"
Call Function "Properties"[/QUOTE]

just a nice tip for using function objects, this method will always remember picked objects I believe, but it's much simpler and more readable

[quote]
this here:
Function: Add Parameter "somestring"
Function: Add Parameter 1
Function: Add Parameter 45
Function: Add Parameter objecta.x
Call Function "Properties"[/QUOTE]

is equivalent to just doing this:

[quote]Call Function .Properties("something",1,45,objecta.x)[/QUOTE]
Spriter Dev
B
87
S
21
G
12
Posts: 3,240
Reputation: 16,461

Post » Thu Nov 03, 2011 4:54 pm

R0J0hound
Yup, understood, but this is similar to what I have and am trying to avoid - multiple SetArray sequences with the only difference being the object referenced.

The UID option might work though, iterating through Family members, but using the UID as the object identifier to pick up the data.

Thanks.

Jayjay and Yann
Noted, thanks. I've ran into a similar issue - i needed to calculate the point of intersection of a line and an elipse centered on each of my object instances...
No crashes, but it halved my framerate when dealing with only a hundred or so objects.

lucid
I didn't know about this shortcut - thanks.
B
8
S
2
G
2
Posts: 85
Reputation: 1,218

Post » Thu Nov 03, 2011 8:47 pm

[Quote]Call function -> .properties(param,param,...,param)[/QUOTE]

keep in mind that this generates one extra function call, namely to the function: 0 (since the return value is 0), which actually can't be accessed since "call function" only takes a string as the parameter. To access it you must cast the function call to a string with str() like so: Call function -> str(.properties(param,param))

Then when you make a condition [ On function: "0" ] It will perform the events because it calls the function called "0".

Keep in mind that lucid's method is around 3x slower. Here's a benchmark I had made.

B
25
S
3
G
6
Posts: 1,197
Reputation: 5,620

Post » Fri Nov 04, 2011 1:03 am

looks like davio is right, which was kind annoying, so I made a new function object. seems to be about 1.5 times the speed using the single expression method than the regular function object runs with the separate action method

http://dl.dropbox.com/u/1013446/sharinplugs/func/plugins.rar
http://dl.dropbox.com/u/1013446/sharinplugs/func/functest.cap

this one will only accept the format:
CallFunction :     .myfunctionname(param0,param1)

there is no addparam, and callfunction does not pay attention unless you use that .functionname(params here) method
or just .functionname if you have no parameters

you get your params in one of two ways
func.p(index)
or just
func(index)

because it's easier to type p or nothing than param
in this object parameters are 0 based, which means, the first parameter you put in is 0, the second is 1, etc

this does not have the "on any function" or 'alias' features, but I can add them if anyone really uses them, and wants to use this faster function caller

I would have just optimized the regular function object, but that code isn't on the svn

let me know if anyone tries it, or if they encounter any bugslucid2011-11-04 01:35:55
Spriter Dev
B
87
S
21
G
12
Posts: 3,240
Reputation: 16,461

Next

Return to Help & Support using Construct Classic

Who is online

Users browsing this forum: No registered users and 2 guests