0 votes

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 !

Simon

asked Jun 14, 2019 in Engine by simLC (20 points)

1 Answer

+1 vote
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

Usage:

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

Usage:

print(Decoder.decode('GZKKI >IEKS')) # HELLO WORLD ???
answered Jun 14, 2019 by Dlean Jeans (3,911 points)
selected Jun 14, 2019 by simLC

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.