Skip to content

Latest commit

 

History

History
220 lines (146 loc) · 10.4 KB

Readme.md

File metadata and controls

220 lines (146 loc) · 10.4 KB

Docs - Discord - Roadmap - Template

DISCLAIMER

The plugin is being used to develop a Game but it isn't feature complete yet. To get started there is a Third Person Template implementation in NimForUE: https://github.com/jmgomez/NimTemplate

Why NimForUE?

The core idea is to have a short feedback loop while having native performance and a modern language that can access all features Unreal provides.

The plugin outputs native dynamic libraries (DLLs) that are hooked into UnrealEngine for development. When releasing the code is statically linked as published as an Unreal native plugin. This approach make the plugin portable and fast. It can run on any platform that Unreal supports (not all tested).

The design philosophy is to not change any unreal conventions on the API side of things, so anyone that knows Unreal can reuse its knowledge. The code guidelines follow (for the most part) the Nim conventions when not in contradiction with the Unreal ones.

If you have any question or wants to know more and follow the updates:

Join us on the Discord group

If you dont have Discord, you can also reach out at:

Twitter

Why Nim?

Nim is fast and easy to read with a good type system and a fantastic macro system, and it also has the best C++ interop in the industry.

The compiler is incredibly fast, and it's about to get faster with incremental compilation on the works.

The performance is the same as with C++ because you are outputting optimized C++ with zero overhead.

Fully control the memory if you so desire (including move semantics).

Nim Type System has everything (and probably more) that you can expect from a typed lang: generics, sum types, constraints on those, and it even has C++-like concepts (my personal favorite feature from C++) it even has hints of dependent types.

The macro system is outstanding. Just to give you an idea, await/async are implemented as a library. The same applies to Pattern Matching. This means (as you will see below) that you can create typed DSLs for your Unreal Projects that use the semantics that better fit your project.

Nim compile time capabilities are so good that we were able to rebuild UHT (Unreal Header Tool) at Nim's compile time.

The C++ interoperability makes this plugin stand out from the rest as you can consume any native API and implement virtual functions which is the way that Unreal extends most engine APIs.

NimForUE 101 Playlist:

NimForUE 101 Playlist

Showcase at NimConf 2022:

Showcase at NimConf 2022

Showcase GameFromScratch

Showcase GameFromScratch

Examples

The whole Cpp ThirdPersonTemplate in Nim would be like this:

uClass ANimCharacter of ACharacter:
  (Reinstance)
  (config=Game)
  uprops(EditAnywhere, BlueprintReadOnly, DefaultComponent, Category = Camera):
    cameraBoom : USpringArmComponentPtr 
  uprops(EditAnywhere, BlueprintReadOnly, DefaultComponent, Attach=(cameraBoom, SpringEndpoint), Category = Camera):
    followCamera : UCameraComponentPtr
  uprops(EditAnywhere, BlueprintReadOnly, Category = Input):
    defaultMappingContext : UInputMappingContextPtr
    (jumpAction, moveAction, lookAction) : UInputActionPtr

  defaults: # default values for properties on the cdo
    capsuleComponent.capsuleRadius = 40
    capsuleComponent.capsuleHalfHeight = 96
    bUseControllerRotationYaw = false
    characterMovement.jumpZVelocity = 700
    characterMovement.airControl = 0.35
    characterMovement.maxWalkSpeed = 500
    characterMovement.minAnalogWalkSpeed = 20
    characterMovement.brakingDecelerationWalking = 2000
    characterMovement.bOrientRotationToMovement = true
    cameraBoom.targetArmLength = 400
    cameraBoom.busePawnControlRotation = true
    followCamera.bUsePawnControlRotation = true

  #Notice this override an actual C++ function that exists in the base type. 
  proc setupPlayerInputComponent(playerInputComponent : UInputComponentPtr) {.virtual, override.}  =    
    let pc = ueCast[APlayerController](self.getController())
    if pc.isNotNil():
      let inputComponent = ueCast[UEnhancedInputComponent](playerInputComponent)
      let subsystem = tryGetSubsystem[UEnhancedInputLocalPlayerSubsystem](pc).get()
      subsystem.addMappingContext(self.defaultMappingContext, 0)
      inputComponent.bindAction(self.jumpAction, ETriggerEvent.Triggered, self, n"jump")
      inputComponent.bindAction(self.jumpAction, ETriggerEvent.Completed, self, n"stopJumping")
      inputComponent.bindAction(self.moveAction, ETriggerEvent.Triggered, self, n"move")
      inputComponent.bindAction(self.lookAction, ETriggerEvent.Triggered, self, n"look")
      
  ufuncs:
    proc move(value: FInputActionValue) = 
      let 
        movementVector = value.axis2D()
        rot = self.getControlRotation()
        rightDir = FRotator(roll: rot.roll, yaw: rot.yaw).getRightVector()
        forwardDir = FRotator(yaw: rot.yaw).getForwardVector()
      self.addMovementInput(rightDir, movementVector.x, false) 
      self.addMovementInput(forwardDir, movementVector.y, false) 

    proc look(value: FInputActionValue) =
      let lookAxis = value.axis2D()
      self.addControllerYawInput(lookAxis.x)
      self.addControllerPitchInput(lookAxis.y)

uClass ANimGameMode of AGameModeBase:
  proc constructor(initializer: FObjectInitializer) = #Similar to default but allows you to write full nim code
    let classFinder = makeClassFinder[ACharacter]("/Game/ThirdPerson/Blueprints/BP_ThirdPersonCharacter")
    self.defaultPawnClass = classFinder.class

This code can be found at src/examples/actorexample. There are more examples inside that folder. You can do import examples/example in from game.nim (see the NimTemplate) to play with it.

#Nim UClasses can derive from the same classes that blueprints can derive from.

uClass AExampleActor of AActor:
    (BlueprintType, Blueprintable) #Class specifiers follow the C++ convention. 
    uprops(EditAnywhere, BlueprintReadWrite): #you can declare multiple UPROPERTIES in one block
        exampleValue : FString #They are declare as nim properties. 
        anotherVale : int #notice int in nim is int64. while in c++ it is int32.
        anotherValueInt32 : int32 #this would be equivalent to int32
        predValue : FString = "Hello" #you can assign a default value to a property.
        predValueInt : int =  20 + 10 #you can even use functions (the execution is deferred)
        nameTest : FString = self.getName() #you can even use functions within the actor itself. It is accessible via this or self.

#In general when using the equal symbol in a uClass declaration, a default constructor will be generated.
#you can specify a custom constructor if you want to by defining a regular nim function and adding the pragma uconstructor

proc myExampleActorCostructor(self: AExampleActorPtr, initializer: FObjectInitializer) {.uConstructor.} =
    UE_Log "The constructor is called for the actor"
    self.anotherVale = 5
    #you can override the values set by the default constructor too since they are added adhoc before this constructor is called.
    self.predValue = "Hello World"

#Notice that you rarelly will need to define a custom constructor for your class. Since the CDO can be set within the DSL. 

#UFunctions

#UFunctions can be added by adding the pragma uFunc, and for each meata, another pragma:
#Since in nim functions are separated from the type they are declared in, you need to specify the type as the first argument.

proc myUFunction(self: AExampleActorPtr, param : FString) : int32 {. ufunc, BlueprintCallable .} = 
    UE_Log "UFunction called"
    5

#You can also use the uFunctions macro to declare multiple uFunctions at once. The preferred way is still to use them in an uClass block like shown above.
uFunctions:
    (BlueprintCallable, self:AExampleActorPtr) #you must specify the type and any shared meta like this.

    proc anotherUFunction(param : FString) : int32 = 10 #now you can define the function as you normally would.
    proc yetAnotherUFunction(param : FString) : FString = 
        self.getName() #you can access to the actor itself by the name you specify in the uFunctions macro.
    
    proc customPragma(param : FString) : int32 {. BlueprintPure .} = 10 #you can also specify custom pragmas per functions rather than creating a new block

    proc callFromTheEditor() {. CallInEditor .} = 
        UE_Log "Call from the editor"
        

Which produces: Blueprint

Install

  1. Clone the repo inside YourGame/Plugins
  2. Run nimble nuesetup from YourGame/Plugins/NimForUE

A video showcasing the installation process in more detail:

Installing NimForUE video

VSCode setup

For Intellisense from nimsuggest, install the nim-lang.org extension.

When setting up a project:

  • Create a new workspace (it can be in the UE project folder).
  • File>Add Folder to Workspace:
    • The NimForUE directory that contains game.nim
    • The NimForUE plugin directory.
    • The imported directory in NimForUE plugin directory i.e .\src\nimforue\unreal\bindings\imported

Debugging in Visual Studio

Compile your game with the debug flag, nue game --debug. Then you can debug the game with nim source maps in Visual Studio.

To switch the project's target configuration use the targetconfig task.

To debug the engine along with your project, use nue targetconfig --debug and nue targetconfig --dev to switch back to Development. This will modify the NimForUE json config file and recompile the plugin and game dll.

Troubleshooting

Windows

  • The project path must not be too long, the default location C:/Users/<USER>/Unreal Projects is too long for instance.

You can move the "Unreal Projects" folder to the root of a drive and symlink it back to the user document directory if needed by other process / applications.