Home   Help Search Login Register  

Author Topic: Changing the type of a variable  (Read 1252 times)

0 Members and 1 Guest are viewing this topic.

Offline snYpir

  • BIS Team
  • ****
  • OFPEC Jedi Master
    • OFPEC
Changing the type of a variable
« on: 04 Feb 2003, 09:12:33 »
This may have been answered before (and yes i have searched!)...

Is there a quick way to change the type of a variable? In particular, change a string to a number?

I have a dialog, and in it there are edit fields. Now, these edit fields are strings by default. To change the strings to a number i am using a function as follows (also attached):

Code: [Select]
// a very limited way to convert a positive string to a number
// is there a better way to do this?

private ["_string","_c"];

_string = _this;
_c = -1;

while { (format["%1",_c]) != _string } do
{
   _c = _c + 1
};

_c

It simply increments a counter until it equals the string. VERY limited, because:

  • It doesn't work for floats (only integers)
  • It only works up to numbers smaller than 10000 (because it is a function)
  • It is slow and a stupid way to do this


Anyone able to suggest a better method (and tell me if there is a simple way to do this, pehaps a command i missed or something?) :P
Bohemia Interactive Simulations

Offline Sui

  • Former Staff
  • ****
    • OFPEC
Re:Changing the type of a variable
« Reply #1 on: 04 Feb 2003, 10:11:13 »
Upon seeing this topic I recalled something in my snippets file (the text file of pieces of stolen code I have collected over the years)...

It went something like this:

Call "MyFloat = "+MyString

I mucked around with it, as I'm not quite smart enough to understand it and came up with this:

Call format ["Variable = "+ string ]

Which will make Variable the number version of your string. It works with floats (as far as I could tell), and you can rename (or retype is more accurate I think) an existing variable using that code.

As I said, I stole this off some smart guy (can't remember who I'm afraid) so full credit to them, rather than to me ;)

Offline snYpir

  • BIS Team
  • ****
  • OFPEC Jedi Master
    • OFPEC
Re:Changing the type of a variable
« Reply #2 on: 04 Feb 2003, 19:43:51 »
Fantastic! I'll give it a go... ;D
Bohemia Interactive Simulations

Offline snYpir

  • BIS Team
  • ****
  • OFPEC Jedi Master
    • OFPEC
Re:Changing the type of a variable
« Reply #3 on: 04 Feb 2003, 20:05:41 »
Works like a charm. I have made it a function, and added it to the function library. What other gems do you have in that file? ;)
Bohemia Interactive Simulations

Offline Sui

  • Former Staff
  • ****
    • OFPEC
Re:Changing the type of a variable
« Reply #4 on: 05 Feb 2003, 00:13:23 »
Most of it is pretty basic stuff from back in the day...

Some of it is useful but random, like the class name for bullet cartridge is "FXCartridge" ;)

Basically when I see something I didn't know, I copy it and paste it in there...