#This is not safe because `ParentClass`

1 messages · Page 1 of 1 (latest)

rain pasture
#

.

#

just copying this in here

#
public class ParentClass {

}

public class ChildClass : ParentClass {

}

public class MasterScript {
  public ParentClass myParent;
}

public class ReceiverScript {
  public ChildClass myChild;
}
solemn goblet
#

i know but i want receiverscript to use a variable in myChild

rain pasture
#

You can't safely assign a ParentClass into a ChildClass field. myParent can hold objects that myChild can't

solemn goblet
#

well is there a way to do get myChild to be assigned using myParent

rain pasture
#

what are you trying to make your game do?

solemn goblet
#

get the jump script to reset the gravity in the physics script every jump

#

this is so that the double jump and normal jump would be the same height

rain pasture
#

well, I don't see how that requires you to downcast (convert a parent type into a child type)

solemn goblet
#

because the physics script has a function called ResetGravity which is what im trying to access

#

and i cant reference it before the start because i want the movement system to be modular

rain pasture
#

then you have a design problem.

#

something is generally wrong if you need to downcast

#

If I'm holding a ParentClass, it shouldn't matter what the exact type of the object actually is

solemn goblet
#

you know what, i think i have a solution

#

have in ParentClass public override void Request(int order)

#

and then a switch and for each order do a different action

#

so say, set the order to 1, do void x, set to 2, do void z

#

does this sound like a good plan or is it something that will come to bite me in the ass in the future?

rain pasture
#

it sounds weird. show me your current code.

solemn goblet
#

it'd be a bit weird since its spread across but i can sure

#
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ModularPlayerMovementAbilityMaster : MonoBehaviour
{

    // This script is a way to make modules for all player movement abilities

    [Header("Modularity")]
    // These are variables that are needed for the modular asepcts of the modular master script

    public string moduleName;
    public bool isModuleActive;
    public int abilityStage;
    public bool initialized;
    public PlayerMovementExecutor playerScript;
}

this is like the parent class

#

im going to crop the master script so it only has the things neeed

rain pasture
#

if you want to adjust things like "jump height", then you should do that in a central place

#

and have the different movement systems read those configuration values

solemn goblet
#
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovementExecutor : MonoBehaviour
{

    // This script is for activating and executing all of the player movement ability modules that are attached to the player. That being said, there are some universal functions that still need to be done within this script

    [Header("Modularity")]
    // These are variables that are related to the execution and connection of the modules

    public ModularPlayerMovementAbilityMaster[] modules;
    public bool finishedInitializing;


    void Awake()
    {
        foreach (ModularPlayerMovementAbilityMaster script in modules)
        {
            script.playerScript = this;
            script.initialized = true;
        }
        finishedInitializing = true;
        rb.freezeRotation = true;
    }
    public void LocateModule(ModularPlayerMovementAbilityMaster setAs, string nameOf, ModularPlayerMovementAbilityMaster sender)
    {
        int moduleCountByName = 0;
        foreach (ModularPlayerMovementAbilityMaster script in modules)
        {
            if (script.moduleName == nameOf)
            {
                moduleCountByName++;
                setAs = script;
            }
        }

solemn goblet
rain pasture
#

If your code has to figure out exactly what kind of movement system it currently has to configure it, then that's kind of the antithesis of modularity

solemn goblet
#

no i just have every part of the movement as a sperate script

rain pasture
#

wait, that's not how the expression goes

#

potayto potahto :p

#

store information like "gravity strength" in a central place

solemn goblet
#

i dont want the strength, i just want to reset the gravity

solemn goblet
#

oh my god i have been so stupid