Coding your games with AI

Using Chat GPT (v4) to help write gaming code for beginners and beyond.

Chat GPT makes it easy to get guidance (ask questions) and help (writing sample codes) for anyone working on the popular gaming platforms for today. Examples which include Roblox (Roblox Studio & Lua), Minecraft (Minecraft XXX), Godot with its GDScript, Stencyl using ActionScript and Unity Game Engine with UnityScript,

Read below several examples showing how to query Chat GPT for some useful code to help building games such as Roblox, Minecraft etc. Full code samples will also be linked below showing the complete code to help build games.

====================================================================================================================================

Roblox (Lua)

Prompt used for this: "show me how to add a custom character in Roblox by writing code in Lua. Also add some code for a character that will hunt me down by following me"

Response from Chat GPT 4:

local character = Instance.new("Model") character.Name = "Character" character.Parent = game.Workspace local humanoid = Instance.new("Humanoid") humanoid.Parent = character local torso = Instance.new("Part") torso.Name = "Torso" torso.Size = Vector3.new(1, 2, 1) torso.Parent = character local head = Instance.new("Part") head.Name = "Head" head.Size = Vector3.new(1, 1, 1) head.Position = Vector3.new(0, 2, 0) head.Parent = character local limb = Instance.new("Part") limb.Name = "Limb" limb.Size = Vector3.new(0.5, 2, 0.5) limb.Parent = character local rootPart = torso rootPart.Name = "HumanoidRootPart" humanoid.RootPart = rootPart

====================================================================================================================================

Minecraft (Java)

Prompt used for this: "show me how to add a custom character in Minecraft by writing code in Java. Also add some code for a character that will hunt me down by following me"

Response from Chat GPT 4:

import net.minecraft.entity.EntityType; import net.minecraftforge.fml.RegistryObject; import net.minecraftforge.registries.DeferredRegister; import net.minecraftforge.registries.ForgeRegistries;

public class CustomEntities { public static final DeferredRegister<EntityType<? data-preserve-html-node="true">> ENTITY_TYPES = DeferredRegister.create(ForgeRegistries.ENTITIES, "your_mod_id");

public static final RegistryObject<EntityType> CUSTOM_ENTITY = ENTITY_TYPES.register("custom_entity", () -> EntityType.Builder.create(CustomEntity::new, EntityClassification.MONSTER) .size(0.6f, 1.95f) .build("custom_entity"));}

====================================================================================================================================

Godot (GDScript)

Prompt used for this: "show me how to add a custom character in Godot by writing code in GDScript. Also add some code for a character that will hunt me down by following me"

Response from Chat GPT 4:

extends KinematicBody2D var velocity = Vector2() func _physics_process(delta): velocity = move_and_slide(velocity)
func _process(delta): code to make the character follow the player goes here

====================================================================================================================================

Stencyl (ActionScript)

Prompt used for this: "show me how to add a custom character in Stencyl by writing code in ActionScript. Also add some code for a character that will hunt me down by following me"

Response from Chat GPT 4:

package { import org.flixel;

public class CustomCharacter extends FlxSprite { public function CustomCharacter(X:Number, Y:Number) { super(X, Y); // Load the sprite graphic loadGraphic(ImgCustomCharacter, true, true, 32, 32); // Set the bounding box width = 16; height = 16;offset.x = 8;offset.y = 16; // Add animations addAnimation("idle", [0]); addAnimation("walk", [1, 2, 3, 0], 8); } override public function update():void {super.update();} } }*

====================================================================================================================================

Unity Game Engine (UnityScript)

Prompt used for this: "show me how to add a custom character in Unity by writing code in UnityScript. Also add some code for a character that will hunt me down by following me"

Response from Chat GPT 4:

var target : Transform; // the target to follow (e.g. the player) var speed : float = 5.0; // the speed of the character function Start () { target = GameObject.FindWithTag("Player").transform; } function Update () { var direction = (target.position - transform.position).normalized; transform.position += direction speed Time.deltaTime; }

====================================================================================================================================

Unreal Engine

Prompt used for this: "show me how to add a custom character in Unreal Engine by writing code in C++. Also add some code for a character that will hunt me down by following me"

Response from Chat GPT 4:

*#include "CoreMinimal.h"

#include "GameFramework/Character.h"

#include "AIController.h"

#include "Perception/AISense_Sight.h"

#include "BehaviorTree/BehaviorTree.h"

#include "BehaviorTree/BehaviorTreeComponent.h"

#include "BehaviorTree/BlackboardComponent.h"

#include "HuntingCharacter.generated.h"

UCLASS() class MYPROJECT_API AHuntingCharacter : public ACharacter { GENERATED_BODY() public: AHuntingCharacter(); protected: virtual void BeginPlay() override; UPROPERTY(EditAnywhere, Category = "AI") class UBehaviorTree BehaviorTree; UPROPERTY(EditAnywhere, Category = "AI") class UAISenseConfig_Sight SightConfig; class UBlackboardComponent BlackboardComp; class UAIController AIController; public:
virtual void Tick(float DeltaTime) override; };

And code for hunting the player below

#include "HuntingCharacter.h"

#include "Engine.h"

AHuntingCharacter::AHuntingCharacter() { SightConfig = CreateDefaultSubobject<UAISenseConfig_Sight data-preserve-html-node="true">(TEXT("Sight Config")); SightConfig->SightRadius = 3000.0f; SightConfig->LoseSightRadius = 3500.0f; SightConfig->PeripheralVisionAngleDegrees = 90.0f; SightConfig->DetectionByAffiliation.bDetectEnemies = true; SightConfig->DetectionByAffiliation.bDetectNeutrals = true; SightConfig->DetectionByAffiliation.bDetectFriendlies = true; GetPerceptionComponent()->SetDominantSense(SightConfig->GetSenseImplementation()); GetPerceptionComponent()->OnPerceptionUpdated.AddDynamic(this, &AHuntingCharacter::OnPawnDetected); GetPerceptionComponent()->ConfigureSense(SightConfig); }

void AHuntingCharacter::BeginPlay() { Super::BeginPlay();

AIController = Cast<UAIController>(GetController());
BlackboardComp = AIController->GetBlackboardComponent();
BlackboardComp->SetValueAsVector(FName("PlayerLocation"), GetActorLocation());
RunBehaviorTree(BehaviorTree);

}

void AHuntingCharacter::Tick(float DeltaTime) { Super::Tick(DeltaTime); BlackboardComp->SetValueAsVector(FName("PlayerLocation"), GetActorLocation()); }

Previous
Previous

How to create an AI Chat Bot

Next
Next

Uneasy lies the head that wears a Crown*