[Closed] Structs with functions
This is the way I usually go about calling functions before they are declared in a struct. This happens rarely as I do try and put my functions in order.
struct my_struct
(
fn b =
(
my_struct.a()
),
fn a =
(
print "working"
)
)
my_struct = my_struct()
my_struct.b()
Then show us the way !
So far have not had any problems with this way so never had a reason too look for a different way, but I am open to it if you explain !
how does your method work for multiple instances of the same structure? you can say that you’ve never had a reason to use multiple instances but…
the solution has to be universal.
i’ve showed couple times on this forum how i handle this.
i don’t have max around so i wrote the code below without checking it.
struct ss
(
owner,
fn a = if isstruct owner do owner.b(),
fn b = format "%
" owner,
on create do owner = this
)
some one may say that i can use this directly. not always, not in any case.
Yeah I never had to create multiple instances of a struct being used for a tool (YET!), only cases I have instanced a struct was where I am filling an array with structs that only contain data.
Interesting what you have there, I am somewhat new about using THIS I see it a lot in python and some other programming languages maybe I should try using it more.
I tend to use:
struct ss
(
fn a = if isstruct this do this.b(),
fn b = format "%
" this
)
Can you please show a case, where it is not suitable denisT?
in some situations the using of THIS might be confusing. there are some samples: nested structures, CA in structure or structure in CA, scripted plugin in structure, etc.
struct test
(
owner,
someVar="Hello world",
fn someFn=
(
rollout testRoll "test"
(
button helloBtn "hello"
on helloBtn pressed do ( print owner.someVar )
)
),
fn showDialog=
(
createDialog ( owner.someFn() )
),
on create do ( owner=this )
)
a=test()
a.showDialog()
I use your method denis, but it didnt work inside rollout
struct test
(
someVar="Hello world",
fn someFn=
(
rollout testRoll "test"
(
local owner
button helloBtn "hello"
on helloBtn pressed do ( print owner.someVar )
)
),
fn showDialog=
(
local ro = someFn()
createDialog ro
ro.owner = this
)
)
a=test()
a.showDialog()
The rollout definition doesn’t know of the existence of owner.
You have to create an instance of the rollout first and then assign the value to this instance.
thanks mate, I didnt think of that
but we cannot call 2 rollout with same struct, though I will not use it yet I think
and happy holiday