Game Development Community

Probably easy, but I can't find the answer. Strings in script?

by Stefan Lundmark · in Torque Game Engine · 04/04/2004 (3:07 pm) · 10 replies

I've tried and tried to set strings in different ways and I just can't get it to work properly. I've been going around this problem using IF statements and alot of commandtoserver/commandtoclient commands, and now I realise that was not an effective way.

This is how I tried to set %selchar to char01 and then output it.
%selchar = char01;
echo (%selchar);

This ALWAYS gives me a blank field.
I have also tried to use "char01" instead of char01 and it does not help.
I have also tried it manually in the console and with the same results.

Someone know the answer?

#1
04/04/2004 (3:22 pm)
Sorry, I'm not sure exactly what you're after.

Can you be more clear? What is char01 supposed to be?

Just trying to guess.. the only thing I can figure is that you want %selchar to hold the value of a specific character in a string? For example, if $mystring = "Hello", you'd like to be able to get at each character, "H", "e", "l", "l", and "o". Is this correct?

I can't really tell from what you wrote. :)

In any case, the code you have above, if executed all by itself, is erroneous. %selchar is a local variable, and cannot be used at global scope. $selchar should be used instead.

If this code is inside a function it will work, but you'll have to specify for us what char01 was set to previously. If it was not set to anything in previous code, %selchar will literally have the value "char01"; e.g. echo(%selchar); would echo "char01" to the console. (Again, only if this is executed at local scope, if it's in global code space, nothing will happen)

So, please tell us in what context this code is operating, and give some more details about what you're trying to do. :)
#2
04/04/2004 (3:32 pm)
Pretty simpile idk what josh said im to lazy to read it but ya :D strings are done with "" around them in toruqe
EG

%selchar = "char01";
echo (%selchar);
#3
04/04/2004 (6:42 pm)
Josh Williams

Heya :) Sorry. Char01 is merely text, it could be ANYTHING. I just want %selchar to resemble that text, so that when I give echo(%selchar); it should say

==>echo
char01

Edit: Hey there! What are you saying? Are not % meant for strings? I have used it for quite some time now and thought that it would work. So $ is the right way to go and save strings? Alright, I'll give you my code under these answers just to make sure.

Casey "kc_0045" Vanden Hoek

Sorry mate but that did not work at all.
I already tried that, as I said in my original post.

Quote:
I have also tried to use "char01" instead of char01 and it does not help.

-------------------------------------------------
function CharacterList::onSelect( %this, %id, %text )
{
	switch ( %id ){
	case 1:ShowInfo01();
	case 2:ShowInfo02();
    }
}

function ShowInfo01()
{
 zone01.setVisible(true);
 %selchar = "char01";
}

function EnterWorldPhase01()
{
 commandToServer('EnterWorld', $pref::Password, $pref::Username, %selchar);
}

I think this code is self-explanory to you Torque gurus so I'll leave the explanations out.

Anyway, what the above results in is a blank %selchar when it is sent over to the server, wich is not really what I aimed for.
#4
04/04/2004 (6:59 pm)
A "%" means the var is local and ONLY exists in the function it is used in and functions that are called with it pasted to them as a var on the function line. AKA when you use it in your EnterWorldPhase01 function it doesn't exist so it uses a blank.

If you want a "global" var use the "$" in front of your vars.
#5
04/04/2004 (7:35 pm)
Hey Stefan, thanks for posting the full code. Ok, it's clear now what you're looking for.

Yeah, as I said above, and Dan states too, % indicates that a variable has "local scope". Unless you are a somewhat experienced programmer that probably doesn't mean much though, so I'll explain what we mean by that. Read on if you're interested.

In TorqueScript, the % identifier makes a variable local. That means it can't be seen outside of the function it is created in. Once the program steps outside of the function, the variable will be destroyed.

TorqueScript doesn't force all variables to be local though. You can use the $ identifier to make a variable global. Once a global variable is created, it can be seen, anywhere, even in separately loaded script files.

Most programming languages support the concept of local and global variables, so this is a good thing to learn if you're interested in programming. Good question :)

Ok, so all that might be interesting in a high-level kind of way, but you're probably more interested in just seeing how the heck to get your code to work. There are, as always, a bunch of ways to do what you want. The simplest (but not the cleanest) solution is to use a global variable.

To do this, just change all of your instances of %selchar to $selchar. That will make $selchar a global variable, which means that it will exist outside of the ShowInfo01() function, and you can read it anywhere.

Again, this will work but there is a cleaner, better way to accomplish the same thing.

Generally, using global variables should be avoided as much as possible. It's probably too much to explain here in detail, but the basic reasoning for this (in any programming language) is that global variables are.. well, difficult. If you have a global variable, any piece of code can change it's value. That's just asking for trouble. As your project grows, it becomes harder and harder to keep track of which lines of code affect which global variables, and you can end up with very hard to manage code that changes global variable values in confusing ways.

Ok, so what's this method of doing what you want to do without using a global variable?

You can use a function return value. A return value essentially lets you see the value of a local variable in a function, without having to make that variable global. We can use return-parameters to do the same thing as well. Unfortunately, the way your code is set up right now, there's no nice and clean way to utilize a return value or parameter with ShowInfo01().

So, you can just decide to do it with a global variable later on, or we can talk about how to structure your functions differently. It's up to you :)

If you are interested in seeing about structuring your functions in a different way, let me know. I'll have to ask you for more details on what exactly you're trying to do, but we'll be able to figure it out. You certainly don't have to do this though, your code will work as is, with a global variable. It's just not the safest possible way to do things, in terms of avoiding complicated (and hard to debug) code.

Hope that is helpful. :) Again, the short answer is.... change all instances of %selchar to $selchar. If you are just concerned with getting it to work right now, that'll be fine. If you plan on having lots of code and don't want it to be a mess later on, let me know and we can talk more.
#6
04/05/2004 (2:37 am)
Josh Williams

I'm very interested. I have seen return commands in C++ alot but never actually understood what they did. The %selChar variable in this case does not need any security at all really, all it does is select a character on the server and if you put anything else than char01, char02 or char03 the server will drop the connection. If the value would be manipulated, all it would do is to tell the server to use another character, which isn't a problem for the gameplay at all. ;)

Now, I was going to do alot of other functions that I planned using it the same way, but if, as you say; it's very unsecure, then I might wanna hear a bit about the return commands, if that's okay with you. I'm going to stop writing of the zone code and wait.

Can I contact you by email with a short code snippit?
Again, thanks Dan & Josh for all your help. This was very interesting to me.

Edit: Horrible grammar.
#7
04/05/2004 (3:20 am)
Here is some quick code off the top of my head, there may be mistakes and definatly needs rewritten to fit your needs. This is mostly to give you an idea of a way this could be done.

I am not sure exactly what you are trying to do, so some of it may not even fit at all.

function foo()
{
   return bar;
}

function checkfoo()
{
   %check = foo();
   echo(%check);
}
Executing checkfoo, should echo bar, utilizing the return of foo. A very rough example but may help you get an idea.

I'm not sure exactly how you code is set up but you could do something like:

function CharacterList::onSelect( %this, %id, %text )
{
   //Remove the switch and just pass the id onto a general showinfo function 
   //that will handle any ID
   ShowInfo(%id);
}

function ShowInfo(%id)
{
   //Logic to sort out what needs done when an id was selected. 
   //This could be switches ifs etc. 
   //What follows is just a rough example of using the passed id.
 
   //%id was passed as 1 so we display the first
   if(%id == 1)
   {
      zone01.setVisible(true);
   }
   else if(%id == 2)
   {
      zone02.setVisible(true);
   }
}

function EnterWorldPhase01()
{
   %selchar = getSelectedChar();
   commandToServer('EnterWorld', $pref::Password, $pref::Username, %selchar);
}

function getSelectedChar()
{
   %selchar =  //Code to figure out what character is slected 
   // then return it to the code that asked for it.
   return %selchar
}
Edit: Formatting.
#8
04/05/2004 (9:00 am)
Stefan, of course you can contact me by email. It's joshw@garagegames.com :) I'm super-busy with projects, so I'm not saying I can be a full-on consultant or anything, but there's always time to help out a bit.

Bruce's suggestions above are very helpful. I will try to expand on them today, but I'm not sure exactly when I'll have time to do it.

Anyway, it's great that you're learning programming with TorqueScript. I can see you're already good at figuring out what you want to do, and finding a way to do it in script. Now we can figure out even slicker ways to get stuff done. I'll write again later, or maybe someone else will jump in for me. :)
#9
04/05/2004 (9:09 pm)
Hey Stefan, finally got home. Just checking in. Was Bruce's code above helpful enough for ya? If not, let me know.
#10
04/06/2004 (4:35 pm)
I have been away, finally got home. Needed to take a break away from TorqueScript for a while. 24/7 in 3 weeks bring you down to a pretty lame level. :/

Josh Williams

Thanks alot for your help. I think the above example gave me a clue on how it works, because suddenly it said "DING!" in my head :) Took a while though lol.

TorqueScript is actually very fun to play around with, and yes, we have it all on paper.. how we want it, I just need to do it in script. Needless to say it gets into a lot of IF statements :P

Bruce Wallace

Ey! There we go, thanks alot mate :) It made me think twice on my current setup, and yes you understood what I wanted to do :)

Thanks Josh & Bruce, you're gold!