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;
x = 0;
y = 0;
with (emitter) {
delaymin = 0.05;
delaymax = 0.05;
nrofparticles = 1;
emitautomatically = false;
autorotation = true;
emissionoffset = {-2, -2, 0};
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", 0, 0, "angle", "replace", degtorad(-80), degtorad(-60));
addlocalmodifier("once", 0, 0, "x", "replace", 0, screenwidth);
}
}
}
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;
x = 0;
y = 0;
with (emitter) {
delaymin = 0.05;
delaymax = 0.05;
nrofparticles = 1;
emitautomatically = true;
autorotation = true;
emissionoffset = {-2, -2, 0};
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", 0, 0, "angle", "replace", degtorad(-80), degtorad(-60));
addlocalmodifier("once", 0, 0, "x", "replace", 0, screenwidth);
}
}
this.weather_snow = findImg(201);
with (this.weather_snow)
{
z = 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", 0, particle.lifetime, "alpha", "replace", 1, 0);
}
removemodifiers();
addlocalmodifier("once", 0, 0, "x", "add", -16, 16);
addlocalmodifier("once", 0, 0, "y", "add", -16, 16);
addlocalmodifier("once", 0, 0, "movez", "replace", -3, -5);
addlocalmodifier("once", 0, 0, "movex", "replace", -1, 1);
addlocalmodifier("range", 0, 1, "alpha", "replace", 0, 1);
addglobalmodifier("impulse", 0.5, 2, "movex", "add", -2, 2);
}
}
}
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.