#This is not safe because `ParentClass`
1 messages · Page 1 of 1 (latest)
.
just copying this in here
public class ParentClass {
}
public class ChildClass : ParentClass {
}
public class MasterScript {
public ParentClass myParent;
}
public class ReceiverScript {
public ChildClass myChild;
}
i know but i want receiverscript to use a variable in myChild
You can't safely assign a ParentClass into a ChildClass field. myParent can hold objects that myChild can't
well is there a way to do get myChild to be assigned using myParent
what are you trying to make your game do?
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
well, I don't see how that requires you to downcast (convert a parent type into a child type)
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
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
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?
it sounds weird. show me your current code.
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
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
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;
}
}
i dont want to change the jump height
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
potato tomato
no i just have every part of the movement as a sperate script
wait, that's not how the expression goes
potayto potahto :p
store information like "gravity strength" in a central place
i dont want the strength, i just want to reset the gravity
this is the physics script
oh my god i have been so stupid