+1 vote

Hi, for the use of a project I would like to create an extension of the type String (named Message) but I don't know how to do this. I know how to extends a Node but not a type and it doesn't seem to be as simply as "extends String" ;)

for example I would be able to do the following :

var msg : Message
msg = "GZKKI >IEKS"

var msg2 : Message
msg2 = msg.decode()

//msg2 = "HELLO WORLD"

where msg.decode is a method of the type message.

How to do this?

thanks in advance for your help !


in Engine by (21 points)

1 Answer

+2 votes
Best answer

I think it's not possible. Although if you insist, you can extend Resource:

class_name Message
extends Resource

var value:String

func _init(value):
    self.value = value

func decode():
    var decoded = value
    # decoding
    return decoded


var msg:Message = Message('GZKKI >IEKS')
var msg2 = msg.decode()
print(msg2) # HELLO WORLD ???
msg.value = 'assign to other value'

Alternatively, you can write a static decode function:

class_name Decoder

static func decode(value:String):
    # decoding
    return decoded_value


print(Decoder.decode('GZKKI >IEKS')) # HELLO WORLD ???
by (4,103 points)
selected by

Thanks for your response. I would have prefer to avoid static decode funciton but I think I won't have the choice...

Thanks ! :)

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.