function calling by specific class

Hi,
I have some functions on a call, I don’t want to make those functions public but still call them by only 1 controller, How to have functions on a class only allowed to be called by a certain class ? (without making the function public)?

what i’m thinking might work is if I do following check on function to check if caller is the class we want but idk.

Function (behavior b)
{
if (b != ControllerScript)
            Return;
}

Is this a security requirement, or are there other reasons why you Need this? If it’s security, your implementation can be easily spoofed by an attacker passing a valid b as Parameter without being b itself. You could try some simple Password protection (only allow execution if caller has the password or hash), or come up with a callback sheme where after calling the result is passed to b via callback. But it’s going to be ugly, difficult to maintain, and error-prone. Same goes for any implementation involving Caller Information.

From the language itself, you can try and make the functions protected, and the Controller a subclass, but I believe that is not what you are looking for.

Perhaps we can help you more if we better understand why you need that behavior.

Cheers,
-ch

So I have a main “Game Controller” Script, Then I have smaller “Mini Controller” Scripts that each have functions about specific purpose, for example (Scoring).
I want to use the main script to manage / call functions on the mini scripts and in a perfect world i’d like to keep those functions private but still call them by the main script

Understood. But why do you want to keep them private?

guess its cleaner to have a hierarchy of controllers with the topmost main controller having full control of child controllers only ?

Why is that cleaner? All design elements in modern programming languages (interfaces, protocols, polymorphism) ge exactly the other way: they provide access and expose properties to other objects that are not in their ancestry. You seem to be looking for something that is mostly inverse: provide access to other, highly specific objects outside their ancestry. It’s a novel concept I’m trying to understand and therefore would love to see some practical applications.

There’s not really a practical reason to do this. The only value is stopping you - the programmer - from making calls from sources which you already know are [arbitrarily] unauthorized and you’ll only know about it at runtime. It requires a stack trace to properly understand where the method call came from. It is not efficient, useful or necessary.

1 Like

Ok so I shouldn’t make derived classes + use protected voids for example ? just make things public

Perhaps we are talking about different things here. When you say cleaner, it sounds a lot like you mean more secure. From a design standpoint having an attribute or method that’s public to some arbitrary classes, and private to other classes outside their own class is IMHO anything but clean. That would be a design and maintenance nightmare. It’s the difference between a clean design (everyone who conforms to x has y) and a muddled one (everyone who conforms to x has y, unless it is named z).

Why the aggressive tone and intentional misrepresentation of what I wrote? I’m genuinely interested in the concept you proposed, just not sold on the question if it can work to our advantage.

What you suggest to do to make this design both more clean / secure? I’ve very limited experience programming and usually heard making functions public is not very good idea, not trying to come off “aggressive” or anything just trying to understand what is a good method to use here.