NX OOP pattern

1

SP4CEBAR 2026-10-10 20:55 (Edited)

object oriented programming

Version 2 below is way better

version 1

(not recommended, use version 2 instead)

the structs array could also live in working ram

n_classes = 1
n_properties = 1
dim global structs(n_classes, n_properties), class_indexes(n_classes)

call my_class(0, 8,9)
call my_class(1, 1,2)

my_class_public_method(0, 5,6)

sub my_class(n, a, b)
  gosub register_class
  gosub load_class

  this_a = a
  this_b = b
  gosub my_private_method

  gosub save_class
end sub

my_private_method:
  print this_a
return

sub my_class_public_method(n, c, d)
  gosub load_class
  this_a = c
  this_b = d
  gosub my_private_method
  gosub save_class
end sub



'vendor

register_class:
  class_indexes(n) = -1
return

load_class:
  gosub validate_class_index
  this_a = structs(n, 0)
  this_b = structs(n, 1)
return

save_class:
  gosub validate_class_index
  structs(n, 0) = this_a
  structs(n, 1) = this_b
return

validate_class_index:
  if not class_indexes(n) then gosub throw_uninitislized_error
return

throw_uninitislized_error:
  error_class_unititialized=n/0
return


SP4CEBAR 2026-10-10 21:33 (Edited)

The above code has the issue that the variable names are fixed, the code below would be more practical

version 2


CALL MAIN
END

SUB MAIN
  n_properties = 3
  dim a(n_properties), b(n_properties)

  call myclass(a(), 8,9, 1,5)
  call myclass(b(), 1,2, 5,3)
  myclass_public_method(a(), 5,6)
END SUB



'class shape
sub shape(this(), x,y)
  RESTORE INDEXES
  gosub shape_properties
  gosub shape_construct
end sub

shape_properties:
  READ p_x
  READ p_y
return

shape_construct:
  this(p_x) = x
  this(p_y) = y
return
'end of shape



'class MYCLASS
sub myclass(this(), a, b, x, y)
  RESTORE INDEXES
  gosub myclass_properties
  gosub myclass_construct
end sub

myclass_properties:
  READ p_a
  READ p_b

  'aka "extends shape"
  gosub shape_properties
return

myclass_construct:
  this(p_a) = a
  this(p_b) = b
  gosub myclass_private_method

  'aka "super()"
  gosub shape_construct
end sub

myclass_private_method:
  print this(p_a)
return

sub myclass_public_method(this(), c, d)
  gosub myclass_properties
  this(p_a) = c
  this(p_b) = d
  gosub myclass_private_method
end sub
'END OF MYCLASS


'vendor

INDEXES:
0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20


SP4CEBAR 2026-10-10 22:01 (Edited)

This turned out way better than expected!

However by the time you need OOP to keep your project structurized you probably would soon run out of tokens anyway


SP4CEBAR 2026-10-10 22:26 (Edited)

version 2 - RAM

If you prefer RAM over arrays you could do

A = $A000
call myclass(A, 8,9)

'class MYCLASS
sub myclass(this, a, b)
  RESTORE INDEXES
  gosub myclass_properties
  gosub myclass_construct
end sub

myclass_properties:
  READ p_a
  READ p_b
return

myclass_construct:
  poke this+p_a, a
  poke this+p_b, b
end sub
'end of myclass


'vendor

INDEXES:
0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20


Log in to reply.