Using a variable in a string.
by Adam Troutman · in General Discussion · 10/07/2006 (11:20 am) · 17 replies
Well this is probably a really stupid question but I am trying to get an ip adress into string format like so
%raw = getRawIP(%client);
%mid = ('%1', %raw);
%pi = %mid;
And I must be doing something wrong because in the console I get this error but I have no clue what it is actually telling me.
%raw = getRawIP(%client);
%mid = ('%1',## ##%raw);
%pi = %mid;
Well I am sure one of you smart people could tell me what I did wrong, thank you.
%raw = getRawIP(%client);
%mid = ('%1', %raw);
%pi = %mid;
And I must be doing something wrong because in the console I get this error but I have no clue what it is actually telling me.
%raw = getRawIP(%client);
%mid = ('%1',## ##%raw);
%pi = %mid;
Well I am sure one of you smart people could tell me what I did wrong, thank you.
#2
10/07/2006 (2:07 pm)
I am getting the users ip adress, I want it to be words not numbers because I don't want all the periods causing problems because it is going to end up being a variable named $money[%pi] = 100 or some other value I need the ip adress to identify the player when he joins the server so his money can be loaded, I don't know I guess that isnt going to work I will just have to find another way to identify the player.
#3
I do want to solve this though, it is alot easier than you might think if you can scope the full idea to me.
You want the IP, so far I'm with you. Then what? You want it divided into words? Like 192 168 0 1 instead of 192.168.0.1?
10/07/2006 (2:21 pm)
I'm still very confused and your syntax above does not suggest alot.I do want to solve this though, it is alot easier than you might think if you can scope the full idea to me.
You want the IP, so far I'm with you. Then what? You want it divided into words? Like 192 168 0 1 instead of 192.168.0.1?
#4
I e:
If you need to know how much money each of the clients have, you just iterate over all the clients and snarf the "money" field out of that.
Alternatively, if you want to avoid the IP address being accidentally parsed as a number, you could just pre-pend an underscore. That way, it's safe to use as an index.
10/07/2006 (3:46 pm)
If you're storing money per client, the easiest way to store it would be with a dynamic field on the client.I e:
%client.money = 100;
If you need to know how much money each of the clients have, you just iterate over all the clients and snarf the "money" field out of that.
Alternatively, if you want to avoid the IP address being accidentally parsed as a number, you could just pre-pend an underscore. That way, it's safe to use as an index.
$money["_"@getRawIP(%client)] = 100;
#5
stefan. I Could try and explain the entire scope of what I am doing to you but that doesn't really seem necessary, essencially all I need is a way of identifying players connecting to the server in a way that can be constantly repeated so I can save their money to a file and reload their money when they connect. The easiest way would seem to be their ip adress but the periods in the ip adress cause problems and thus screw up the process I need to either remove these periods or something.
Okay J \"hplus\" W I understand the idea but it doesn't work because the underscore wont effect anything past the first period in the ip adress.
10/07/2006 (6:38 pm)
J \"hplus\" W I will try the second thing you listed, I realize a client side field would be much easier but I didn't want players just changing their money amount all willy nilly and I wanted it to be server side so I wouldn't have to send everyone the changed client side files since its just a mod for a game not my own.stefan. I Could try and explain the entire scope of what I am doing to you but that doesn't really seem necessary, essencially all I need is a way of identifying players connecting to the server in a way that can be constantly repeated so I can save their money to a file and reload their money when they connect. The easiest way would seem to be their ip adress but the periods in the ip adress cause problems and thus screw up the process I need to either remove these periods or something.
Okay J \"hplus\" W I understand the idea but it doesn't work because the underscore wont effect anything past the first period in the ip adress.
#6
A field on the "%client" argument within the server code will not turn it into a "client-side" field.
If a string is treated as a number, it's only done so when the leading character is a digit. If you prepend an underscore, the string will not be treated as a number. However, it really shouldn't matter, as array indices are string-based anyway -- arrays are just a shallow wrapper on top of global variable naming.
10/07/2006 (10:49 pm)
Quote:I realize a client side field would be much easier but I didn't want players just changing their money
A field on the "%client" argument within the server code will not turn it into a "client-side" field.
Quote:I understand the idea but it doesn't work
If a string is treated as a number, it's only done so when the leading character is a digit. If you prepend an underscore, the string will not be treated as a number. However, it really shouldn't matter, as array indices are string-based anyway -- arrays are just a shallow wrapper on top of global variable naming.
#7
10/07/2006 (10:53 pm)
I tryed it out it doesn't work it the string isnt treated as a number with the underscor attached, until it reaches the first period in the ip adress.... and it doesnt even matter if its numbers its just the periods are messing it up.
#8
$a["1.2.3.4"] = 100;
$a["1"] = 200;
echo($a["1.2.3.4"]);
echo($a["1"]);
If your code has problems, but this works in the console, then it's likely the problem is somewhere else.
10/08/2006 (11:52 am)
Try this in the console, it should show you how it works:$a["1.2.3.4"] = 100;
$a["1"] = 200;
echo($a["1.2.3.4"]);
echo($a["1"]);
If your code has problems, but this works in the console, then it's likely the problem is somewhere else.
#9
10/08/2006 (11:53 am)
Well if you do not feel it is nessecary then I'm sure you can fix it yourself :) Good luck!
#10
10/08/2006 (5:36 pm)
Okay, some arrays? I don't know what your trying to tell me by showing me that. I want to make the ip adress have " " around it instead of jsut being a number thats all I really want to do atm.
#11
10/08/2006 (9:20 pm)
Why not just strip the periods out of the string?
#12
10/09/2006 (11:53 am)
Because I don't know how to do that.
#13
Using IPs to identify the clients is fragile though... you probably want authentication.
If you don't want to do that, generate a random string that is sufficiently long the first time they run, and store it back out with the rest of the prefs, and send that up to the server during the connection handshake.
10/09/2006 (12:46 pm)
Take a look at "strreplace"...Using IPs to identify the clients is fragile though... you probably want authentication.
If you don't want to do that, generate a random string that is sufficiently long the first time they run, and store it back out with the rest of the prefs, and send that up to the server during the connection handshake.
#14
10/10/2006 (8:06 am)
Thank you guys for all your help, I think I am just not as smart as I want to be I keep working on this stuff but I understand so little maybe I just cant do it maybe I should just be reaslistic with myself and pursue something else I love scripting and programming but I don't think I can ever be great at it there all these people that know how to do so many different complex things I don't think I will ever be able to learn I can read all the books I want it seems like I will just continue to be a failure. Thank you all for your help your very kind skilled people I am sure you will make it far in life.
#15
10/10/2006 (8:14 am)
Take some happy pills Adam, everyone has to start somewhere. No-one just knows how to program off the top of their head, it takes time and patience and perseverance. If it's taking you longer than expected that doesn't make you a failure. Take things slow, one step at a time and never give up, giving up makes you a failure.
#16
10/10/2006 (9:07 am)
Agreed :) We all started at 0, remember that.
#17
It's all about diligence, not smarts, IMHO.
I've been programming for 20 years, and all the best programmers I've worked with (and most productive) aren't necessarily the smartest ones (although they're usually pretty smart).
The best developers I've worked with develop rigorous habits and have a very high level of seeing-it-through.
If you start working in that mode, the successes almost always generate enough endorphins and joy that it just eventually becomes second nature and you love to program.
In fact, I've worked in plenty of environments where there were *too many* smart people, and all they do is stand in the hallways all day arguing about the "right way" and nothing ever happens! :)
10/10/2006 (9:26 am)
Tim's right.It's all about diligence, not smarts, IMHO.
I've been programming for 20 years, and all the best programmers I've worked with (and most productive) aren't necessarily the smartest ones (although they're usually pretty smart).
The best developers I've worked with develop rigorous habits and have a very high level of seeing-it-through.
If you start working in that mode, the successes almost always generate enough endorphins and joy that it just eventually becomes second nature and you love to program.
In fact, I've worked in plenty of environments where there were *too many* smart people, and all they do is stand in the hallways all day arguing about the "right way" and nothing ever happens! :)
Torque Owner Stefan Lundmark
All variables/globals in TorqueScript *are* strings. Now, it is not clear what you want from the above example. Where did you get %mid from? What does getRawIP return? Etc. Very unclear.