Announcement

Collapse
No announcement yet.

global variables are not really global.

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    global variables are not really global.

    If I have a base class called "Car" with a variable:'var int myState', then I make children of "Car"...each of these children will have a unique instance of 'myState'. The only thing that makes them global is that they can be referred to throughout the entire script of each child, but not outside of them.

    I've been doing research on these forums and such and am having trouble doing what I need, I've been trying to just create a class called "carState" that will just hold 'var int myState'. Then I want to be able to read and write this value in the children of "Car" and "Car" itself.

    This is my class for holding the variable:
    ____________
    class carState extends Object;

    var int myState;

    defaultproperties
    {
    myState=0
    }
    ______________



    now here is my "Car" base class:
    _______________
    class Car extends Actor;

    var class CS;

    function PostBeginPlay()
    {
    CS=Spawn(class'carState');
    }

    function Touch(actor Other)
    {
    log(CS.myState);
    CS.myState=1;
    }
    _________________

    when I compile, i get something like "unrecognized member" in class'Class'. I've tried several different ways of typecasting "CS", but I can't figure it out. I'm thinking it might be because the 'carState' class is not an instance of the class, but I'm not sure what that means either.

    #2
    var carstate CS;

    Comment


      #3
      "Error, Call to 'Spawn': type mismatch in parameter 1"

      Comment


        #4
        Ah. Only Actor subclasses can be spawned.

        Also, I don't understand what carstate is doing. Why not have it be a part of car itself?

        Comment


          #5
          I was actually just about to post again and ask you if it was because i was extending "Object".

          so my "carState" class should read:

          _______
          class carState extends actor;
          _________

          I'll test.

          Comment


            #6
            wierdness. two dragon heads appear in the level next to each instance of Car's children. and its not working properly.

            to answer your question. CarState can't be part of Car because I want the children of Car to be able to refer to the value of one single variable, rather than their own unique instance of CarState/myState. What I'm doing is changing the children's bUnlit property based on carState, and I want the children to be able to change the singular carState when they are touched. i hope that makes sense.

            Comment


              #7
              Originally posted by yourBestMan
              wierdness. two dragon heads appear in the level next to each instance of Car's children. and its not working properly.
              1) Set bHidden in the properties.

              2) Make sure Car is set up to be able to receive Touch calls. Look at Trigger's default properties to see what settings need to be there.

              Comment


                #8
                here is the script for "Car" (I would use the tags, but I get an Internet Explorer Script Prompt Dialog Box when I click, sorry)
                __________

                class Car extends Actor;

                var carState CS;
                var int myState;
                var int myChange;

                function PostBeginPlay()
                {
                CS=Spawn(class'carState');
                }

                function Touch(actor Other)
                {
                ........if (CS.myState==myState)
                ........{
                ..............CS.myState=myChange;
                ........}
                }

                defaultproperties
                {
                myState=0
                myChange=1
                }
                ______________________

                and each child of "Car" has a unique value for myState and myChange

                Comment


                  #9
                  "Car", and all of it's children do receive the touch(i have a log). but only the first instance works with the bUnlit property. so I am making some kind of syntax error I guess.

                  Comment


                    #10
                    I figured out one reason why it wasn't working:

                    if (bUnlit==FALSE)
                    { return; }

                    That way, the player doesn't touch multiple times and undoes what it was supposed to do.

                    The second reason, I don't have a solution for. As this is NOT happening:

                    CS.myState=myChange;

                    maybe I can't assign new values to variables in the carState class?

                    Comment


                      #11
                      So your thinking is that if you change one carState, ALL other cars should change as well? :bulb:

                      Comment


                        #12
                        yes, that is my thinking. well, not that the children should change, but that they should be able to read and write to this one umbrella variable.

                        If I cook a pizza at one temp, each pepperoni should be cooked at this same temp as well...not independent instances of the temp. There is only one temp! If I lower the cooking temp of Pizza, than the temp of Peperroni's should change as well, they shouldn't remain at unique cooking temps.However, it seems that Uscript doesn't natively allow this kind of thinking. am I wrong?

                        Comment


                          #13
                          Hm, yes and no. You can do it, but not the way you are implementing it. You see, each Car is creating their own carState. Changing the state of one will not affect the others. What you need is either to first search AllActors for an existing carState and use that if you find one, or if not found make one. Then all of your Cars will use this one carState.

                          Alternately you could have one controller object that spawns all of the Cars and assigns their CS variable a reference to an already existing carState object.

                          Comment


                            #14
                            yeah, you're right, I actually realized this after my last post. I'm spawning a unique carState with a unique myState variable.

                            what a pain in the ***. this can't be real OOP. your first suggestion seems taxing on the CPU, the second seems reasonable, but does that mean I can't place the objects using UnrealEd?......can I refer to the myState variable(inside of a sole instance of carState) in Car without spawning a carState object inside of Car?

                            Comment


                              #15
                              Its not very OOP at all, which should tell you somethign about the way you're implementing it

                              I dont know what it is you need it for, specifically, but I would bet its either silly, or not needed.

                              The first method is probably easier, and not TOO taxing on the cpu since it only happens once per actor, unless you have thousands of them.

                              Comment

                              Working...
                              X