5

Godot以太坊NFT游戏开发环境配置

 2 years ago
source link: http://blog.hubwiz.com/2022/02/06/godot-nft-dev/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

在这个教程里,我们将学习如何在Godot中建立NFT开发环境,使用的 是Moralis SDK,集成的web3钱包是Metamask。

Godot NFT游戏开发

用熟悉的语言学习 以太坊开发NFT实战 | Java | Php | Python | .Net / C# | Golang | Node.JS | Flutter / Dart

最近,我发现Chainsafe提供了适用于 Unity 游戏开发的NFT SDK,用来创建 Dapps 或 NFT 游戏非常棒。 然而,我个人喜欢 Godot 而不是 Unity,我认为有利有弊,但至少对于 2D 游戏,我个人偏好——所以我 决定整合Godot和Moralis,一个很棒的 Dapps 或 NFT 游戏平台。

1、新建Godot项目

打开 Godot,创建​​一个新项目。对于HTML5 游戏,推荐使用OpenGL ES 2.0:

godot nft游戏开发

接下来创建一个新场景并添加一个 Button 和 Label,然后保存。 此外,将按钮和示例文本都改为“Login”。

godot nft游戏开发

然后,为Godot游戏场景分配一个新脚本,并在Button中添加 pressed() 信号。

godot nft游戏开发

2、在Godot中导入Moralis SDK

在 Godot 中,转到项目 -> 导出… -> 添加… -> HTML5。

将以下代码添加到“Head Include”中:

<script src="https://cdn.jsdelivr.net/npm/web3@latest/dist/web3.min.js"></script>
<script src="https://unpkg.com/moralis/dist/moralis.js"></script>

godot nft游戏开发

3、Godot Moralis身份验证代码

为Godot游戏场景分配一个新的脚本文件并编写代码:

extends Node2D

# Moralis Server settings
export (String) var server_url = ""
export (String) var app_id = ""

# Signals emitting logged-in and logged-out for the Moralis
signal logged_in(user)
signal logged_out

# Moralis object
var moralis = JavaScript.get_interface("Moralis")
# Authenticated User
var current_user = null

# Callbacks
var _callback_login = JavaScript.create_callback(self, "_on_logged_in")
var _callback_logout = JavaScript.create_callback(self, "_on_logged_out")


func _ready():
if OS.has_feature("JavaScript") and !current_user:
# Initialize the Moralis SDK
var options = JavaScript.create_object("Object")
options.serverUrl = server_url
options.appId = app_id

moralis.start(options)


func login():
# Check if user already logged in
current_user = moralis.User.current()

if !current_user:
moralis.authenticate().then(_callback_login)


func logout():
moralis.User.logOut().then(_callback_logout)


func _on_logged_in(args):
current_user = args[0]

$Button.text = "Logout"
$Label.text = "Logged in!\nETH Address: " + args[0].get("ethAddress")


func _on_logged_out(args):
current_user = null

$Button.text = "Login"
$Label.text = "Logged out."


func _on_Button_pressed():
if current_user:
logout()
else:
login()

注意:以上代码使用了JavaScript,所以在不能使用JavaScript的环境下无法运行。

之后,在Godot游戏场景中 设置 Moralis 服务器的Server Url和App Id的值。

godot nft游戏开发

可以在Moralis 服务器详细信息中查看上述值,如果还没有的话,则需要创建 Moralis 帐户和服务器。

godot nft游戏开发

4、导出Godot项目的HTML文件

在Godot编辑器中单击项目 -> 导出… -> 导出项目。

在Godot项目文件夹中创建一个名为“html5”的新文件夹。文件名应该是“index.html”。 然后取消选中“带调试导出”:

godot nft游戏开发

顺便在Godot项目设置中将当前场景设置为主场景如下:

godot nft游戏开发

5、调试Godot NFT应用

最后,我们可以运行这个简单实现了 Moralis身份验证的游戏来验证Godot和Moralis的集成。 打开终端并输入以下内容:

cd <GODOT-PROJECT-FOLDER>/html5

python -m http.server 8000

在你的浏览器URL 栏中输入“localhost:8000”。然后应该看到登录屏幕:

godot nft游戏开发

当你单击“登录”按钮时,MetaMask 弹出窗口将打开以进行登录。 如果登录成功,示例文本将更改为你的 eth 地址。并且按钮的文本将变为“注销”。


原文链接:Integrate Godot and Moralis for NFT Games

汇智网翻译整理,转载请标明出处


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK