Graal Forums  

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

 
 
Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1  
Old 11-24-2010, 09:34 PM
DrakilorP2P DrakilorP2P is offline
Registered User
DrakilorP2P's Avatar
Join Date: Apr 2006
Posts: 755
DrakilorP2P is just really niceDrakilorP2P is just really nice
Quick Guide to Weather Effects

This script goes into a weapon/GUI-script.

PHP Code:
//#CLIENTSIDE
function onCreated()
{
  if (!
weathereffectsenabled) return; // Important
  
  
defineEmitters();
  
  
onTimeout();
}

function 
onTimeout()
{
  
// Code goes here.
  
  
setTimer(1);
}

function 
defineEmitters()
{
  
this.weather_rain findImg(200);
  
  
with (this.weather_rain) {
    
layer 5;
    
0;
    
0;
    
    
with (emitter) {
      
delaymin 0.05;
      
delaymax 0.05;
      
nrofparticles 1;
      
emitautomatically false;
      
autorotation true;
      
emissionoffset = {-2, -20};

      
with (particle) {
        
image "g4_particle_spark.png";
        
zoom 1;
        
red 0;
        
green 0.25;
        
blue 1;
        
alpha 0.75;
        
mode 0;
        
lifetime 5;
        
speed 1000;
      }

      
removemodifiers(); // Make sure there aren't any duplicate modifiers.
      
addlocalmodifier("once"00"angle""replace"degtorad(-80), degtorad(-60));
      
addlocalmodifier("once"00"x""replace"0screenwidth);
    }
  }
}

function 
GraalControl.onResize()
{
  
// This code is here because the rain effect depends on screenwidth.
  
defineEmitters();

The reason the 'weathereffectsenabled' line is important is that it exits the script if the player has weather effects turned off in the F3 menu. It's not fun when you have a slow computer and you turn off effects, only for the playerworld to display them anyway.

There's some code that creates a rain effect. Its specifics are not too important, but what is important is that we now have a particle effect floating around in memory, and that we can access it via the variable 'this.weather_rain'.

The following code goes into the serverside part of the script. It creates a variable called 'serverr.weather', and alternates its value between "rain" and "" every five seconds. We'll get back to this later.

PHP Code:
function onCreated()
{
  
onTimeout();
}

function 
onTimeout()
{
  if (
serverr.weather == "rain"serverr.weather "";
  else 
serverr.weather "rain";
  
  
setTimer(5);

'serverr.' variables are great because you can read them clientside, and it'll have the same value for every player. This means that if you have two computers running Graal, it will rain on both screens at the same time.
We'll have to change the script to make this happen first though:

PHP Code:
function onTimeout()
{
  
updateWeather();
  
  
setTimer(1);
}

function 
updateWeather()
{
  if (
serverr.weather == "rain"this.weather_rain.emitter.emitautomatically true;
  else 
this.weather_rain.emitter.emitautomatically false;

We're now clientside again, but serverr.weather still works.

You might remember how I said earlier that the particle effect was floating in memory. At no point am I actually removing it; I'm just turning it off when it's not raining. And that's what the 'emitautomatically' variable does; it lets you turn on and off particle effects smoothly without suddenly hiding particles that are in mid-flight.

You probably don't want to see rain indoors. There's no standard way to tell indoors from outdoors, but a good approximate is to look at if the player is on a gmap:

PHP Code:
function onPlayerEnters()
{
  
updateWeather();
  
  
temp.inside = (player.gmap.name == "");
  if (
temp.inside) {
    
this.weather_rain.emitter.removeparticles();
  }
}

function 
updateWeather()
{
  
temp.outside = (player.gmap.name != ""); 
  
  if (
temp.outside && serverr.weather == "rain"this.weather_rain.emitter.emitautomatically true;
  else 
this.weather_rain.emitter.emitautomatically false;
  
  if (
temp.outside && serverr.weather == "snow"this.weather_snow.emitter.emitautomatically true;
  else 
this.weather_snow.emitter.emitautomatically false;

I'm using 'removeparticles' in order to abruptly end the effect when the player walks inside. If it stops raining while outside it will still be a smooth transition though.

Here's the final script. It's all the same as above, except there's now snow, and I added an event function called 'onWeatherChanged', so that the script can show a flavour text when the snowing starts.

PHP Code:
function onCreated()
{
  
onTimeout();
}

function 
onTimeout()
{
  if (
serverr.weather == "rain"serverr.weather "snow";
  elseif (
serverr.weather == "snow"serverr.weather "";
  elseif (
serverr.weather == ""serverr.weather "rain";
  
  
setTimer(10);
}

//#CLIENTSIDE
function onCreated()
{
  if (!
weathereffectsenabled) return; // Important
  
  
defineEmitters();
  
  
onTimeout();
}

function 
onTimeout()
{
  if (
this.previousWeather != serverr.weather) {
    
this.previousWeather serverr.weather;
    
    
this.trigger("onWeatherChanged"serverr.weather);
  }
  
  
setTimer(1);
}

function 
onWeatherChanged(newWeather)
{
  
updateWeather();
  
  if (
newWeather == "snow"player.chat "It's freezing!";
}

function 
onPlayerEnters()
{
  
updateWeather();
  
  
temp.inside = (player.gmap.name == "");
  if (
temp.inside) {
    
this.weather_rain.emitter.removeparticles();
    
this.weather_snow.emitter.removeparticles();
  }
}

function 
updateWeather()
{
  
temp.outside = (player.gmap.name != ""); 
  
  if (
temp.outside && serverr.weather == "rain"this.weather_rain.emitter.emitautomatically true;
  else 
this.weather_rain.emitter.emitautomatically false;
  
  if (
temp.outside && serverr.weather == "snow"this.weather_snow.emitter.emitautomatically true;
  else 
this.weather_snow.emitter.emitautomatically false;
}

function 
defineEmitters()
{
  
this.weather_rain findImg(200);
  
  
with (this.weather_rain) {
    
layer 5;
    
0;
    
0;
    
    
with (emitter) {
      
delaymin 0.05;
      
delaymax 0.05;
      
nrofparticles 1;
      
emitautomatically true;
      
autorotation true;
      
emissionoffset = {-2, -20};

      
with (particle) {
        
image "g4_particle_spark.png";
        
zoom 1;
        
red 0;
        
green 0.25;
        
blue 1;
        
alpha 0.75;
        
mode 0;
        
lifetime 5;
        
speed 1000;
      }

      
removemodifiers(); // Make sure there aren't any duplicate modifiers.
      
addlocalmodifier("once"00"angle""replace"degtorad(-80), degtorad(-60));
      
addlocalmodifier("once"00"x""replace"0screenwidth);
    }
  }
  
  
this.weather_snow findImg(201);
  
  
with (this.weather_snow)
  {
    
10;
    
attachtoowner true;
    
    
with (emitter) {
      
nrofparticles 1;
      
delaymin 0.1;
      
delaymax 0.1;
      
checkbelowterrain true;
      
      
with (particle) {
        
image "g4_particle_whitespot.png";
        
mode 1;
        
zoom 0.4;
        
        
lifetime 10;
        
speed 0;
      }
      
      
with (dropemitter)
      {
        
nrofparticles 1;
        
        
with (particle)
        {
          
image "g4_particle_whitespot.png";
          
mode 1;
          
zoom 0.4;
          
layer 0;
          
          
lifetime 3;
          
speed 0;
        }
        
        
removemodifiers();
        
addlocalmodifier("range"0particle.lifetime"alpha""replace"10);
      }
      
      
removemodifiers();
      
addlocalmodifier("once"00"x""add", -1616);
      
addlocalmodifier("once"00"y""add", -1616);
      
addlocalmodifier("once"00"movez""replace", -3, -5);
      
addlocalmodifier("once"00"movex""replace", -11);
      
addlocalmodifier("range"01"alpha""replace"01);
      
addglobalmodifier("impulse"0.52"movex""add", -22);
    }
  }
}

function 
GraalControl.onResize()
{
  
// This code is here because the rain effect depends on screenwidth.
  
defineEmitters();

What's left to do now is edit the part of the script that updates 'serverr.weather'. For example, you might want it to pick a random weather every hour or so.
Reply With Quote
 


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 07:29 AM.


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