Author Topic: type casting?  (Read 801 times)

0 Members and 1 Guest are viewing this topic.

Offline zyxstand

  • Veteran
  • *****
  • Posts: 1106
  • Mother of all Bombs
type casting?
« on: June 15, 2007, 01:09:53 am »
I'm so annoyed by the fact that I can't convert an integer to a byte or to a single.  I couldn't find anything online about it!  Is there a way to type cast so to convert from an integer to a byte?
I understand that a byte holds less info but I know for a fact that the integer in my code will always be below 256 (even though it's signed).
I'm glad that there is a strtoint and inttostr but that really doesn't get me far enough.

For example:
If I'd want a player to the spas by typing "/setwep 5" I can only read the 5 as a string and convert it to an int but that's not good enough to parse it as a ForceWeapon parameter.

Thnx for any help!
Can't think of anything original to put here...

Offline EnEsCe

  • Retired Soldat Developer
  • Flamebow Warrior
  • ******
  • Posts: 3101
  • http://enesce.com/
    • [eC] Official Website
Re: type casting?
« Reply #1 on: June 15, 2007, 01:28:29 am »
lol, strtoint is compatible with Byte variables.

Offline Avarax

  • Veteran
  • *****
  • Posts: 1529
    • Official Hexer & MMod site
Re: type casting?
« Reply #2 on: June 15, 2007, 03:42:12 am »
function  InttoByte(IntValue: integer): byte;
var temp: string;
begin
  result:=0;
  If (IntValue<=255) and (IntValue>=0)  then begin
     temp :=inttostr(IntValue);
     result:=strtoint(temp);
  end;
end;



if enesce is right this should work ;P
I like to have one Martini
Two at the very most
Three I'm under the table
Four I'm under the host

Offline chrisgbk

  • Moderator
  • Veteran
  • *****
  • Posts: 1739
Re: type casting?
« Reply #3 on: June 15, 2007, 05:04:43 am »
As far as Pascal itself goes, you can do Byte(Value) to convert an integer to a byte, albeit with the risk of loss of precision etc.

Don't know if the script engine supports it though.

Offline zyxstand

  • Veteran
  • *****
  • Posts: 1106
  • Mother of all Bombs
Re: type casting?
« Reply #4 on: June 15, 2007, 02:56:08 pm »
well, Byte(int) works!  Is Single(int) also possible?  What conversions are possible?

Date Posted: June 15, 2007, 11:22:32 AM
hah i figured out the problem and it's as weird as ever:
I needed to convert to byte to make something appear somewhere specific on the map.
To check the location I took my player there and made the machine tell me the x and y of my location.
When I took my player there it said 43 for x.  I leave the spot move somewhere else and go back to the exact same spot and it suddenly becomes 51.  It always fluctuates between those two numbers and I'm accurately standing on the spot.  Then I realized that the directions change depending on what direction I'm facing so now I need to guess, test, repeat to make the object spawn where I want it to....
That's why I thought that byte(int) didn't work...

Date Posted: June 15, 2007, 11:33:18 AM
I also discovered that you can use an integer instead of a single for the x and y location in SpawnObject.  Thnx guys - mod seems to be coming along rather well, though I'm gonna need more custom maps... I've only made one - for testing purposes.
« Last Edit: June 15, 2007, 04:24:07 pm by zyxstand »
Can't think of anything original to put here...