Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   NPC Scripting (https://forums.graalonline.com/forums/forumdisplay.php?f=8)
-   -   Sleep/Timer help (https://forums.graalonline.com/forums/showthread.php?t=134266470)

greggiles 05-17-2012 06:33 PM

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");
}

CujoDaMan 05-17-2012 10:23 PM

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.

fowlplay4 05-17-2012 10:48 PM

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""");



cbk1994 05-17-2012 11:13 PM

Quote:

Originally Posted by CujoDaMan (Post 1694942)
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.

Tigairius 05-17-2012 11:19 PM

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.

greggiles 05-18-2012 12:22 AM

@Tigairius, do you mind explaining a little.
1. What does "i" mean in the this.i?
2. also, what is pi?

Tigairius 05-18-2012 12:32 AM

Quote:

Originally Posted by greggiles (Post 1694958)
@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".

http://i.imgur.com/3bmHA.png

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

greggiles 05-18-2012 02:24 AM

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.?

cbk1994 05-18-2012 02:32 AM

Quote:

Originally Posted by greggiles (Post 1694974)
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.

greggiles 05-18-2012 02:43 AM

Is repetitivness bad?

Tigairius 05-18-2012 02:54 AM

Quote:

Originally Posted by greggiles (Post 1694974)
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);
  }



Tigairius 05-18-2012 04:02 AM

Quote:

Originally Posted by greggiles (Post 1694976)
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.

Devil_Lord2 05-18-2012 09:12 PM

Quote:

Originally Posted by Tigairius (Post 1694981)
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.

Tigairius 05-18-2012 10:01 PM

Quote:

Originally Posted by Devil_Lord2 (Post 1695011)
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.


All times are GMT +2. The time now is 05:18 AM.

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