UE4入门序列04(UE中的网络使用,C++及蓝图举例)

本文深入探讨Unreal Engine的网络框架,包括监听服务器与专用服务器的区别,详细解析属性同步、通知方式、Actor同步以及RPC通信在C++和蓝图中的应用,旨在帮助开发者理解并掌握UE4中的网络使用。
摘要由CSDN通过智能技术生成

使用Unreal引擎制作游戏或VR产品的时候,需要遵循Unreal引擎已经约定好的框架;
Unreal引擎非常复杂,本人是基于Application的应用层面来使用Unreal封装好的功能;
在制作一款联网端游的时候遇到了网络的问题,特此记录一下,个人的经验分享!

https://docs.unrealengine.com/en-US/InteractiveExperiences/Networking/index.html


#1 Unreal 的框架简介
#2 Unreal 中的网络框架
#3 Unreal 属性同步,蓝图和C++
#4 Unreal 属性同步,Notify方式
#5 Unreal 中基于Actor同步
#6 Unreal 中使用RPC同步


#1 Unreal 的框架简介
Unreal中默认使用的框架在GameMode的设置中列举出来了
在这里插入图片描述

  1. GameModeOverride 游戏模式
    The GameModeBase defines the game being played. It governs the game rules, scoring, what actors
    are allowed to exist in this game type, and who may enter the game.
    It is only instanced on the server and will never exist on the client.
    A GameModeBase actor is instantiated when the level is initialized for gameplay in C++ UGameEngine::LoadMap().
    The class of this GameMode actor is determined by (in order) either the URL ?game=xxx,
    the GameMode Override value set in the World Settings, or the DefaultGameMode entry set in the game’s Project Settings.
    简介:每个关卡都可以设置一个游戏模式
    作用:设置游戏的规则
    网游:只存在于服务器中
    单机:每个关卡都有

  2. Default Pawn Class 默认的控制
    Pawn is the base class of all actors that can be possessed by players or AI.
    They are the physical representations of players and creatures in a level.
    简介:游戏默认的操作控制
    作用:封装好了游戏的各种控制
    网游:由服务器复制到各个客户端
    单机:自由控制切换

  3. HUD Class 游戏UI
    Base class of the heads-up display. This has a canvas and a debug canvas on which primitives can be drawn. It also contains a list of simple hit boxes that can be used for simple item click detection.
    A method of rendering debug text is also included.Provides some simple methods for rendering text, textures, rectangles and materials which can also be accessed from blueprints.
    简介:游戏战斗时的UI
    作用:可以绘制各种UI,血条、伤害数字等
    网游:和服务没有关系
    单机:自由控制显示

  4. Player Controller Class 玩家控制器
    PlayerControllers are used by human players to control Pawns.
    ControlRotation (accessed via GetControlRotation()), determines the aiming
    orientation of the controlled Pawn.
    In networked games, PlayerControllers exist on the server for every player-controlled pawn,
    and also on the controlling client’s machine. They do NOT exist on a client’s
    machine for pawns controlled by remote players elsewhere on the network.
    简介:游戏玩家的控制器
    作用:设置游戏的输入
    网游:服务器及客户端同步
    单机:每个关卡都有

  5. Game State Class 游戏状态
    GameState is a subclass of GameStateBase that behaves like a multiplayer match-based game.
    It is tied to functionality in GameMode.
    简介:游戏进行的状态
    作用:管理当前游戏的状态
    网游:服务器及客户端同步
    单机:每个关卡都有

  6. Player State Class 玩家状态
    A PlayerState is created for every player on a server (or in a standalone game).
    PlayerStates are replicated to all clients, and contain network game relevant information about the player, such as playername, score, etc.
    简介:游戏玩家的状态
    作用:管理当前玩家的状态
    网游:服务器及客户端同步
    单机:每个关卡都有

  7. Spectator Class 观战控制
    SpectatorPawns are simple pawns that can fly around the world, used by
    PlayerControllers when in the spectator state.
    简介:游戏玩家观战
    作用:控制当前玩家的观战
    网游:和服务没有关系
    单机:一般来说不需要

在这里插入图片描述
在这里插入图片描述


#2 Unreal 中的网络框架

Unreal中的服务器分为两种:监听服务器(局域网对战)和专用服务器(独立云服务器),两种服务器的游戏在制作的过程中差不多,打包的方式不一样;
制作测试的时候可以使用Unreal提供的便捷功能:
在这里插入图片描述
可以设置测试开启的玩家数量和网络模式

  1. Play Offline 单机运行
  2. Play As Listen Server 监听服务器
  3. Play As Client 专用服务器(默认会开启一个服务器)
    其中学习的时候使用监听服务器,可以区别服务器和客户端,使用HasAuthority()方法;
    项目使用专用服务器,开启的全是客户端。

#3 Unreal 属性同步,蓝图和C++
这种同步的属性无论是否有变化,数据一直在同步,比较耗网络资源

  • 蓝图同步设置,基于Actor中的属性(必须要到Actor层级的继承才有网络同步)
    在这里插入图片描述
  • 创建的属性需要设置为复制,表示这个变量是Server所有权
    在这里插入图片描述
  • 蓝图测试,Actor和关卡蓝图,Replicated的变量有特殊标志
    在这里插入图片描述
  • 关卡蓝图测试
    在这里插入图片描述
    以下是C++中使用属性复制功能:
  • 创建C++ Actor类
// HowTo_ReplicateProps.h

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "HowTo_ReplicateProps.generated.h"

UCLASS()
class TUTORIALSDEMO_API AHowTo_ReplicateProps : public AActor
{
   
	GENERATED_BODY()
	
public:	
	// Sets default values for this actor's properties
	AHowTo_ReplicateProps();

protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;

public:	
	// Called every frame
	virtual void Tick(float DeltaTime) override;

	//Step1. 标记为Replicated
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值