• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Xer0botXer0

Methods accessing each others variables

6 posts in this topic

Hi guys,

So I'm wondering why cant I reference a value or even access a value that's in a method from another method.. like so

 

public class Main {

    public static void main(String arg[])
    {
                
        String bob = "bobby";
        compute();
    }

    public static void compute() 
    {
    main.bob = "yoyo";
    
    }
}


 

I don't know if I'd ever want to do this, but I'm just curious, does this mean that there's simply no other way to modify the variable values of a method without providing it with values through invoking the method ?  

 

So in the following way I can basically set what values (true to the predefined data types) these argument variables will have..

public class Main {

    public static void main(String arg[])
    {
        
        String one = "Ping",two = "Pong";
        String Result = "";
        Result = compute( one,  two);
        System.out.println(Result);
    }

    public static  String compute(String first, String second) 
    {
        
        String joined = first + " " + second;
    return (joined);
    
    }
}

 

What if instead of this I have

 

        String joined = first + " " + second;

        String joined_2 = third + " " + forth;

        String Altogether = joined + joined_2;
    return (Altogether);

 

So say i get to the first line in that code and suddenly want to change a value in the second line, would that be possible ? 

 

Okay crappy example, I suppose parameters are then the only manner available for methods to get values from the invoking method ? 

 

0

Share this post


Link to post
Share on other sites

Most languages (including C# of Java, which you appear to maybe be using) are not designed this way; they simply don't consider a method to have local instance variables and state associated with it that persists outside the temporary memory reserved for that method when it is executing. In fact, I don't know any languages (offhand) that would explicitly permit you to do what you wrote the initial example for.

Some languages do have "static" local variables in methods, but they're usually just like globals with a narrower lexical scope; explicitly narrowed so you cannot refer to them like you're suggesting.

So yes, there's no way to modify the value of a variable in a method without passing a value to that method (or similar) to indicate how the method should initialize the variable. The (non-static) variables you're declaring in a method are local: they come into being when the method is called, and exist only for the duration of the method's execution. Then they are (effectively) destroyed when the method ends. So there's nothing to "modify" outside that method.

45 minutes ago, Xer0botXer0 said:

Okay crappy example, I suppose parameters are then the only manner available for methods to get values from the invoking method ? 

 

If you want to "share" information you need to either pass it from method to method or store it as a member of the containing class. Those are generally your two best options, and there are cases where either choice is the better one. It depends on what you're doing.

You can also create globals and share information that way, but this is generally not a great option (and not directly supported by C#, but you can do things to create effective globals that share all the same problems anyhow).

2

Share this post


Link to post
Share on other sites

Thanks, well I'm using Java. And I was thinking of objects in Game Makers GML, where variables of objects can be referenced  and modified.

But I've come to realize that methods in Java are similar to Scripts in GML which are temporary. So what I'm looking for I think would be classes. I recall now that I can access variables/arrays and such from one class to another using the classname.variablename line. 

I'm mainly trying to get a better image of what everything can and cant do while studying. 

0

Share this post


Link to post
Share on other sites
18 minutes ago, Xer0botXer0 said:

Thanks, well I'm using Java. And I was thinking of objects in Game Makers GML, where variables of objects can be referenced  and modified.

 

Variables stored in objects can be referenced and modified in Java. Methods, however, are not objects in that sense.

19 minutes ago, Xer0botXer0 said:

So what I'm looking for I think would be classes. I recall now that I can access variables/arrays and such from one class to another using the classname.variablename line. 

Yes. A class describes a type of object, and you can create instances of that type. Those instances are the kinds of objects you can read and write the member variables of.

2

Share this post


Link to post
Share on other sites

Posted (edited)

So I did a bit of further reading and from what I can see, There are Instance fields/Instance methods and there are Class fields/Class methods, The class fields and methods are unique to each instance(object) of a class. Where Class fields and methods are somewhat global and are recognized by their static modifier in their definition. 

I can modify the instance fields/methods by storing a reference to the object in a variable.

But I've got a question regarding the Static types, I'm going to test this out now anyway for the practice because as someone said before it helps to try it out myself too. But I like the forum since you can sometimes get more information which is apart of what helped me progress in Java quite a bit. 

If I create a class called Fruit, and within the class I create a class variable called fruit_Type, then create four objects of class Fruit, I don't use a for loop rather just one after another, in each case I set the fruit_Type class variable to something different like "mango", "orange", "tomato(is tomato a fruit lol)", "guava". 

If I then iterate through all four objects, will they have unique values for fruit_Type, can I even access a static field(var) that way ? Going to test this out now. 

 

From my understanding the purpose of a class field is to store a value that all objects of that class will share, the purposes of instance fields is that the instance values can be unique, instead of having a basket.java of mangoes I'd have a basket of a variety of fruit.

 

Okay I tested it.

public class Main {
    
    public static void main(String arg[])
    {
        
        Fruit f1 = new Fruit();
        f1.fruit_Types = "Banana";
        
        System.out.println(f1.fruit_Types);
        System.out.println("");
        
        Fruit f2 = new Fruit();
        f2.fruit_Types = "Guava";
        
        System.out.println(f1.fruit_Types);
        System.out.println(f2.fruit_Types);
        System.out.println("");
        Fruit f3 = new Fruit();
        f3.fruit_Types = "Grape";
        
        System.out.println(f1.fruit_Types);
        System.out.println(f2.fruit_Types);
        System.out.println(f3.fruit_Types);
        System.out.println("");
    }
}

 

Fruit.java :: public static String fruit_Types = "Lemon"; produces:

  • Banana
  •  
  • Guava
  • Guava
  •  
  • Grape
  • Grape
  • Grape

while without the static modifier it becomes an instance field and produces:

  • Banana
  •  
  • Banana
  • Guava
  •  
  • Banana
  • Guava
  • Grape

 

So I'm happy with the findings. :D

Edited by Xer0botXer0
0

Share this post


Link to post
Share on other sites

Static, in this context, does mean "shared across all instances of this class". Everything else has its own copy per instance. Good work on testing for yourself and verifying your understanding. Try and avoid statics unless you absolutely need something shared, however.

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0

  • Similar Content

    • By Goyal_Sahab
      I want to pursue my career in game development . I don't know much how to start. Currently , I have knowledge of Java, C ,Python and little bit of javascript. Now I want to know the recommended learning path. I just want to start making simple games .Please recommend some learning too. Thanks
    • By BarraBod
      This is the first game i have made and was more of a practice game before I try something bigger.   But I might start another small app next as I'm still learning! 
      This game was written in Java using the LibGDX framework which I love.  It also means I can port it to IOS and HTML which I'm hoping to do.
      My sprites and textures were taken from free sources, such as www.freeclipart.org, as I can't do graphic design or game art.   I'll need to fix this if I'm going to try something bigger.
      You can download the game here: 
      SubSea
      My games FaceBook page
      Any bugs can be posted here, if anyone wants to play it.
      Thank you and good luck.
    • By SneakyCrago
      Hello everyone!

      This is my first project for android.
      I was very interested of making games, and find LibGDX framework, and there I started. After 6-7 month I finished first mode for my game.

      So here we go ^^
       
      Undercore - hardcore runner for android.
      You have to use skills like jump and stay on line. And you goal is to make a highscore dodging obstacles.
      ○ Improve your skills - the way will be rough, will you become a master?
      ○ Contest - your friend hit 40 points? Double score and make him jealous!
      ○ Collect - buy new color themes that would make the gameplay brighter!
      ○ Achieve - beat records, die, earn. Collect achivements. No pain no gain.

      PlayMarket: https://play.google.com/store/apps/details?id=com.sneakycrago.undercore&hl=en
      Youtube - Gameplay



       
      I hope you enjoy it and also wait for feedback 

      I can't make clickable button.Sorry, just link: https://goo.gl/dG1dLj
    • By Seer
      Using the LibGDX InputProcessor, what can I do with the keycode integer values generated in keyDown() and keyUp() to handle input correctly? To expand, what I mean by handling input correctly is to handle it in such a way so that GameObjects which need to be controlled by user input, do not themselves actually handle the user input or hold any input related values such as key down/up flags, and are instead influenced indirectly by something else which handles the input.
      One way in which I feel this can be done is with a Controller object which can act as a mediator between the input values and the object you want to be influenced by the input. The Controller would hold a reference to the object and methods to receive the keycode input value. Depending on this value, the Controller would then, using a switch case or set of if-else's, call relevant methods on the object such as move(x, y) or attack(). This decouples the object from the input and allows you to control it in a top down way.
      The problem with this approach, in my case at least, is that it does not provide a fine enough level of control over the object so that it responds appropriately to different sets of keys. A concrete example of this would be if the object is moving in a certain direction because a key is held and another key is pressed detailing that it should move in the opposite direction, I would want the key that would make the object move in the opposite direction to be ignored so long as the first key remains held. With the Controller setup that wouldn't happen. The second input would override the first, causing the object to move in the opposite direction.
      The way that I have managed to achieve the level of control I need over how input is acted on by GameObjects, which in my case is primarily the Player object, has been by making the Player receive the keycode directly from the InputProcessor and handle it through the use of flags and states in the form of State objects. The flags represent the state of input which is relevant to it and the states determine explicitly the state of the Player and what it can do at any one time, including how it handles input state. The current state updates every Player update and based on a set of conditions, including the state of the flags, it either changes to another appropriate state or continues updating.
      This setup is bad for a couple of reasons. It makes it so that the Player is responsible for maintaining key state in the form of flags, essentially becoming an input handler. It also tightly couples the States it uses to itself, making it so that they can only be used by the Player and not another kind of object.
      The problem I am facing is that I don't know how to change the setup so that GameObjects which should act on input can do so while remaining decoupled from the input, while also maintaining the fine level of control that states and flags provide over how the input is processed.
      What can I do to achieve this? Thank you.
       
    • By Xer0botXer0
      Hi guys,
      So I managed to get a book on Java! and I'm at a point where it's talking about try/catch/finally statements, from what I understand say I've got a java program and the user clicks on a button called "load recent save" .. the button click event would call a method called load_recent and have a try catch statement, within the try it will check say an ini file for the last known location, but now say the user deleted the file.. so the last known location would be incorrect and it would throw the exception java.io.FileNotFoundException, this is where I'd like to know what happens, say I catch the exception and popup a dialog allowing the user to browse for the file and also it will clear that location. Okay so I've handled the exception.. but now I'm wondering when the code block of the catch has finished running, does the try statement start over ? I'm kind of confused on how this works.
       
      The other thing is, I get the impression that try catch can be entirely avoided by just coding properly. For example if a person divides 0 by 0 it'll throw some exception, but it'll only throw that exception because as a coder I didn't think hey instead of using try catch what I can do is prevent 0 from being divided by 0 in the first place. So my guess is that reason try catch exists is to avoid extra code. By extra code I mean without using try catch a dev has to add more code to prevent that from happening by checking which values and operators are being used. How ever if try catch is used then no extra code is read, only when an exception is thrown does the compiler look for the relevant catch exception.. does that make sense ?
      So first question is when an exception is found, does the compiler move back to the try statement or carry on from the caught exception ? or carry on from where an exception was last caught ? the third possibility sounds about right.
      And then the second question is, is try catch really useful when errors can be predicted and handled with code in the first place. 
  • Popular Now