Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

string to integer?

Pedlar Decosta
Registered User
Join date: 7 May 2007
Posts: 12
04-19-2008 20:17
My script starts off with an integer that is initially = 1.
When an if statement is met it uses the integer as a string (I think) to do a simple llOwnerSay to announce the current value of the integer.
It then increments the integer in that block of the code with ++.

It works fine,
except I also need to set the number value of the integer from a captured listen (said by the owner only) in open chat so that the increment begins from that number from there on.

My problem I think is not knowing how to convert the string from open chat back into the integer. Whichever way I try I get a type mismatch error.

I thought I had it solved several times. I tried to use the prim description to capture the value, using llSetObjectDesc & llGetObjectDesc, but I kept getting the mismatch errors when I tried to llGetObjectDesc() into the new integer value.

I thought about using a list, but I need a few hundred possible numbers and I'm not even sure if that would solve the problem.

I have got the block of code that I want to use to change the integer value in a different state at the moment (not sure if it is necessary) but I still have the problem of how to set the value of the integer from the captured value from open chat.

This is the framework for it below. This works, but the resume only captures the open chat. So from that point, how do I set the numerical whole number value said by the user in open chat to the new value of the integer k so that the increment starts from the new value when k++ is triggered ?

CODE

integer menu_channel = -8925;
integer k =1;
integer listenkey;
list menu_main = ["Say","Reset","Cancel","Resume"];

default
{
state_entry()
{
llListen(menu_channel, "", llGetOwner(), "");
}
touch_start(integer total_number)
{
llDialog(llDetectedKey(0), "Choose a button", menu_main, menu_channel);
}
changed(integer change)
{
if ( change & CHANGED_INVENTORY )
{
state default;
}
}
listen(integer channel, string name,key id, string message)
{
if (id == llGetOwner())
if (message == "Reset")
{
llOwnerSay("Reseting script...");
llDialog(id, "Choose a button", menu_main, menu_channel);
k= 1;
}
{
if (message == "Say")
{
llOwnerSay( "Number " + (string)k);
k++;
llDialog(id, "Choose a button", menu_main, menu_channel);
}
}
{
if (message == "Cancel")
{
llListenRemove(-8925);
}
}
{
if (message == "Resume")
{
state resume;

}

}
}
}
state resume
{
state_entry()
{
llOwnerSay( "What number do you want to resume from?");
listenkey = llListen(0,"",llGetOwner(),"");
}
listen(integer chatnel,string name,key id, string k)
{
if(ZERO_VECTOR != llGetAgentSize(id))
{
llOwnerSay("Number " + (string)k);
}
llDialog(id, "Choose a button", menu_main, menu_channel);
llListenRemove(listenkey);
state default;

}
}
CODE
Johan Laurasia
Fully Rezzed
Join date: 31 Oct 2006
Posts: 1,394
04-19-2008 20:34
In that particular script, the integer variable 'k' never stops being an integer type variable. If you look at the llOwnerSay (), the integer variable 'k' is (temporarily) cast to a string, so it can be printed, because llSay(), llOwnerSay(), etc, only prints strings, therefore:

integer k=1;
llOwnerSay ((string)k);

will cause the integer to be 'cast' as a string, just temporarily though, the variable 'k' never stops being an integer variable.

However, if you are presented with a situation where an integer IS stored in a string variable, such as:

string myString = "1";

you can do the reverse operation, and cast it into an integer.

string myString = "1";
integer myInteger = (integer)myString;

it's called typecasting, or casting for short. Read the wiki section on variables:

http://wiki.secondlife.com/wiki/Typecast

that should clear it all up for you.
Kahiro Watanabe
Registered User
Join date: 28 Sep 2007
Posts: 572
04-19-2008 20:35
To convert a string to an integer you have to cast it: http://rpgstats.com/wiki/index.php?title=Typecast

for example:

string line = "5";
integer number;

number = (integer)line;

---

Although if the string has characters it will return zero.
Pedlar Decosta
Registered User
Join date: 7 May 2007
Posts: 12
04-19-2008 21:55
Thank you very much, that is exactly what I needed explained. Problem solved :)
I had sort of worked out that the integer remained and the value was only 'used' ..or as you mention the correct term 'cast' to the string.
I also had been very close to the solution in some of my earlier attempts to solve it. This information allowed me to understand it better and see the correct syntax which was all important of course. Once I had the confidence in the correct syntax I it didn't take too much tinkering to get the script working as it should.
An FYI, I ended up keeping the different states as well as using the object description to store the string k. It does seem to process a bit slower now, not sure if it is just SL at the moment, but I will experiment and try to tidy up and streamline it.
JayD Torgeson
Registered User
Join date: 24 May 2004
Posts: 5
04-19-2008 22:30
since casting a string with letters in it returns a 0 I would use a list of the ten base numbers ["0","1","2","3","4","5","6","7","8","9"] and make sure each character in your string matches something in the list before converting so you don't get a false 0. If you detect any letters ask the user for a new string, if not then cast normally.
Pedlar Decosta
Registered User
Join date: 7 May 2007
Posts: 12
04-22-2008 06:24
Thanks JayD.
That is a great tip. I am still struggling a bit with syntax and structure especially where tests are concerned but I will keep at it. I appreciate the advice, it makes sense.
Talarus Luan
Ancient Archaean Dragon
Join date: 18 Mar 2006
Posts: 4,831
04-22-2008 08:24
From: JayD Torgeson
since casting a string with letters in it returns a 0 I would use a list of the ten base numbers ["0","1","2","3","4","5","6","7","8","9"] and make sure each character in your string matches something in the list before converting so you don't get a false 0. If you detect any letters ask the user for a new string, if not then cast normally.


This is not correct.

string myStr = "12Ah93e24";
integer myInt = (integer)myStr; // will result in the integer value 12

Casting to integer converts digits until something that breaks the conversion is found, and then returns that value.

You can also convert a hex string to integer easily like so:

string myStr = "12Ac93e2";
integer myHexInt = (integer)("0x" + myStr);