Logo
  • Features
  • Educators
  • What's New
  • Blog
  • Showcase
  • About us
  • Contact
Try it now!
Python In Gandi Demo

Python In Gandi Demo

Introduction

The introduction of Python by Gandi aims to assist in Scratch creation rather than replace Scratch. Therefore, Gandi Python itself does not have the ability to run independently. All Python functions or data calls can only be accessed through Scratch blocks. Python's capabilities in data processing far exceed those of Scratch. Introducing Python can effectively enhance users' efficiency in data processing during the process of creating Scratch projects.

For example, a comparison between implementing bubble sort in Scratch and implementing bubble sort in Python:

image
image

Basic Usage Part 1

In this section, we will introduce the functionality of using a Python program to read JSON files and then accessing JSON data through Scratch blocks. Steps:

  1. Define a JSON file and fill it with data.
💡
Python text editors can create JSON files, which can be directly read by Python. For example, the data.json file here can be read in Python by read('data.json').read(). Generally, in large and complex game projects, there will be some configuration information (such as level configuration, combat data, dialogue scripts, etc), based on this file system, it is convenient to write these configurations to JSON files, and then read them through Python and pass the results to Scratch. This is more intuitive and convenient than the previous common practice in Scratch (writing to Data or using a large variable).
  1. Load the JSON file and process the data through a Python program.
💡
Tips1: The execution of Python scripts runs throughout the game's lifecycle (from clicking the green flag to the end of the game), always in a runtime environment. This means that you can use Python global variables to maintain state. For example, in the sample file, data is a global variable, and each data set through kv_set is saved in the global data, which can be accessed by kv_get in different characters. With the runtime state, combined with Python's rich data types and standard library, it is easy to manage some runtime data in the game (player data, round battle data, cache data, etc.) and perform some complex processing (calculation, sorting, statistics, etc). During editing testing, you can manually clear the runtime context by using the "Compile and Reset Variables" button in the Python plugin.
💡
Tips 2: Gandi Python has built-in almost all standard libraries, which can be imported and used normally, but currently does not support third-party libraries. The development team will gradually support them in the future.
  1. Generate Scratch blocks to call Python functions.

Demonstration Video

Basic Usage Part 2

Implement a bubble sort and use Scratch blocks to sort an unordered list. Steps:

  1. Add your own custom function bubble_sort in the Python editor, and add the function name to the __all__ variable.
💡
Tips1: Since Scratch only supports a limited number of types, while Python has a rich type system, automatic type conversion is required during data transfer between the two languages.

Here are the conversion rules:

Python types
Scratch types
str
String
int/float
Number
bool
Boolean
list/set/tuple
List

Based on the supported data mapping relationships above, it is required that the parameter and return value types declared in Python can only be the types supported above. If a type that is not supported or has not been declared is encountered, it will be treated uniformly as (str/String).

💡
Tips2: In Python programs, __all__ is a special variable used to define the symbols (variables, functions, classes, etc.) exported in a module. When a module is imported, the all variable is used to specify which symbols will be exported, defining the module's public interface. Only functions exported through all can be called by external Scratch.
  1. Click on the "Refresh blocks" button in the Python blocks category to load newly defined Python functions and convert them into blocks.
💡
Tips: It's not just about adding new functions. Whenever there's a change in the definition of Python functions or any code logic, you need to "Refresh blocks" to synchronize the latest Python logic with Scratch.
  1. Define an unordered list and sort it by calling the bubble sort block.

Demonstration Video

Other Features

  1. Integrated AI capability for automatic interpretation and code generation.
image
  1. Debugging and Console Output.
image
💡
If you have any questions or suggestions, you can find us in the discord server: https://discord.gg/U3nyveCvRa

Logo

Designed by 2ndR with love

Discord