Godot i wanna get random kirby images from the web

Godot Version

v3.5.3.stable

Question

I want to get all the web images containing Kirby in the form of a URL and display them randomly to the user.

Code Right Now

The variables are stored in an array.

var kirby_images:Array = [
	"https://upload.wikimedia.org/wikipedia/en/2/2d/SSU_Kirby_artwork.png",
	"https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcS6HmPYDbnjdfewJHL0D1-t1HgrT4OBIQQ-Lw&s",
	"https://static1.srcdn.com/wordpress/wp-content/uploads/2017/06/Kirby-Nintendo.jpg",
	"https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcS5mTfCQ1kL8d5f4o9sAFr8f8aqJOAO_8SK2xQVPGFCbw&s",
	"https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRJaX8F8wr8euOW_SbPbRxR3XgVuvCuhgmHfWnOmSxMeA&s",
	"https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRj7to_2cV-WE9GaXkW7agVaERmdpD-iRtRs_p7KHM1jQ&s",
	"https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSqJx3AYZPxQsqRdy3j8a6ZIZsFrgV2IlsrPvIFt1iL3A&s",
	"https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSjwom3DLjzhEhVgALKLhh1e-ZR7lY4SaFgbeLqdMRN7g&s",
	"https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQPfrk8MQqy9OpHa7iTxIezuPRQEL_VQDqTUw&s",
	"https://www.reddit.com/media?url=https%3A%2F%2Fpreview.redd.it%2Fthe-most-powerful-creature-kirbo-v0-xr00qsfvqb4b1.png%3Fauto%3Dwebp%26s%3D2023564da99051fc49bee6c7bddcdd32ec817810"
]

Then I get a random image by

var rand_no = randi() % kirby_images.size()
kirby_images[rand_no]

According to this: HTTPRequest — Godot Engine (3.2) documentation in English, you can make an HTTP(s) request then use the result with load_png_from_buffer.

I am really new to godot i just want some sort of library which contains these images. I am using discord.gd to build a discord bot the problem is not showing the image but getting a ton of images.

var rand_no = randi() % kirby_images.size() 

embed.set_image(kirby_images[rand_no])

			
bot.send(msg, {
  "embeds": [embed]
})
			

You could send a HTTP(s) GET request to a web scraper like this one: Google Images API - SerpApi then parse the results and pick a random image from the results.

2 Likes

Nice, i think this will do the trick.

1 Like

Hello I do not know how to get or extract data, as I said I have no experience in godot so do u have any tutorials or documentation lying around

Some useful docs:
HTTPRequest — Godot Engine (3.2) documentation in English
JSON — Godot Engine (3.2) documentation in English
Google Images API - SerpApi

Summing up the documentation:

  • use HTTPRequest.new() to create a HTTP(s) request node and add_child() to add it to your scene
  • my_https_node.connect("request_completed", self, "a_result_function") will call a function once it completes (in this case the place holder “a_result_function”)
  • the function declaration should look something like this: func a_result_function(result, response_code, headers, body):
  • you can use parse_json() to the parse the result string into a JSON object
    like this: var response = parse_json(body.get_string_from_utf8())
  • SerpApi can used like this https://serpapi.com/search.json?q=Kirby&engine=google_images&ijn=0&api_key=YOURKEY replace YOURKEY with a private SerpApi key. You can currently get a key for free.
1 Like