A means of accessing user variables from unrelated classes

Post a reply

Smilies
:D :) :( :o :shock: :? 8-) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :wink: :geek: :ugeek: :!: :?: :idea: :arrow: :| :mrgreen: :3: :wub: >:( :blergh:
View more smilies

BBCode is OFF
Smilies are ON

Topic review
   

Expand view Topic review: A means of accessing user variables from unrelated classes

Re: A means of accessing user variables from unrelated class

by Kzer-Za » Mon May 25, 2020 5:03 am

Because I went away from modding for some time and didn't need to update until now. And now I can't update GZdoom due to some compilation errors, to get rid of which I would need to update my system, which I can't because it's Manjaro 32-bit (which has not received update for several months), so I would need to move to 64-bit, which I can't because I currently can't afford to break a working environment that I need for work.

So for now I'm gonna just be fiddling with things that do not require the latest features. It's nice to know anyway that they are there when I update :)

Re: A means of accessing user variables from unrelated class

by Rachael » Mon May 25, 2020 4:46 am

Why are you using 4.1.3?

We can't help you if you refuse to update to the latest version of GZDoom.

Re: A means of accessing user variables from unrelated class

by Kzer-Za » Mon May 25, 2020 2:06 am

Ah, sorry, I'm not a programmer and know little about such things. What I learned about programming was mostly during trying to fiddle with GZDoom mods :) It seems that mixins are indeed what I'm looking for.

PS Were they introduced recently? I'm using 4.1.3 and I get "Unable to resolve mixin as type." I suppose, I need to update?

Re: A means of accessing user variables from unrelated class

by phantombeta » Mon May 25, 2020 12:52 am

ZScript is a statically typed language, and this is simply how statically typed languages work. It cannot be done without something like dynamic typing (which will never happen, and would require completely changing how ZScript works), interfaces (which requires the classes to implement them, and are very unlikely to happen), or reflection. (which is slow, and allows you to access private and protected fields from where you shouldn't, so it'll never happen either)
If what you want is simply to reduce code duplication, that's what [wiki=ZScript_mixins]mixins[/wiki] are for.

A means of accessing user variables from unrelated classes

by Kzer-Za » Mon May 25, 2020 12:11 am

It would be useful if user variables could be accessed and changed from a class that is not inherited from the same class. (If the variable really needs to be inaccessible, it can always be declared protected or private).

I have tried creating a "library" class with static functions, which would be used by any class (viewtopic.php?f=122&t=68675), but I can't access user variables in an unrelated class from such a function.

Why this is a problem? For example, I may need to call one and the same function from a player and from a monster, and obviously I can't inherit a monster and a player from one class. There are other cases when inheriting two classes from the common ancestor is more trouble than it's worth, but they need to call the same function.

Why don't use separate functions with the same code for them? Because if/when I decide to expand/change that code I may change it in one place and forget about the other(s). It seems like a bad design to keep one code in more than one place.

BTW, I need it for more than the simple function in the topic the link to which I has given, it's just a start. Besides, others may need it too. And since the security concerns are covered by the possibility to declare variables as protected or private, it seems reasonable that normal variables should be accessible from anywhere.

Top