Author Topic: How to implement IntFlags in VC  (Read 2968 times)

0 Members and 1 Guest are viewing this topic.

Aniv_D

  • Jr. Member
  • **
  • Posts: 42
  • Karma: 10
    • View Profile
How to implement IntFlags in VC
« on: December 07, 2020, 03:22:38 PM »
Hello, please tell me. here I have the code.
Code: [Select]
from enum import IntFlag

class status(IntFlag):
    Docked = 1
    Landed = 2
    LandingGearDown = 4
    ShieldsUp = 8
    SuperCruise = 16
    FlightAssistOff = 32
    HardpointsDeployed = 64
    InWing = 128
    LightsOn = 256
    CargoScoopDeployed = 512
    SilentRunning = 1024
    ScoopingFuel = 2048
    SrvHandbrake = 4096
    SrvUsingTurretView = 8192
    SrvTurretRetracted = 16384
    SrvDriveAssist = 32768
    FsdMassLocked = 65536
    FsdCharging = 31072
    FsdCooldown = 262144
    LowFuel = 524288
    OverHeating = 1048576
    HasLatLong = 2097152
    IsInDanger = 4194304
    BeingInterdicted = 8388608
    InMainShip = 16777216
    InFighter = 33554432
    InSrv = 67108864
    HudInAnalysisMode = 134217728
    NightVision = 268435456
    AltitudeFromAverageRadius = 536870912
    FsdJump = 1073741824
    SrvHighBeam = 2147483648


flags = status(16842765)

if status.Docked in flags:
    print("Docked found")
else:
    print("Docked not found")

if status.HasLatLong in flags:
    print("HasLatLong found")
else:
    print("HasLatLong not found")

if status.InMainShip in flags:
    print("InMainShip found")
else:
    print("InMainShip not found")

it works in Python 3.7.
how can this be implemented in IronPython in VC?
I tried to find a way on the Internet, but unfortunately I was not able to achieve success.

jitterjames

  • Administrator
  • Hero Member
  • *****
  • Posts: 7714
  • Karma: 116
    • View Profile
    • VoxCommando
Re: How to implement IntFlags in VC
« Reply #1 on: December 07, 2020, 06:35:55 PM »
Well first of all, VoxCommando uses IronPython which is limited to Python 2.7.

So first you need to be using code made for Python 2 not Python 3.  Sometimes the code is the same, sometimes there are differences.

But you also need the libraries.  "enum" is not a default library in Python 2.7.

Even then not everything in Python 2 necessarily works in IronPython.

If you can get your code working in Python 2 then you can probably use it in Voxcommando but you would need to copy the library files to the VoxCommando\plugins\py\Lip folder.

It's not so easy. ;P

I installed Python 2.7, then I installed enum (using pip) so I could test your code in Python 2.7.  If it worked then I would have tried to do it in VoxCommado.

But I get an error
Code: [Select]
Traceback (most recent call last):
  File "C:/Python27/test.py", line 3, in <module>
    class status(IntFlag):
NameError: name 'IntFlag' is not defined

So it seems that something has changed in enum for Python 2.7 that was not updated in enum for Python 2.7.

To summarise.  To bring code in from outside you first must be able to make it work in Python 2.7.  Then if it works you can hopefully get it to work in VoxCommando by copying the libraries over.

Aniv_D

  • Jr. Member
  • **
  • Posts: 42
  • Karma: 10
    • View Profile
Re: How to implement IntFlags in VC
« Reply #2 on: December 08, 2020, 04:57:48 PM »
thanks for the answer.
I tried putting the module 'enum34' py2, but it didn't have IntFlags... but that's okay. I did it a little differently and I'm happy with the result.
also, I tried
Code: [Select]
import clr
from System import Enum
....
but either there is no or (most likely) I just don't know how to use it. :D

jitterjames

  • Administrator
  • Hero Member
  • *****
  • Posts: 7714
  • Karma: 116
    • View Profile
    • VoxCommando
Re: How to implement IntFlags in VC
« Reply #3 on: December 09, 2020, 09:36:49 AM »
using this should "work": (meaning these 2 lines of code do not throw an error)
Code: [Select]
import clr
from System import Enum

But understand that when you do this you are importing from C# (this can only be done with IronPython nor regular Python).  So the way you use Enums will depend on how the Enum libraries in C# work.  You won't necessarily be able to drop Python code in and expect it to work.

But enums are not so important.  If you are writing your own code there are plenty of ways to accomplish the same thing.  The difficulties arise when trying to use someone else's code that has been written using Python 3 and various non-native libraries.