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

GDScript 2.0: Casting to Zero when using property #65675

Closed
Ajrarn777 opened this issue Sep 12, 2022 · 4 comments · Fixed by #73398
Closed

GDScript 2.0: Casting to Zero when using property #65675

Ajrarn777 opened this issue Sep 12, 2022 · 4 comments · Fixed by #73398

Comments

@Ajrarn777
Copy link

Ajrarn777 commented Sep 12, 2022

Godot version

4.0.16

System information

Windows 10

Issue description

When using a property and casting the value from an INT to a FLOAT, produces very strange result.

See to step to replicate.

It seems when we assign a property to a float, the internal variable type is corrupted.

Steps to reproduce

#===============================================================================
var Value:int = 0 :
	get:
		return Value
	set(v):
		Value = v
		
#===============================================================================
func Test():
	var f:float 
	var n:int 
	
	Value = 7
	f = Value
	n = int( f )   # Here : f = 7, and n = 0 ??? <<<<<<<<<<<<<<<<<<<<<<<< BAD !!!

	f = 4
	n = int( f )   # Here : f = 4, and n = 4 GOOD

Minimal reproduction project

No response

@Calinou Calinou added this to the 4.0 milestone Sep 12, 2022
@Calinou Calinou changed the title Casting to Zero when using property (godot 4) GDScript 2.0: Casting to Zero when using property Sep 12, 2022
@KoBeWi
Copy link
Member

KoBeWi commented Sep 12, 2022

prints(f, int(f)) prints 7 0 🤔
The bug only occurs if you use a getter.

@TheSofox
Copy link
Contributor

TheSofox commented Sep 13, 2022

Seems connected to Proposal 844 godotengine/godot-proposals#844

@anvilfolk
Copy link
Contributor

anvilfolk commented Feb 15, 2023

Digging a little into this one since I can still replicate it :)

It appears that there is no conversion happening between the int from the getter and the float f.

	f = Value
	print(typeof(f) == TYPE_INT)

if you ad this print statement, it will print true, which it shouldn't, because f is supposed to be a float.

Furthermore, in those conditions it appears that the constructor for the variant of type int might actually skip a few steps and forget to assign the value? I'm digging in a little more.

@anvilfolk
Copy link
Contributor

Ok, so I got a fix in, but I'm not convinced it's completely thorough.

Essentially getters were not their return address types in the compiler, meaning that the compiler wouldn't know to create an OPCODE_ASSIGN_TYPED_BUILTIN instead of an OPCODE_ASSIGN.

Somehow I still believe that it's possible to fool the compiler into creating an OPCODE_ASSIGN rather then the typed variant, which will result in the same issue: the float variant containing an int.

Which also doesn't solve problem number 2, which is that calling int() on something strange like a float variable with an int inside results in a zero.

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

Successfully merging a pull request may close this issue.

5 participants