Problem using String.Append [SOLVED]

Started by theatrx, Fri 12/10/2007 02:13:03

Previous topic - Next topic

theatrx

I don't want the player to use the keyboard for this.

There are a bunch of letters (objects).  I would like the player to press the mouse to catch the letter and then put that letter in the GUI textbox.

I have this but it doesn't seem to work.

NETEXT.Text.Append("e");

Any suggestions?

Life is a banquet and most poor sonsofbitches are starving to death

frission

#1
.Append doesn't automatically change the String on which it is called; it just returns a String that you then have to feed back into the original if you want it to be changed.

Try:

NETEXT.Text = NETEXT.Text.Append("e");

(It took me a little while to figure this out too.)

theatrx

#2
I think this would be a nice thing for people to know.  I've struggled with this for 2 days.  Thank you for the answer. It works perfectly!
Life is a banquet and most poor sonsofbitches are starving to death

monkey0506

Straight from the manual:

The new concatenated string is returned as a new string from this function; it does NOT modify the original string.

Example:

Code: ags
String mytext = "Hello";
mytext = mytext.Append("World");
Display(mytext);


will display "HelloWorld".

So yes, this would be a nice thing for people to know. Then again, RTFM would be a nice thing for people to do*. :P

*I know this may sound harsh, but I don't mean to be rude. I'm simply stating that a lot of problems encountered could be solved simply by reading the manual entry. It's nothing personal I assure you. That's why I put the smiley!!!

theatrx

I read.  The issue is not what one reads.  The issue is "does one understand what one is reading".  The way that answers are displayed on AGS are not necessarily the way the question is possited.  Meaning "You never get a straight answer on AGS".   It's always given with a bit of knowledge yet unknown... which I'm not adverse to... I'd simply like a straight answer to a straight question.  Thank you for giving me that.  You solved a problem REALLY QUICKLY by simply telling me the way out of the problem.  Believe me, I tried but when not all of the answers are given in the manual, it's really hard to find out where the 'real' answer is.  You made it very clear at what AGS could and could not do.  I understand that now.  It will be filed away and the question never asked again.  But, I think it's a valid question that SHOULD be posted somewhere so no one has to ask the question again.  Tell me, How would one know that?  That an append doesn't really mean an append in this situation?  Just wondering.  Again, thank you for answering the question... You solved 12 puzzles with one answer.  I'm not trying to be a smart ass... I just want that the mods don't have to answer this question again. and again... and AGAIN... who would know that append doesn't mean append!? Shouldn't someone tell someone that?  Thank you again.  But you solved a problem with a simple and elegant response and hopefully one that others will look at and understand the answer.  Thank you again.  theatrx
Life is a banquet and most poor sonsofbitches are starving to death

Khris

Quote from: theatrx on Fri 12/10/2007 08:07:14But, I think it's a valid question that SHOULD be posted somewhere so no one has to ask the question again.  Tell me, How would one know that?  That an append doesn't really mean an append in this situation?  Just wondering.

W? T? F?
Just open your eyes, for fuck's sake:

"The new concatenated string is returned as a new string from this function; it does NOT modify the original string."

You should be apologizing, not trying to defend your ignorance.

Ashen

#6
And you should watch your tone, Khris. You've been warned about that before.

Steve, if all it said was "The new concatenated string is returned as a new string from this function; it does NOT modify the original string." then yes, I might agree it's a little unclear (at least, without looking up 'concatenated' - but "does NOT modify the original string" is pretty clear, IMO). However, there's also the code example, which includes the line:
Code: ags

mytext = mytext.Append("World");


Showing you the correct usage of Append, as the code examples are meant to if the text description isn't clear. Further more, a forum search (which you're meant to do, as well as reading the manual, before posting) for 'String.Append' turns up at least one thread where this exact question is answered (by monkey_05_06, as it happens), and countless others which show working examples of how it's used. How is that not a "straight answer"?

Yes, the manual should be as easily understandable as possible - but however clear it is, there'll always be somethings that to some people, just don't make sense - which is why we have the forums, and the Search option. Still, questions that shouldn't be asked again and again, will be. All we can do is point them in the right direction, and lock the threads that need it.
I know what you're thinking ... Don't think that.

theatrx

Everyone... thanks for the help.  I guess there were a few things confusing me  like the append character.  I also realized that the object I was pushing had a very small strike point which was only complicating the issue since in some cases it was working but I didn't realize it.  Sorry for my frustration and again thanks for all the help.
Life is a banquet and most poor sonsofbitches are starving to death

frission

I just want to jump in and note that there are two things I find a little confusing about Append():

1. I don't think most people are used to dealing with strings in particular in this OO style. Most high-level scripting languages (PHP, Javascript, Java) let you treat them as any old variable and concatenation is handled simply through operators (usually +, sometimes . -- whether this is really how they handle them, or is just a gloss put on to make them easier to handle, I don't know). Now I'm not complaining about the way AGS does things, as I am sure it is not arbitrary, but I understand why people (including myself) get confused at first. Once I figured out the .Append() thing, all of the rest of how to deal with strings made sense. I'm not sure if a big honking, "HEY, if you are used to concatenation by operators, READ THIS FIRST" warning would be appropriate, but it might be something for the FAQ? (Also, can I say how much I hate the word concatenation? Could computer programmers have come up with a less-familiar, more-scary name? It's almost as bad as HREF as a property for linking URLs. ;))

2. While the manual does try to be clear on this, it took me a few reads (after having it not work correctly) to really figure out what it was trying to say. I'm not 100% sure why this is, but apparently I wasn't alone in this. I suppose if I were going to re-write it, I would add a negative example too, like:

Quote
NOTE: The following will NOT work.
String mytext = "Hello";
mytext.Append("World");
Display(mytext);

The result displayed will ONLY be "Hello", because Append does NOT modify the String on which it is called -- it simply RETURNS a value. In order for that value to be added to the original string, it must be re-assigned as so:

mytext = mytext.Append("World");

Or something like that. Anyway, I know, I know, RTFM, but in this case I had the same confusion, mostly because I don't think I appreciated exactly that Strings are "special" here, that they are structs and not variables, which once you know is easy to make sense of, but it's not what I had in mind when going into it, due to experience in other scripting languages.

Khris

If you're used to using operators, you're used to write:
mytext=mytext+"World";
Correct?

The transition to
mytext=mytext.Append("World");
should be easy, since you're used to put "destination=" in front of the expression.

Just my $0.02 ;)

Ashen

Since theatrx's original question has been answered, and I don't really see this discussion going anywhere, I'm going to lock it.
I know what you're thinking ... Don't think that.

Pumaman

I think it's very easy for us to say "it already says this in the manual, stupid!!" but when people are saying that they have read the manual but not understood it, then to me that says that there is a fault with the manual since it's not explaining things properly.

Therefore, for the next version I am rewording the Append/AppendChar functions to include this:


IMPORTANT: The result of joining the strings together is returned as a new
string from this command. The original string will NOT be changed. For
example, the following script will not do anything:
Code: ags
mytext.Append("World");

what you probably want instead is:
Code: ags
mytext = mytext.Append("World");



Do you guys think this is easily understandable?

theatrx

Thank you Chris.  I was getting confused about the difference between Append and append char.  Another thing that confused me was since the player starts and there is nothing there to begin with, I guess the append means add something to nothing?  I think I was thinking too hard about what you actually wanted... meaning does the player have to put something in the string before it can be appended.   Thank you so much.
Life is a banquet and most poor sonsofbitches are starving to death

Ashen

See, now the conversation's going somewhere...
Personally, the biggest problem I had with the current version was the word 'concatenated'. I didn't know what it meant, but the code example made it pretty clear. The new wording is much clearer, but I'm not sure about the 'negative' example. I can honestly see having a non-functional example in the manual further confusing as many people as it helps.

theatrx:
Append & AppendChar both require there to be something in the String to start with. Try it for yourself - attempting to append to a null String will crash the game. Declaring your Strings like this will avoid it:
Code: ags

String myString = "";


Since the String is empty, but not null (now that's confusing, I'll grant you - and I'd add my support to any suggestion to have Strings initialise like that by default). While you're updating the manual, Chris, maybe you should clarify that point, too?
I know what you're thinking ... Don't think that.

Scorpiorus

Quote from: Ashen on Mon 15/10/2007 23:52:21I'd add my support to any suggestion to have Strings initialise like that by default

Yeah, provided there are no real issues of technical kind with implementation (how memory allocated? etc.), assigning all String pointers (including arrays of pointers and data members of structs) to the dummy "" instance by default would make things less confusive for beginners.

Such a change could potentionally break things in already written code, though -- ie. no errors at compile time, but introducing logic ones at run-time.

I'd also vote for still having a possibility to define/set a null String pointers then, as quite often it is handy to have a separate state for nothing (null).


String a;           // defaults to ""
String b = "blah";  // inits value to "blah"
String c = null;    // nothing


Some older thread discussing the issue: http://www.adventuregamestudio.co.uk/yabb/index.php?topic=28977

Pumaman

Yeah, initializing Strings to an empty string rather than null is probably something that I should have done, but as you say it could break existing scripts if I changed it now.

QuoteThe new wording is much clearer, but I'm not sure about the 'negative' example. I can honestly see having a non-functional example in the manual further confusing as many people as it helps

Hmm, I'm not sure about this. The most common mistake people make with String.Append is not assigning the result to anything, so my feeling would be that showing this and explicity saying that it won't work might help people. I guess only time will tell.

monkey0506

Quote from: Pumaman on Wed 17/10/2007 22:12:12as you say it could break existing scripts if I changed it now.

It might be a bit specialized...but maybe there could be some type of game option for this? A move toward empty-initialised Strings versus null-initialised ones would be good IMO though the potential for it to break existing scripts is clearly present. However the ability of a String to hold the value null is the reason I always check:

Code: ags
if ((String_param == null) || (String_param == "")) return;


In my functions.

In any case this is a wee bit off topic now. :=

SMF spam blocked by CleanTalk