How can I access the instantiated function from the instantiated program?

How can I access the main script’s function from the instantiated script?

If I use “Main : main = main{}” in the script on the instantiated side to create an access route to the main side as shown below, a compilation error will occur.

shown error:
Linker task graph contains a cycle:
Versecompiler ingo: Unable to assemble “script name” (SDO instantiantion, etc,)

###############
Main Scripit
###############
main := class(creative_device):

    @editable
    Sub : sub = sub{}

    function1(Agent:agent):void=
        block:

###############
instantiated Script
###############
sub  := class<concrete>:

    Main : main = main{}

    function2(Agent:agent):void=
        Main.function1()

Ok I just found out what’s wrong, you’re basically creating an infinite loop from the start!

Since sub creates a main{} automatically and main creates a sub{}. The design is just not working :smiley:

If you want an access to main from sub, what you can do is using a setter.

In main OnBegin function :

Sub.SetMain(Self)

In sub :

var Main : ?main = false # this way we don't loop and it makes more sense also

SetMain(Main: main):void=
    set Main = option{Main}

Here you go :tada:

EDIT: You could also use weak_map to create a singleton of main that’s accessible from anywhere

Thank you for your promptness
I was running away from like ”Self” and not studying properly.
It seems difficult, but I will try to analyze it properly.

1 Like

Self returns the current instance of the class you’re calling it from. (also called an object in all programming languages)

In your case, calling Self inside the main class will return the actual main device. (which eventhough it’s called main, could be instanced many times, each one would be different but Self would return each of these instances every time it’s called)

But maybe I’m not clear enough, just lookup classes and objects in programming :+1:

1 Like

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