Call by reference or call by value

Hi to alll, I want to know when I have call by value and when call by reference.

For Example I have these four classes

Generator := class():

var Counter: float = 0.0

  CountUp():void=
    set Counter += 1

  GetCounter():float=
    return Counter
Meow := class():

  SingleGenerator : Generator = Generator{}
  MainCounter  : float = 0.0

  Init():void =
    Generator.CountUp()
    set MainCounter = Generator.GetCounter() # Not working as it is not a var MainCounter

MakeMeowClass<constructor>(GeneratorConstructor:Generator, MainCounterConstructor:float) := Meow :
    SingleGenerator := GeneratorConstructor
    MainCounter := MainCounterConstructor
Test := class():
  MainCounter : float = 0.0

MakeTestClass<constructor>(MainCounterConstructor:float) := Test :
    MainCounter := MainCounterConstructor

Manager := class()
  var Generator : Generator = Generator{}
  var MainCounter : float = 0.0

  Initiate():void = 
    set Meow = MakeMeowClass(Generator, MainCounter)

If I put in the Manager Constructor my Generator object found out that I have a call by reference, but the MainCounter is only call by value. How can I have a reference in the Meow class? Do I have to create a seperat class only for the MainCounter with functions such as Increase(value:float) or Decrease(value:float)? Is that event correct that I have a call by reference for the Generator inside the constructor?
I just want that the values of the MainCounter in the Test class and Meow class should be the same (just a reference) if I change it in one class

1 Like

In Meow Init() you gotta change your references from Generator to SingleGenerator, as Generator is referencing the class as a whole. Same in the Manager class, you have set Meow = but there is no initialization of Meow, and if you make a Meow named Meow you’ll get an ambiguous definition error. You’ll have to make a newMeow := MakeMeowClass(Generator, MainCounter)If you want to change all instances of a class you’ll have to store them in an array and iterate through.

Only objects can be and are passed by reference

Primitives, arrays, maps, and structs are always passed by value

Thank you! This is what I’ve been looking for. Is there something in the documentation about this? I couldn’t find anything there.
This means for me that I need a seperate class for the MainCounter to implement for example a global balance. So I can upgrade for example a building and remove the balance of the class or add balance at some generators

I’m not sure about the documentation but I feel like it’s how most high-level programing languages work?

It seems that you already had a class in your examples, which would either be the generator or the manager, you could pass this class along instead of making another one

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.