PDA

View Full Version : Placing string values in your text


prozac424242
12-08-2005, 06:59 PM
One thing in gs2 that I have been trying off and on how to figure out is how to put variables in strings. After seeing some random examples and taking a closer look at the gscript wiki page that I have read many times, it finally occured to me. And since for me, the solution was so elusive, I will post it here so others can see how this rather simple thing is done.

The @ symbol is what combines strings, or strings and variables, in gs2.
If you want a gui box to show the player's name at the end, you could have the text line of the GuiTextCtrl box be


text = "Welcome to some server that is crappy but I can't tell beacsue I am a noob with money who rented a spot and threw stuff together, "@player.nick;


So: You define a string by putting it in quotes "like this"
and the player.nick is a variable whose type is a string, so it does not need quotes. The @ does not read the contents of player.nick, it simply sticks the two together.

But what if you want to put more text after the player's name?
You use another @

text = "Welcome to some server that is crappy but I can't tell becasue I am a noob with money who rented a spot and threw stuff together, "@[email protected]" , so have fun trying to figure out my obtuse customized player movement and swords that hit off target and graphics ripped from emulators, you know the drill. Enjoy it while your gold account lasts casue i r b 1337 baby!";


If, instead of the player's name, you want to put in a player variable or a server string, #s(client.whateveryoucalledthestring) and #s(serverr.thestringnameyougavefortheserverstring) still works.

Also, if you have a Gui box whose text you want updated, having any kind of setTimer(number) will casue the text to flash. .05 time casues the text to not show at all, if read from a server string, if I remember correctly the server's fastest response time is .1, so in the timeout, do a check that grabs the string value to a variable, sleep for .1, and check the variable against the current string value. If the values differ, then update the text attribute of whatever you called the box. The text attribute for GuiTextCtrl ("My_Text") would be My_Text.text and you can set it equal to whatever you like in a separate part of the npc.

I hope this helps someone, and please correct me where I am wrong. But what I did here (with different text of course) works on my server.

ApothiX
12-08-2005, 07:09 PM
One thing in gs2 that I have been trying off and on how to figure out is how to put variables in strings. After seeing some random examples and taking a closer look at the gscript wiki page that I have read many times, it finally occured to me. And since for me, the solution was so elusive, I will post it here so others can see how this rather simple thing is done.

The @ symbol is what combines strings, or strings and variables, in gs2.
If you want a gui box to show the player's name at the end, you could have the text line of the GuiTextCtrl box be
Yes, if you would have RTFM, you would have figured this out quite quickly.

If, instead of the player's name, you want to put in a player variable or a server string, #s(client.whateveryoucalledthestring) and #s(serverr.thestringnameyougavefortheserverstring) still works.
4) If you do not know what you are doing, please do not post a solution here. Doing so only confounds the original advice-seeker. People who consistently post bad advice will be masked from the scripting forum.
You should NOT mix old-graalscript identifiers (#s), with the new graalscript, there is absolutely no need.
text = "This works: " @ client.stringname @ ", Imagine that.";

Also, if you have a Gui box whose text you want updated, having any kind of setTimer(number) will casue the text to flash. .05 time casues the text to not show at all, if read from a server string, if I remember correctly the server's fastest response time is .1, so in the timeout, do a check that grabs the string value to a variable, sleep for .1, and check the variable against the current string value. If the values differ, then update the text attribute of whatever you called the box. The text attribute for GuiTextCtrl ("My_Text") would be My_Text.text and you can set it equal to whatever you like in a separate part of the npc.
Do NOT use Sleep() in a timeout.

Skyld
12-08-2005, 07:12 PM
I would quite like it if you spaced the operator out a bit, i.e.
res = one @ two;
Instead of...
[email protected];
Note that you can also use SPC and NL operators, the former inserting a space and the latter inserting a new line (effectively, a "\n"), i.e.
one = "Doris";
res = "Hello" SPC one;
... producing "Hello Doris!".

If, instead of the player's name, you want to put in a player variable or a server string, #s(client.whateveryoucalledthestring) and #s(serverr.thestringnameyougavefortheserverstring) still works.
This is bad:
res = "My my, " @ #s(clientr.stringname);
You would use the following:
res = "My my, " @ clientr.stringname;
Please do not mix old gscript and gscript.

Also, if you have a Gui box whose text you want updated, having any kind of setTimer(number) will casue the text to flash. .05 time casues the text to not show at all, if read from a server string, if I remember correctly the server's fastest response time is .1, so in the timeout, do a check that grabs the string value to a variable, sleep for .1, and check the variable against the current string value. If the values differ, then update the text attribute of whatever you called the box. The text attribute for GuiTextCtrl ("My_Text") would be My_Text.text and you can set it equal to whatever you like in a separate part of the npc.
And if you are not updating the text every 0.1 seconds, then this is throughly bad practise and you should use approximately more events!

ApothiX
12-08-2005, 07:15 PM
Note that you can also use SPC and NL operators, the former inserting a space and the latter inserting a new line (effectively, a "\n"), i.e.
one = "Doris";
res = "Hello" SPC one;
... producing "Hello Doris!".

SPC and NL look ugly :(


This is bad:
res = "My my, " @ #s(clientr.stringname);
You would use the following:
res = "My my, " @ clientr.stringname;
Please do not mix old gscript and gscript.
I beat you to it >:P

Skyld
12-08-2005, 07:17 PM
SPC and NL look ugly :(
I am not sure about you, but I would rather do the following:
this.text = "Some text" NL
"Some more text" NL
"Yet another line";
... rather than:
this.text = "Some text\n" @
"Some more text\n" @
"Yet another line\n";
I beat you to it >:P
Your need for competition is bewildering.

ApothiX
12-08-2005, 07:20 PM
I am not sure about you, but I would rather do the following:
this.text = "Some text" NL
"Some more text" NL
"Yet another line";
... rather than:
this.text = "Some text\n" @
"Some more text\n" @
"Yet another line\n";
Perhaps NL is good, but 'this.text = "Blah " @ variable' looks better than 'this.text = "Blah" SPC variable', IMO.

Your need for competition is bewildering.
Stop spoiling my moments of glory.

Skyld
12-08-2005, 07:22 PM
Perhaps NL is good, but 'this.text = "Blah " @ variable' looks better than 'this.text = "Blah" SPC variable', IMO.
I agree with you on the rather unnecessary use of SPC, but I suppose it falls down to preference.
Stop spoiling my moments of glory.
I think you have an unusual idea of glory. >_<

prozac424242
12-08-2005, 07:39 PM
... I once again apoligize for something I posted here.
In this case, my only intent was to provide something useful.

Question : why is mixing gs1 and gs2 bad if it makes the npc work?

Also, from the first reply to my post, if you had read my post you would have seen that I DID RTFM several times already and only yesterday did this solution dawn upon me.

From the volume and content of his posts, it appears that Skyld is the most knowledgable about gs2 other than Stefan.

I vote Skyld write the gs2 version of npcprogramming.doc (in some html or php format) with such useful exaples of sample code in context. Becasue such a thing would be most helpful, as a companion to the wiki, in my opinion, which is based on my experience.

I vote for Skyld to do it becasue such an example based tutorial is something that I think many people will need, and I would write one and use it ... if it would not be so harshly criticized by apparently angry and overly critical people, such as some but not all of the poeple who post here seem to be.
And Skyld has a very good way of explaining gs2 in a way that I understand it. Thank you, Skyld.

Skyld
12-08-2005, 07:52 PM
... I once again apoligize for something I posted here.
In this case, my only intent was to provide something useful.
Don't apologise for it. A significant portion of your post was useful.
Question : why is mixing gs1 and gs2 bad if it makes the npc work?
Because new gscript is capable of doing everything old gscript is, and in a nicer fashion.
From the volume and content of his posts, it appears that Skyld is the most knowledgable about gs2 other than Stefan.
I'm not too sure about that!
I vote Skyld write the gs2 version of npcprogramming.doc (in some html or php format) with such useful exaples of sample code in context. Becasue such a thing would be most helpful, as a companion to the wiki, in my opinion, which is based on my experience.
That's quite a large task, with quite a lot to explain, however, I may write some tutorials sometime.
I vote for Skyld to do it becasue such an example based tutorial is something that I think many people will need, and I would write one and use it ... if it would not be so harshly criticized by apparently angry and overly critical people, such as some but not all of the poeple who post here seem to be.
Don't take it too personally. The fact that you are trying to improve is a fairly large step forward.
And Skyld has a very good way of explaining gs2 in a way that I understand it. Thank you, Skyld.
You're welcome.

xAndrewx
12-08-2005, 08:27 PM
Yes, he's also hot too.

jake13jake
12-09-2005, 08:24 AM
I agree with you on the rather unnecessary use of SPC, but I suppose it falls down to preference.

I think you have an unusual idea of glory. >_<

hmmm... variables that self-concatenate... yikes..... That scares me... Couldn't stefan had just recommended defining your own variables and going

SPC = " ";
NL = "\n";
function([email protected]@[email protected]@string);

I generally would do the above only if I'm lazy though, otherwise I'd write in the space and the new line myself. I can see how SPC can be really really useful when trying to debug something in a hurry using the NPC's chat string.

Skyld
12-09-2005, 09:57 AM
hmmm... variables that self-concatenate... yikes..... That scares me... Couldn't stefan had just recommended defining your own variables and going

SPC = " ";
NL = "\n";
function([email protected]@[email protected]@string);

I generally would do the above only if I'm lazy though, otherwise I'd write in the space and the new line myself. I can see how SPC can be really really useful when trying to debug something in a hurry using the NPC's chat string.
Well, they're not really variables.

Fry
12-09-2005, 03:59 PM
It's as much an operator as + or - are, they are just different. Not that bad actually, maybe even useful.

ApothiX
12-09-2005, 06:10 PM
hmmm... variables that self-concatenate... yikes..... That scares me... Couldn't stefan had just recommended defining your own variables and going

SPC = " ";
NL = "\n";
Actually, they are comparible to a C's #define directive,
#define SPC @ " " @
#define NL @ "\n" @

function([email protected]@[email protected]@string);
That's even more ugly than what it really is.. 'function(string SPC string NL string);'

Dach
12-09-2005, 09:46 PM
Perhaps NL is good, but 'this.text = "Blah " @ variable' looks better than 'this.text = "Blah" SPC variable', IMO.

The more applicaple usage of SPC is;
this.text = variable SPC variable2;
Which is many times more appropriate than;
this.text = variable @ " " @ variable2;

Thank you for your time! ;)

jake13jake
12-10-2005, 04:18 PM
Actually, they are comparible to a C's #define directive,
#define SPC @ " " @
#define NL @ "\n" @


That's even more ugly than what it really is.. 'function(string SPC string NL string);'

#define is just a find/replace all

Fry
12-10-2005, 04:23 PM
#define is just a find/replace all
So what was the reason for that statement? It's doing exactly that what Okiesmokie said, nothing comparable is available in GS2 though, as far as I know.

Admins
12-10-2005, 05:56 PM
There is also TAB. Tabs must be used when doing columns in GuiTextListCtrl or GuiMLTextCtrl.