Author Topic: Changing The Sun's Ambience  (Read 1383 times)

How would I change the sun's ambience through code?

How would I change the sun's ambience through code?

This is a little code my friend helped me with while I was experimenting with stuff:

Code: [Select]
function setSun(%color,%ambient) {
for(%i=0; %i<missiongroup.getCount(); %i++) { %obj=missiongroup.getObject(%i); if(%obj.getClassname()$= "Sun") %obj.delete(); }
$Sun = new sun(INSERT SUN NAME HERE) { color = %color; ambient = %ambient;}; //replace INSERT SUN NAME HERE with whatever you'd like to name your sun
}

Then, all you need to do is call the function setSun with the color, like so:

Code: [Select]
setSun("0.600000 0.600000 0.600000 1.000000","0.600000 0.600000 0.600000 1.000000"); //replace the colors with whatever you'd like. The first set of numbers is the sun's color, and the second set of numbers is the sun's ambient color.

I don't want to change the sun's color, only the ambience.

Get the sun object, and echo(sun.ambient);

and just change it to your liking by doing sun.ambient = %ambient;

Get the sun object, and echo(sun.ambient);

and just change it to your liking by doing sun.ambient = %ambient;
The sun doesn't update in realtime. It will have to be recreated as in Electrk's post.
The color can be preserved in this manner:
Code: [Select]
for(%i = 0; %i < missionGroup.getCount(); %i++)
{
if((%obj = missionGroup.getObject(%i)).getClassName() $= "Sun")
{
%color = %obj.color;
break;
}
}
setSun(%color, %ambient);

So this should work?
Code: [Select]
if((%obj = missionGroup.getObject(%i)).getClassName() $= "Sun")
{
%color = %obj.color;

break;
}

setSun(%color, %ambient);

function setSun(%color,%ambient)
{
for(%i=0; %i<missiongroup.getCount(); %i++)
{
%obj=missiongroup.getObject(%i); if(%obj.getClassname()$= "Sun") %obj.delete();
}

$Sun = new sun(Sun)
{
color = %color;
ambient = %ambient;
}
}

here's a more updated version of what electrk posted, i'm pretty sure it works from what i've tested

Code: [Select]
function getSunCount()
{
%ct=-1;
for(%i=0;%i<missiongroup.getcount();%i++)
{
%obj=missiongroup.getObject(%i);
if(%obj.getClassName()$="Sun"){%ct++;}
}
return %ct;
}

function getSun(%c)
{
%ct=-1;
for(%i=0;%i<missiongroup.getcount();%i++)
{
%obj=missiongroup.getObject(%i);
if(%obj.getClassName()$="Sun"){%ct++;}
if(%ct == %c){return %obj;}
}
}

function changeLighting(%d, %l, %p, %r, %s, %c, %am, %az, %e)
{
for(%i = 0; %i <= getSunCount(); %i++)
getSun(%i).delete();
new Sun(Sun)
{
color = %c;
ambient = %am;
azimuth = %az;
elevation = %e;
direction = %d;
locked = %l;
position = %p;
rotation = %r;
scale = %s;
};
missionGroup.add(Sun);
}

so you could just do something like this maybe

Code: [Select]
function changeAmbient(%sun, %a)
{
changeLighting(%sun.direction,
      %sun.locked,
      %sun.position,
      %sun.rotation,
      %sun.scale,
      %sun.color,
      %a,
      %sun.azimuth,
      %sun.elevation);
}
changeAmbient(getSun(0), whatever);


not tested but try it

also in case you bugger something up here's the default lighting for skylands

changeLighting("0.57735 0.57735 -0.57735", 1, "0 0 0", "1 0 0 0", "1 1 1", "0.6 0.6 0.6 1.0", "0.5 0.5 0.5 1.0", "238", 21);

« Last Edit: December 27, 2011, 10:33:39 PM by otto-san »