Menu
CoddyTech

Script Loading

Lesson 36 of 48 in Coddy's GDScript for PHP Developers course.

PHP require_once evaluates a file once and exposes its declarations; __DIR__ provides a path relative to the current file. GDScript scripts are resources and classes. class_name registers a name, preload resolves a resource during parsing, and load resolves at runtime. res:// identifies the project root. Keep named helpers in their required files and avoid circular loading assumptions. Similar file splitting does not imply identical import semantics.

Complete PHP 8.3 program files (run main.php with the CLI):

PHP file Words.php:

<?php
declare(strict_types=1);
function greeting(string $name): string { return "Hi ".$name; }

PHP file main.php:

<?php
declare(strict_types=1);
require_once __DIR__."/Words.php"; echo greeting("Ada"),"\n";

PHP output:

Hi Ada

Complete GDScript example files:

GDScript file demo_words.gd:

class_name DemoWords
extends RefCounted
static func greeting(name: String) -> String:
	return "Hi " + name

GDScript file main.gd:

extends Node

func _ready() -> void:
	print(DemoWords.greeting("Ada"))

GDScript output:

Hi Ada
challenge icon

Challenge

Easy

Implement PathWords.suffix(text:String)->String returning text after its last dot, or none when no dot exists. Exercise uses the globally registered class. Empty suffix after a trailing dot is valid.

Input is one JSON array on stdin. The locked main parses it, calls the public APIs and prints result= followed by the returned text and a newline. No helper should print. Independent fixed probes verify required API behavior before the input case; they do not enforce implementation style.

Your code goes in path_words.gd, exercise.gd. main.gd holds the supplied input/output code and cannot be edited. Keep each class_name type in its named file.

Try it yourself

extends Node

func contract_ok(data: Array) -> bool:
	if PathWords.suffix("a.b.c") != "c" or PathWords.suffix("no") != "none": return false
	return true

func _ready() -> void:
	var data: Array = JSON.parse_string(OS.read_string_from_stdin())
	if not contract_ok(data):
		print("API_CONTRACT_FAILED")
		return
	print("result=" + Exercise.solve(data))
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in GDScript for PHP Developers

Practice on your own: Online GDScript compiler