Digital Workshop

Welcome to the Digital Workshop Message Boards
It is currently December 23rd, 2024, 12:15 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 12 posts ] 
Author Message
 Post subject: Function Scripting
PostPosted: May 25th, 2008, 4:29 am 
Offline
Godlike
Godlike

Joined: November 11th, 2004, 1:18 pm
Posts: 1213
Location: New York
Opus: Opus Pro 9.75
OS: Windows 10 Pro 64 bit
System: Core i7, 16G RAM, Nvidia 640GT (desktop), plus Windows 10 and Android tablets
Hi

I am trying to save on adding the same script to several object's and wondered if there is a way to create a function for this. The problem is that each of the actions that would go into the function is prefaced by an "if" statement.

For example:

Code:
if (IsKeyPressed("Right"))
{
this.MoveX(1)
}

if (IsKeyPressed("Left"))
{
this.MoveX(-1)
}

if (IsKeyPressed("Up"))
{
this.MoveY(-1)

}

if (IsKeyPressed ("Down"))
{
this.MoveY(1)

}



From doing some reading on JS, I don't think that functions can contain conditional statements, but being a novice at this syntax, possibly, I am incorrect. Maybe there is a way to create a function containing if statements. :?:

Any suggestions would be appreciated.

Kind Regards,

_________________
Stephen


Top
 Profile  
 
 Post subject:
PostPosted: May 25th, 2008, 5:38 am 
Offline

Joined: November 11th, 2004, 4:05 am
Posts: 636
Location: Christchurch, NZ
Hi Stephen,
You can put anything in a function, except another function, and you put functions in a script object, not a script action attached to a trigger.

Paul


Top
 Profile  
 
 Post subject:
PostPosted: May 25th, 2008, 6:40 am 
Offline
Godlike
Godlike

Joined: November 12th, 2005, 1:56 am
Posts: 1474
Location: SFBay Area
Opus: OpusPro v9.0x, & Evol.
OS: Vista32
System: Core 2 duo 2Ghz, RAM 3GB, Nvidia Go 7700 - laptop
Paul wrote
Quote:
You can put anything in a function, except another function
I think you are saying it is best to not try to 'define' a function there. Certainly 'calls' to other functions work from within functions.

Steve.
I may jumble this up a bit... but
Code:
function doTHEkeypressed ( whichkey, whichobject )
{
if (whichkey == "Right")   {  whichobject.MoveX(1)    }
if (whichkey == "Left")    {  whichobject.MoveX(-1)   }
if (whichkey == "Up")      {  whichobject.MoveY(-1)   }
if (whichkey == "Down")    {  whichobject.MoveY(1)    }
// and some 'else' error message
}


//and somewhere else make the calls to function
Code:
var currDirection = "Left" ;
doTHEkeypressed ( currDirection , this )

_________________
_good things come to those who wait(0)_


Top
 Profile  
 
 Post subject:
PostPosted: May 25th, 2008, 12:49 pm 
Offline
Godlike
Godlike

Joined: November 11th, 2004, 1:18 pm
Posts: 1213
Location: New York
Opus: Opus Pro 9.75
OS: Windows 10 Pro 64 bit
System: Core i7, 16G RAM, Nvidia 640GT (desktop), plus Windows 10 and Android tablets
Thanks Paul and Larry

I had tried this and similar code last evening in a script object, and for whatever reason, kept getting "errors."

I'll try it out, see if I can get it to work.

It looks like I will need to write a different set of instructions in the function. There are multiple objects which will have the functions added using mouse over triggers.

I am trying to get the following code into a function called "keypress"

Code:
if (IsKeyPressed("Right"))
{
this.MoveX(1)
}

if (IsKeyPressed("Left"))
{
this.MoveX(-1)
}

if (IsKeyPressed("Up"))
{
this.MoveY(-1)

}



if (IsKeyPressed ("Down"))
{
this.MoveY(1)

}



if (IsKeyPressed("1"))
{
this.Rotate(1)

}



if (IsKeyPressed ("2"))
{
this.Rotate(-1)

}

if (IsKeyPressed("3"))
{
this.ScaleH(.01)

}

if (IsKeyPressed("4"))
{
this.ScaleH(-.01)

}


If I simply add

Code:

function keypress()

{
if (IsKeyPressed("Right"))
{
this.MoveX(1)
}

if (IsKeyPressed("Left"))
{
this.MoveX(-1)
}

if (IsKeyPressed("Up"))
{
this.MoveY(-1)

}



if (IsKeyPressed ("Down"))
{
this.MoveY(1)

}



if (IsKeyPressed("1"))
{
this.Rotate(1)

}



if (IsKeyPressed ("2"))
{
this.Rotate(-1)

}

if (IsKeyPressed("3"))
{
this.ScaleH(.01)

}

if (IsKeyPressed("4"))
{
this.ScaleH(-.01)

}
}


I get error messages. No matter how I add or subtract curly brackets (like that last double {), I still get errors.

So, I have a way to go. Any additional suggestions will be appreciated.

Kind Regards,

_________________
Stephen


Top
 Profile  
 
 Post subject:
PostPosted: May 25th, 2008, 3:06 pm 
Offline
Godlike
Godlike

Joined: November 12th, 2005, 1:56 am
Posts: 1474
Location: SFBay Area
Opus: OpusPro v9.0x, & Evol.
OS: Vista32
System: Core 2 duo 2Ghz, RAM 3GB, Nvidia Go 7700 - laptop
Stephen,

Are you using "RegisterEventHandler" somewhere?

If not, then the following may help point the way.
Hold down the Right Arrow key before you Mouse Over. I think you will see that your code works then. As a single triggered event, "MO", the testing condition must be 'true' at the time of the trigger.

So I think that means you will need to set up a Loop of some sort. For example, use 'while' instead of 'if'. Or, a 'for loop' with a 'wait(0)' or 'wait (0.005)' as a ticker. Would that make it a resource hog??

I suspect you could change the trigger from MO to 'Key Pressed' (using Std trigger/actions). I have not used enough to recommend. Copy/Paste might help for applying to many objects. I think your Cloned objects will inherit all these.

good luck.

_________________
_good things come to those who wait(0)_


Top
 Profile  
 
 Post subject:
PostPosted: May 25th, 2008, 3:31 pm 
Offline
Godlike
Godlike

Joined: November 11th, 2004, 1:18 pm
Posts: 1213
Location: New York
Opus: Opus Pro 9.75
OS: Windows 10 Pro 64 bit
System: Core i7, 16G RAM, Nvidia 640GT (desktop), plus Windows 10 and Android tablets
Hi

Thanks for your reply. No event handlers.

I was finally able to create the function within a script object (needed to add one more curly bracket).

However, using "this" as in "this.MoveX(-1)" doesn't seem to work: generates an error message when the script is run using the function call.

Since there are about 50 objects in which I was hoping to use a function call, and these are all routinely cloned in runtime, "this" is the only way to "shorthand" the scripting. And, it appears "this" won't work in this function call. If each object had to be specified, then the function call couldn't be used as a time and script saver.

Probably will leave well enough alone and just add the 50 scripts to the 50 objects. That method, which I've tested on a few objects and their clones, works.

Kind Regards,

_________________
Stephen


Top
 Profile  
 
 Post subject:
PostPosted: May 25th, 2008, 3:41 pm 
Offline
Godlike
Godlike

Joined: November 12th, 2005, 1:56 am
Posts: 1474
Location: SFBay Area
Opus: OpusPro v9.0x, & Evol.
OS: Vista32
System: Core 2 duo 2Ghz, RAM 3GB, Nvidia Go 7700 - laptop
Quote:
However, using "this" as in "this.MoveX(-1)" doesn't seem to work: generates an error message when the script is run using the function call.

It may work very easily... when you pass the object as a Parameter to the function.

see viewtopic.php?t=3222&postdays=0&postorder=asc&start=45
where Mackavi wrote:
Quote:
Just a smaller pointer to keep things simpler - if working on an actual object passed to a function you can pass the object NOT the name IE:

theRotateFunction(this) rather than the RotateFunction(this.GetName())
------- --------- ---------
function theRotateFunction(thingy)
{
thingy.Rotate(45)
}
so, put the 'this' w/o quotes in the CALL to the function....
and the 'receiving' parameter is named anything, such as 'thingy' above.

_________________
_good things come to those who wait(0)_


Top
 Profile  
 
 Post subject:
PostPosted: May 25th, 2008, 4:44 pm 
Offline
Godlike
Godlike

Joined: November 11th, 2004, 1:18 pm
Posts: 1213
Location: New York
Opus: Opus Pro 9.75
OS: Windows 10 Pro 64 bit
System: Core i7, 16G RAM, Nvidia 640GT (desktop), plus Windows 10 and Android tablets
Hi Larry

Thanks for your help.

Followed these suggestions, and still can't get the function call to work: script error message.

If I try to create the function in the script object as:

function keypress()
{
}
the script editor shows "OK"

Then I call the function in the object's script box:

keypress(this)

and I get a script editor error when I preview, plus the function does not run.

If I write the function call as

keypress("this")

doesn't help.

If I change the function in the script object to

function keypress (this)

then I get script object errors.

As previously mentioned, I need to use this for actions like MoveX, since there are about 50 objects for which I would like to use the function call (plus their cloned "children").

Still stuck.

Kind Regards,

_________________
Stephen


Top
 Profile  
 
 Post subject:
PostPosted: May 25th, 2008, 5:36 pm 
Offline
Godlike
Godlike
User avatar

Joined: March 21st, 2007, 10:44 am
Posts: 3188
Location: UK
Opus: Evolution
Hi Stephen,

in terms of 'correct' scripting - the first example is correct - but I'm not sure that it will acheive what you need to do as this isn't clear - but first:

From your examples:

1. keypress (this) - will call the function named key press and pass the object calling the function. Hence, if you have a frame called 'F1' and you use this - it passes the object - NOT THE NAME!

2. keypress ("this") is literal - it passes the STRING 'this' which means no more to the program than 'dog' 'cat' or 'fish' - it is simply another word and probably not much help

3. function keypress (this) - won't really work, because this is a reserved word, but had you picked something else like function keypress (t), then this initialise the function and allows it to receive a value stored in 't'.

-------------------------------------------------------------------------------

On to your problem. What are you trying to do? It seems that you want to press a key and have an action occur for one of several on-screen objects - but I don't see how you are identifying which object???

Mack

_________________
When you have explored all avenues of possibilities, what ever remains, how ever improbable, must be the answer.

Interactive Solutions for Business & Education
Learn Anywhere. Learn Anytime.

www.interaktiv.co.uk
+44 (0) 1395 548057


Top
 Profile Visit website  
 
 Post subject:
PostPosted: May 25th, 2008, 6:59 pm 
Offline
Godlike
Godlike

Joined: November 11th, 2004, 1:18 pm
Posts: 1213
Location: New York
Opus: Opus Pro 9.75
OS: Windows 10 Pro 64 bit
System: Core i7, 16G RAM, Nvidia 640GT (desktop), plus Windows 10 and Android tablets
Hi Mack

The pedigree tool has many shapes and lines, all of which are cloned, and the clones dragged and dropped on the canvas. Dragging/dropping a shape or line precisely can be a frustrating experience, so I thought it may be helpful to give the user (and me, when creating a demo) the option of nudging, once or repeatedly, these clones by 1 pixel up, down, left and right, plus 1 degree of + or - rotation or 1% scaling horizontal or vertical. Since this would be infrequently used, and since most triggers are already used for bigger animations (scale, rotate, etc.), I thought that combining mouse over with specific keys pressed would work.

If I create a script for mouse over with if keypressed ("left") then this.MoveX (-1) and a few others with different keys, add it to a script item within the parent object, and use "this", then all clones perfom the action upon mouse over and the correct key pressed. I've tested it, and it works fine for all clones, using "this".

With about 50 parent objects (shapes, lines, etc.), I would either have to add the same mouse over trigger and script to each or, possibly I could create a function in a page-level script object, then simply add the function call to the mouse over trigger within each parent object.

Since the pedigree drawing page is getting a little slow because of all of the many objects and actions and scripting, I thought a single script object with a function plus a simple function call in each parent object would be conserving and also a little easier to accomplish.

Unfortunately, that challenge has proven elusive. Can't seem to get a function to work no matter what I try. Then again, functions is not an area of skill for me.

Any suggestions appreciated.

Kind Regards,

_________________
Stephen


Top
 Profile  
 
 Post subject:
PostPosted: May 25th, 2008, 9:17 pm 
Offline
Godlike
Godlike
User avatar

Joined: March 21st, 2007, 10:44 am
Posts: 3188
Location: UK
Opus: Evolution
Hi Stephen,

Pretty much what I thought. I can hazard a guess at the problem - in that you're trying to use "this" (without quotes) inside the function. Whilst there are reason for using this inside a function - you don't want to in this case. The function call from you clone will pass this to the local variable of your function and the variable should be used in place of 'this'.

IE

In clone script action: keyPress(this)

In script object:

function keyPress(t) //t is local variable
{
t.moveX(-1) //Note the use of t NOT this.
}

I'll email you an example,

Mack,

_________________
When you have explored all avenues of possibilities, what ever remains, how ever improbable, must be the answer.

Interactive Solutions for Business & Education
Learn Anywhere. Learn Anytime.

www.interaktiv.co.uk
+44 (0) 1395 548057


Top
 Profile Visit website  
 
 Post subject:
PostPosted: May 25th, 2008, 9:33 pm 
Offline
Godlike
Godlike

Joined: November 11th, 2004, 1:18 pm
Posts: 1213
Location: New York
Opus: Opus Pro 9.75
OS: Windows 10 Pro 64 bit
System: Core i7, 16G RAM, Nvidia 640GT (desktop), plus Windows 10 and Android tablets
Hi Mack

Thanks. I'll look forward to the email example.

I'm hoping the function will work for (1) all cloned children of a specific object, and (2) can be re-used in every parent object for their cloned children.

If the "t" would have to be changed ("m", "k", etc.) for each new parent object (many symbols and lines), then I'm not sure that it isn't easier to use the original means to accomplish this, without a function: by placing the 8 "if" items/actions (this. MoveX -, this.MoveX +, this.MoveY -, this.MoveY +, this.Rotate -, this.Rotate +, this.Scale H -, this. ScaleH +) shown in the code above into every parent object using a mouse over trigger.

Hopefully, the example will clarify.

Kind Regards,

_________________
Stephen


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 12 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: Bing [Bot] and 22 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group