Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integer to string casting and more string blocks #351

Open
DoomTas3r opened this issue Jan 2, 2025 · 2 comments
Open

Integer to string casting and more string blocks #351

DoomTas3r opened this issue Jan 2, 2025 · 2 comments

Comments

@DoomTas3r
Copy link
Contributor

Code_It_All made this video using version 0.8.0, but could not cast an integer to a string.

Perhaps make all string parameters accept only strings or improve implicit casting
Parser Error: Value of type "int" cannot be assigned to a variable of type "String".
image

A Text category could be added for these string-related blocks:

  1. (float) to string
  2. length of (string)
  3. letter (integer) of (string)

And maybe more complex blocks:

  1. replace (string1) with (string2) in (string3)
  2. slice (integer) of (string1) split by (string2)

And finally, the concat block's label could change to differentiate from the add block, for example: join (string1) (string2)

The current concat block allows both strings and integers to be added, but it compiles to string + integer, which produces an error
Parser Error: Invalid operands "String" and "int" for "+" operator.
image

manuq added a commit that referenced this issue Jan 6, 2025
The types relationship (previously named cast_relationships) tell which
type can be used in the context of another type. For example, integers
and floats are exchangeable. And in particular, all other types can be
used in boolean context: `if not Color.BLACK:` is allowed in GDScript as
Color.BLACK is falsey. Same for the empty string, zero number, etc.

This fixes a bug where an integer or float value block was allowed to be
snapped into a string slot.

This removes outdated custom casting code that has been abandoned.

#351
@manuq
Copy link
Contributor

manuq commented Jan 6, 2025

Code_It_All made this video using version 0.8.0, but could not cast an integer to a string.

First off, let's fix the bug. PR #352 addresses the issue seen in the video.

On top of that, more text blocks would be welcome. Here is my attempt at "to string" which works with integers and floats. Ideally we would allow any type in its slot. I guess we could make the parameter type optional in display_template to allow any type: {value} to string instead of {value: INT} to string.

[gd_resource type="Resource" load_steps=2 format=3 uid="uid://d2eq3sdk61wkk"]

[ext_resource type="Script" path="res://addons/block_code/code_generation/block_definition.gd" id="1_g3t5i"]

[resource]
script = ExtResource("1_g3t5i")
name = &"to_string"
target_node_class = ""
description = "Format the value to String.

Advanced: A format string can be specified."
category = "Log"
type = 3
variant_type = 4
display_template = "{value: INT} to string | with format {format: STRING}"
code_template = "{format} % {value}
"
defaults = {
"format": "\"%s\""
}
signal_name = ""
scope = ""
is_advanced = true

@DoomTas3r
Copy link
Contributor Author

@manuq Based on your attempt, here's my version, which is closer to what I envisioned:

Image

I like the idea of conveniently accepting any type, and in contrast, my version can limit what type can be accepted. I don't know if there would be a reason for that, which is why I'm leaning towards your version.

Here's the branch with my version if you want to look at it:

https://github.com/DoomTas3r/changes-block-coding/tree/to-string-block

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants