Graal Forums  

Go Back   Graal Forums > Development Forums > NPC Scripting
FAQ Members List Calendar Today's Posts

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 05-17-2012, 06:33 PM
greggiles greggiles is offline
Registered User
greggiles's Avatar
Join Date: Sep 2007
Posts: 149
greggiles has a spectacular aura about
Sleep/Timer help

I am trying to have an image I have made, rotate slightly, then rotate back.
This is the only way I know how to do it. I am hoping someone can teach me the proper use of the 'sleep' function or perhaps advice me another way to write it. This does work, but I don't like how It's so long. and too many OnThis functions.

btw, the 'OnRot' stands for 'on rotation.'

Quote:
function onRot1()
{
findImg(3).rotation -= 0.05;
scheduleEvent(0.3, "onRot2");
}
function onRot2()
{
findImg(3).rotation -= 0.05;
scheduleEvent(0.3, "onRot3");
}
function onRot3()
{
findImg(3).rotation -= 0.05;
scheduleEvent(0.3, "onRot4");
}
function onRot4()
{
findImg(3).rotation -= 0.05;
scheduleEvent(0.3, "onRot5");
}
function onRot5()
{
findImg(3).rotation += 0.05;
scheduleEvent(0.03, "onRot6");
}
function onRot6()
{
findImg(3).rotation += 0.05;
scheduleEvent(0.03, "onRot7");
}
function onRot7()
{
findImg(3).rotation += 0.05;
scheduleEvent(0.03, "onRot8");
}
function onRot8()
{
findImg(3).rotation += 0.05;
scheduleEvent(0.03, "onRot1");
}
Reply With Quote
  #2  
Old 05-17-2012, 10:23 PM
CujoDaMan CujoDaMan is offline
Shaded Legend Management
CujoDaMan's Avatar
Join Date: Aug 2004
Location: Canada, Nova Scotia
Posts: 108
CujoDaMan is an unknown quantity at this point
Send a message via AIM to CujoDaMan Send a message via MSN to CujoDaMan
I'm not sure if it matters but i belive your missnig a value in the scheduleEvent.. I'm not sure, i haven't really worked with schedulevents but wouldn't it be something like

PHP Code:
scheduleevent(0.03"OnRot#"0); 
Someone can correct me if im wrong, i've only worked with it a few times.
__________________
Last Manager of Shaded Legend
Reply With Quote
  #3  
Old 05-17-2012, 10:48 PM
fowlplay4 fowlplay4 is offline
team canada
fowlplay4's Avatar
Join Date: Jul 2004
Location: Canada
Posts: 5,200
fowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond repute
Just set the spin value to 1 or something, it'll automatically rotate the image for you.

Your code is also very redundant.

PHP Code:
function onRotateImg() {
  
findimg(3).rotation += 0.05;
  
this.scheduleevent(0.3"RotateImg""");

__________________
Quote:
Reply With Quote
  #4  
Old 05-17-2012, 11:13 PM
cbk1994 cbk1994 is offline
the fake one
cbk1994's Avatar
Join Date: Mar 2003
Location: San Francisco
Posts: 10,718
cbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond repute
Send a message via AIM to cbk1994
Quote:
Originally Posted by CujoDaMan View Post
I'm not sure if it matters but i belive your missnig a value in the scheduleEvent.. I'm not sure, i haven't really worked with schedulevents but wouldn't it be something like

PHP Code:
scheduleevent(0.03"OnRot#"0); 
Someone can correct me if im wrong, i've only worked with it a few times.
A third parameter is only necessary on old (v5) clients. Serverside and on v6 it isn't.
__________________
Reply With Quote
  #5  
Old 05-17-2012, 11:19 PM
Tigairius Tigairius is offline
The Cat
Tigairius's Avatar
Join Date: Jan 2007
Location: Missouri, USA
Posts: 4,240
Tigairius has a brilliant futureTigairius has a brilliant futureTigairius has a brilliant futureTigairius has a brilliant futureTigairius has a brilliant futureTigairius has a brilliant futureTigairius has a brilliant futureTigairius has a brilliant future
I would probably do it kind of like this:
PHP Code:
//#CLIENTSIDE
function onCreated() {
  
this.trigger("RotateImage");
}

function 
onRotateImage() {
  
this.+= 0.05;
  
this.rotation sin(this.i) + pi 3;

  
this.scheduleEvent(0.05"RotateImage"NULL);

If my math is correct, this should rotate the image a bit, then rotate it back. Similar to what you had written originally.
__________________


“Shoot for the moon. Even if you miss, you'll land among the stars.”
Reply With Quote
  #6  
Old 05-18-2012, 12:22 AM
greggiles greggiles is offline
Registered User
greggiles's Avatar
Join Date: Sep 2007
Posts: 149
greggiles has a spectacular aura about
@Tigairius, do you mind explaining a little.
1. What does "i" mean in the this.i?
2. also, what is pi?
Reply With Quote
  #7  
Old 05-18-2012, 12:32 AM
Tigairius Tigairius is offline
The Cat
Tigairius's Avatar
Join Date: Jan 2007
Location: Missouri, USA
Posts: 4,240
Tigairius has a brilliant futureTigairius has a brilliant futureTigairius has a brilliant futureTigairius has a brilliant futureTigairius has a brilliant futureTigairius has a brilliant futureTigairius has a brilliant futureTigairius has a brilliant future
Quote:
Originally Posted by greggiles View Post
@Tigairius, do you mind explaining a little.
1. What does "i" mean in the this.i?
2. also, what is sin and pi?
this.i is just a random variable name, you could've used this.kljglkjdlgka if you wanted. I am using this.i just to increase the X value of the sine function (explained below).

First, pi is a number- it's a constant that is the ratio of a circle's circumference to its diameter. It is equal to approximately 3.14159 (although it's irrational so it actually goes on "forever").

Pi is used closely in the sine function ( aka: sin(x) ). Sine is a repeating wave across the x axis. So, as X increases, it will yield a number between -1 and 1 (as seen on the graph below).

Its wave-like behavior means that as you increase X, it will fluctuate between positive and negative, allowing for us to use it in that fashion to achieve the desired affect of "rotating forward, then back again".



For more information on sine, pi, cosine, or other trigonometric functions, check wikipedia.

http://en.wikipedia.org/wiki/Sine
http://en.wikipedia.org/wiki/Pi
__________________


“Shoot for the moon. Even if you miss, you'll land among the stars.”
Reply With Quote
  #8  
Old 05-18-2012, 02:24 AM
greggiles greggiles is offline
Registered User
greggiles's Avatar
Join Date: Sep 2007
Posts: 149
greggiles has a spectacular aura about
OOh ok. Well I have one more question, is there necessarily anything wrong with the way I wrote it? (using multiple scheduleEvents)

Or is it just not reccomended to write it like that.?
Reply With Quote
  #9  
Old 05-18-2012, 02:32 AM
cbk1994 cbk1994 is offline
the fake one
cbk1994's Avatar
Join Date: Mar 2003
Location: San Francisco
Posts: 10,718
cbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond repute
Send a message via AIM to cbk1994
Quote:
Originally Posted by greggiles View Post
OOh ok. Well I have one more question, is there necessarily anything wrong with the way I wrote it? (using multiple scheduleEvents)

Or is it just not reccomended to write it like that.?
It's ridiculously repetitive but I guess it would work.
__________________
Reply With Quote
  #10  
Old 05-18-2012, 02:43 AM
greggiles greggiles is offline
Registered User
greggiles's Avatar
Join Date: Sep 2007
Posts: 149
greggiles has a spectacular aura about
Is repetitivness bad?
Reply With Quote
  #11  
Old 05-18-2012, 02:54 AM
Tigairius Tigairius is offline
The Cat
Tigairius's Avatar
Join Date: Jan 2007
Location: Missouri, USA
Posts: 4,240
Tigairius has a brilliant futureTigairius has a brilliant futureTigairius has a brilliant futureTigairius has a brilliant futureTigairius has a brilliant futureTigairius has a brilliant futureTigairius has a brilliant futureTigairius has a brilliant future
Quote:
Originally Posted by greggiles View Post
OOh ok. Well I have one more question, is there necessarily anything wrong with the way I wrote it? (using multiple scheduleEvents)

Or is it just not reccomended to write it like that.?
Well, generally speaking, it's better to try to condense your code if you can. For example, if you have:
PHP Code:
function onCreated() {
this.chat 1;
sleep(1);
this.chat 2;
sleep(1);
this.chat 3;
sleep(1);
this.chat 4;
sleep(1);
this.chat 5;
sleep(1);
this.chat 6;
sleep(1);
this.chat 7;
sleep(1);
this.chat 8;

It's better to condense it like:
PHP Code:
function onCreated() {
  for (
temp.1temp.<= 8temp.++) {
    
this.chat temp.i;
    
sleep(1);
  }

__________________


“Shoot for the moon. Even if you miss, you'll land among the stars.”
Reply With Quote
  #12  
Old 05-18-2012, 04:02 AM
Tigairius Tigairius is offline
The Cat
Tigairius's Avatar
Join Date: Jan 2007
Location: Missouri, USA
Posts: 4,240
Tigairius has a brilliant futureTigairius has a brilliant futureTigairius has a brilliant futureTigairius has a brilliant futureTigairius has a brilliant futureTigairius has a brilliant futureTigairius has a brilliant futureTigairius has a brilliant future
Quote:
Originally Posted by greggiles View Post
Is repetitivness bad?
Also, I just wanted to clarify to you: It's bad if the way the code was WRITTEN is repetitive, it doesn't matter if the actual execution of the code is repetitive. Does that make sense?

He means it's repetitive because you have a lot of functions that do, essentially, the exact same thing, when you could've reduced it to one or two functions to complete the task.

One way to have done it without the sine function that is much closer to the way you originally wrote it, is something like this:
PHP Code:
function onRotateBackwards() {
  
findImg(3).rotation -= 0.05;
  
scheduleEvent(0.3this.counter == "RotateForwards" "RotateBackwards");
  
this.counter ++;
}

function 
onRotateForwards() {
  
findImg(3).rotation += 0.05;
  
scheduleEvent(0.3this.counter == ?  "RotateBackwards" "RotateForwards");
  
this.counter ++;

You can see it accomplishes the same task, using the same exact way, but it's just been condensed into two event functions.

You could even go a step further, and write it somehow like this:
PHP Code:
function onRotateImage() {
  
temp.direction this.counter 4;
  
findImg(3).rotation temp.direction ? (findImg(3).rotation 0.05) : (findImg(3).rotation 0.05);
  
this.counter ++;
  
scheduleEvent(0.3"RotateImage");

I think you can agree that it's much nicer and easier to read than having 8 different functions to complete the single task.
__________________


“Shoot for the moon. Even if you miss, you'll land among the stars.”

Last edited by Tigairius; 05-18-2012 at 04:16 AM..
Reply With Quote
  #13  
Old 05-18-2012, 09:12 PM
Devil_Lord2 Devil_Lord2 is offline
David K?
Devil_Lord2's Avatar
Join Date: Apr 2011
Location: PA, MD.
Posts: 643
Devil_Lord2 can only hope to improve
Quote:
Originally Posted by Tigairius View Post
PHP Code:
function onRotateImage() {
  
temp.direction this.counter 4;
  
findImg(3).rotation temp.direction ? (findImg(3).rotation 0.05) : (findImg(3).rotation 0.05);
  
this.counter ++;
  
scheduleEvent(0.3"RotateImage");

I think you can agree that it's much nicer and easier to read than having 8 different functions to complete the single task.
I'm only posting on this thread because I find it useful, but I'd like to point out, you should not assume someone thinks it is easier to read something as confusing as that.

I get up to the question mark which then confuses me...
I don't know how much he knows about programming or math,
but some things that may be common sense to some programmers
may not be as such to those just getting into the scene.

That and we all have certain skills inside the language we are good at
and others we've never bothered to touch.

I think you mean to say easier to read all at once,
rather than easier to comprehend? :x

It takes the system more time to read 20 lines when it can read 4...
Anyway, positive reputing your post explaining sign to him, I believe
most people would not take the time to do something like that.
__________________

Digital Media Artist - David K? </3 (UnLoved)
www.davidkrout.com
www.twitch.com/DavidKkz




Last edited by Devil_Lord2; 05-18-2012 at 09:12 PM.. Reason: When I can give it.
Reply With Quote
  #14  
Old 05-18-2012, 10:01 PM
Tigairius Tigairius is offline
The Cat
Tigairius's Avatar
Join Date: Jan 2007
Location: Missouri, USA
Posts: 4,240
Tigairius has a brilliant futureTigairius has a brilliant futureTigairius has a brilliant futureTigairius has a brilliant futureTigairius has a brilliant futureTigairius has a brilliant futureTigairius has a brilliant futureTigairius has a brilliant future
Quote:
Originally Posted by Devil_Lord2 View Post
I get up to the question mark which then confuses me...
The question mark is called the ternary operator. It is basically like an if-statement.
Here it is written in English:
RESULT = BOOLEAN ? DO THIS : OTHERWISE DO THIS;

A boolean is simply a statement or value that is either true or false.

Now I'll show you an example of converting an if-else statement into ternary.

PHP Code:
if (this.counter >= 4) {
  
findImg(3).rotation 3;
}else {
  
findImg(3).rotation = -3;

Could essentially be written as the following:
findImg(3).rotation = this.counter >= 4 ? 3 : -3;

The red text is the BOOLEAN. If it's true, it will return/execute whatever is in blue, if it's false, it'll return/execute purple.
__________________


“Shoot for the moon. Even if you miss, you'll land among the stars.”
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 10:33 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.