Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

fuel system issues

Mrc Homewood
Mentor of Randomness
Join date: 24 Feb 2007
Posts: 779
09-13-2008 11:54
im creating a fuel system and it is bugging me to death, im not realy the most math skilled and well i think it showed XD

i threw this script togethor fast so it may not be the most streight forward eather each 1/4 of a tank will give a llSetText [||||] and when i am at a empty tank it dose [||||] insted of [----]

id be greatfull for the help

CODE

==================================
=========Homewood's fuel system==========
=========Mrc Homewood 9/13/08==========
==========give credit where do===========
==================================

float odo;
integer max = 500;
vector old_pos;
integer out;
default
{
state_entry()
{
llSetTimerEvent(1);
old_pos = llGetRegionCorner() + llGetPos();
}
timer()
{
odo += llVecDist(llGetRegionCorner() + llGetPos(),old_pos);
old_pos = llGetRegionCorner() + llGetPos();
if(odo >= max)
{
llSetText("[----]\n\n\n\n\n\n",<1,0,0>,1);
llMessageLinked(LINK_SET,0,"out of gas",NULL_KEY);
out = TRUE;
llSetStatus(STATUS_PHYSICS,FALSE);
llSetTimerEvent(0);
state fuelout;
}
else if(odo >= ((max/4)*3)-10 && odo <= ((max/4)*3)+10)
{
llSetText("[---|]\n\n\n\n\n\n",<1,1,1>,1);
}
else if(odo >= (max/2)-10 && odo <= (max/2)+10)
{
llSetText("[--||]\n\n\n\n\n\n",<1,1,1>,1);
}
else if(odo >= (max/4)-10 && odo <= (max/4)+10)
{
llSetText("[-|||]\n\n\n\n\n\n",<1,1,1>,1);
}
else if(odo >= (max/4)+10 && odo <= (max))
{
llSetText("[||||]\n\n\n\n\n\n",<1,1,1>,1);
llMessageLinked(LINK_SET,0,"full tank",NULL_KEY);
odo = 0;
}
}
}
state fuelout
{
state_entry()
{
llListen(-1287,"","","");
llSetStatus(STATUS_PHYSICS,FALSE);
}
changed(integer b)
{
if(b & CHANGED_LINK)
{
if(out)
{
llWhisper(0,"You Are Out of Fuel");
}
}
}
listen(integer chan, string name, key id, string msg)
{
if(msg == "gas up")
{
out = FALSE;
state default;
}
}
}
Mrc Homewood
Mentor of Randomness
Join date: 24 Feb 2007
Posts: 779
09-14-2008 11:48
anyone?
Escort DeFarge
Together
Join date: 18 Nov 2004
Posts: 681
09-14-2008 13:21
Probably it's because (style aside) the reason for the issue isn't obvious from a quick code read... try putting a debug line in the timer event just before if(odo >= max), e.g...
llOwnerSay("odo=" + (string)odo); and see if the numbers make sense ;)

hth
/esc
_____________________
http://slurl.com/secondlife/Together
Jesse Barnett
500,000 scoville units
Join date: 21 May 2006
Posts: 4,160
09-14-2008 13:42
You need to add spaces around the operators as in;

" ((max/4)*3)-10 && odo <= ((max/4)*3)+10)"

that is a negative 10, NOT subtract 10.

";((max/4)*3) - 10 && odo <= ((max/4)*3) + 10)"

Also take out the definition of old_pos that is redeclared in the timer.
_____________________
I (who is a she not a he) reserve the right to exercise selective comprehension of the OP's question at anytime.
From: someone
I am still around, just no longer here. See you across the aisle. Hope LL burns in hell for archiving this forum
Escort DeFarge
Together
Join date: 18 Nov 2004
Posts: 681
09-14-2008 13:48
From: Jesse Barnett
You need to add spaces around the operators as in;

Oh yeah -- good one Jesse :)

From: Jesse Barnett
Also take out the definition of old_pos that is redeclared in the timer.

I think that line must stay as it's necessary to keep the odo cumulative.
_____________________
http://slurl.com/secondlife/Together
Mrc Homewood
Mentor of Randomness
Join date: 24 Feb 2007
Posts: 779
09-15-2008 13:39
/me hates algebra related math

[EDIT]
nope, now it just stays [----] (out of fuel)
Ziggy Puff
Registered User
Join date: 15 Jul 2005
Posts: 1,143
09-15-2008 17:19
Could the vehicle be moving too fast for your code? It looks like you're trying to make it change within fairly narrow windows (3/4 of max +/- 10m, and so on), so is it possible that within a second, you're moving from one side of that window to another?

Concrete example: Let's say at a given second, you have odo = 239. This is in the 1/2 - 3/4 tank range (125 - 250). Let's say your script worked correctly so far, and the text shows -|||.

One second later, your vehicle is at odo = 261. This requires it to move 22 m/s... not sure if that's possible in SL or not :) But if it did, walk through your script, and (I think) you'd see that it wouldn't trigger any of the display changes. You're looking for 250 +/- 10, and the vehicle moved from 250 - 11 to 250 + 11, and your code won't catch that.

So... see if it works better when you're driving slowly :) That might give you some clues as to what's happening.

Or you could simplify the script for now, at the cost of potentially adding to lag by calling llSetText more often that you need to. But you could change the logic to say:

if (odo >= max)
--- display empty tank

else if (odo >= 3/4 max)
--- display 1/4 tank

else if (odo >= 1/2 max)
--- display 1/2 tank

... and so on.

It only goes to an "else if" if the previous "if" check failed, so by the 2nd check, you already know that odo is not >= max, by the 3rd check you already know odo isn't >= 3/4 max, and so on.

Hope that helps. Disclaimer: haven't tried this in-world, so I could be pretty far off.