[No] A means of accessing user variables from unrelated classes

Moderator: GZDoom Developers

A means of accessing user variables from unrelated classes

Postby 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.
Kzer-Za
 
Joined: 20 Aug 2017

Re: A means of accessing user variables from unrelated class

Postby 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 mixins are for.
User avatar
phantombeta
In the meadow of sinful thoughts, every flower's a perfect one
 
Joined: 02 May 2013
Location: Brazil, South America, Earth, Orion-Cygnus Arm, Milky Way
Discord: phantombeta#2461
Twitch ID: phantombeta_
Github ID: Doom2fan
Operating System: Windows 10/8.1/8/201x 64-bit
OS Test Version: No (Using Stable Public Version)
Graphics Processor: nVidia with Vulkan support

Re: A means of accessing user variables from unrelated class

Postby 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?
Kzer-Za
 
Joined: 20 Aug 2017

Re: A means of accessing user variables from unrelated class

Postby 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.
User avatar
Rachael
Webmaster
 
Joined: 13 Jan 2004
Discord: Rachael#3767
Twitch ID: madamerachelle
Github ID: madame-rachelle
Operating System: Windows 10/8.1/8/201x 64-bit
OS Test Version: No (Using Stable Public Version)
Graphics Processor: nVidia with Vulkan support

Re: A means of accessing user variables from unrelated class

Postby 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 :)
Kzer-Za
 
Joined: 20 Aug 2017


Return to Closed Feature Suggestions

Who is online

Users browsing this forum: No registered users and 1 guest