bcc icon indicating copy to clipboard operation
bcc copied to clipboard

Structs-Field can not be modified via Struct-Methods

Open GWRon opened this issue 11 months ago • 1 comments

I tried to manipulate/modify a struct field from within (in my case to retrieve current max2d stuff like resolution, scale, ...) but somehow it is is only visible "inside" the method

SuperStrict
Framework Brl.StandardIO

Struct STest
	Field x:Int
	
	Method Fill()
		Local newX:Int
		FillX(newX)
		x = newX
		Print "Fill() x="+x
	End Method

	Method Modify()
		x = 30
		Print "Modify() x="+x
	End Method

End Struct

Function FillX(value:Int Var)
	value = 20
End Function


Local tests:STest[10]
tests[0].Fill()
Print "tests[0].x = " + tests[0].x
tests[0].Modify()
Print "tests[0].x = " + tests[0].x

Print "Set x=50"
tests[0].x = 50
Print "tests[0].x = " + tests[0].x

Output

Fill() x=20
tests[0].x = 0
Modify() x=30
tests[0].x = 0
Set x=50
tests[0].x = 50

GWRon avatar Jan 20 '25 22:01 GWRon

Seems it only happens for arrays. When using a single struct, it works as expected:

Local test:STest
test.Fill()
Print "test.x = " + test.x
test.Modify()
Print "test.x = " + test.x

Print "Set x=50"
test.x = 50
Print "tests[0].x = " + test.x

Output

Fill() x=20
test.x = 20
Modify() x=30
test.x = 30
Set x=50
tests[0].x = 50

Edit - it seems as if the array-one COPIES the struct instead of referencing it (I marked the line)

Single struct C code:

	if (!_bb_main_inited) {
		_bb_main_inited = 1;
		__bb_brl_blitz_blitz();
		__bb_brl_standardio_standardio();
		bbObjectRegisterStruct((BBDebugScope *)&_m_untitled2_STest_scope);
		struct _m_untitled2_STest bbt_test=_m_untitled2_STest_New_ObjectNew();
		__m_untitled2_STest_Fill_v((struct _m_untitled2_STest*)&bbt_test);
		brl_standardio_Print(bbStringConcat(((BBString*)&_s2),bbStringFromInt(bbt_test.__m_untitled2_stest_x )));
		__m_untitled2_STest_Modify_v((struct _m_untitled2_STest*)&bbt_test);
		brl_standardio_Print(bbStringConcat(((BBString*)&_s2),bbStringFromInt(bbt_test.__m_untitled2_stest_x )));
		brl_standardio_Print((BBString*)&_s3);
		bbt_test.__m_untitled2_stest_x =50;
		brl_standardio_Print(bbStringConcat(((BBString*)&_s4),bbStringFromInt(bbt_test.__m_untitled2_stest_x )));
		return 0;
	}

Array struct C Code:

	if (!_bb_main_inited) {
		_bb_main_inited = 1;
		__bb_brl_blitz_blitz();
		__bb_brl_standardio_standardio();
		bbObjectRegisterStruct((BBDebugScope *)&_m_untitled2_STest_scope);
		BBARRAY bbt_tests=bbArrayNew1DStruct__m_untitled2_STest(10);
---- here it copies the struct instead of referencing
		struct _m_untitled2_STest bbt_=((struct _m_untitled2_STest*)BBARRAYDATA(bbt_tests,1))[0U];
-------
		__m_untitled2_STest_Fill_v((struct _m_untitled2_STest*)&bbt_);
		brl_standardio_Print(bbStringConcat(((BBString*)&_s2),bbStringFromInt(((struct _m_untitled2_STest*)BBARRAYDATA(bbt_tests,1))[0U].__m_untitled2_stest_x )));
		struct _m_untitled2_STest bbt_2=((struct _m_untitled2_STest*)BBARRAYDATA(bbt_tests,1))[0U];
		__m_untitled2_STest_Modify_v((struct _m_untitled2_STest*)&bbt_2);
		brl_standardio_Print(bbStringConcat(((BBString*)&_s2),bbStringFromInt(((struct _m_untitled2_STest*)BBARRAYDATA(bbt_tests,1))[0U].__m_untitled2_stest_x )));
		brl_standardio_Print((BBString*)&_s3);
		((struct _m_untitled2_STest*)BBARRAYDATA(bbt_tests,1))[0U].__m_untitled2_stest_x =50;
		brl_standardio_Print(bbStringConcat(((BBString*)&_s2),bbStringFromInt(((struct _m_untitled2_STest*)BBARRAYDATA(bbt_tests,1))[0U].__m_untitled2_stest_x )));
		return 0;
	}

That line

		struct _m_untitled2_STest bbt_=((struct _m_untitled2_STest*)BBARRAYDATA(bbt_tests,1))[0U];

should be

		struct _m_untitled2_STest* bbt_ptr = &((struct _m_untitled2_STest*)BBARRAYDATA(bbt_tests, 1))[0U];

(I am sure BCC just needs to mark it approbriately).

GWRon avatar Jan 20 '25 23:01 GWRon