LoginSignup
3
1

More than 5 years have passed since last update.

カスタムブループリントクラスをNewする #UE4Study

Last updated at Posted at 2017-03-06

ブループリントクラスをNewする

カスタムブループリントクラスがNewできない

Actorクラスを継承していれば、SpawnActor From Classで動的に生成できるが、処理だけを描いたカスタムブループリントをブループリント内で動的に作成したい。

ソースコードで記述すると以下のような書き方でカスタムブループリントクラスをNewしたい。
BP_Object bp = new BP_Object();

一通りNew出来そうな単語で検索したが見つからなかった
image

AnswerHubにそれらしき対応方法を見つけたので試してみた

Instantiating Instance of Blueprint Class
https://answers.unrealengine.com/questions/206017/instantiating-instance-of-blueprint-class.html

Blueprint Node: Create Object from Blueprint
https://wiki.unrealengine.com/Blueprint_Node:_Create_Object_from_Blueprint

ObjectクラスをNewするC++ BlueprintFunctionを作成する

File -> New C++ Class...
image

Show All Classes -> BlueprintFunctionLibraryを選択 -> Next
image

image

BPCreator.h
// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "Kismet/BlueprintFunctionLibrary.h"
#include "BPCreator.generated.h"

/**
 * 
 */
UCLASS()
class BP_CPP_API UBPCreator : public UBlueprintFunctionLibrary
{
    GENERATED_BODY()

    UFUNCTION(BlueprintCallable, Category = "Object")
    static void Create(UClass *ObjectClass, UObject* &CreatedObject);

};

BPCreator.cpp

// Fill out your copyright notice in the Description page of Project Settings.

#include "BP_CPP.h"
#include "BPCreator.h"

 void UBPCreator::Create(UClass *ObjectClass, UObject* &CreatedObject)
 {
     //NewNode = new ObjectType();
     CreatedObject = NewObject<UObject>((UObject*)GetTransientPackage(), ObjectClass);
 }


ブループリント:BP_Objectを作成する

Blueprint Classを作成する
image

親クラスにObjectを選択する
image

名前をBP_Objectに設定する
image

変数を追加する

変数名 変数の型 デフォルト値
Name String CustomBPClass

image

カスタムブループリントクラスをブループリントでNewすることができる

レベルブループリントにBP_Objectを動的に作成するコードを記述する
image

変数に設定したデフォルト値が表示できた
image

3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1