Updating Class List in Class itself

About Monkey 2 Forums Monkey 2 Programming Help Updating Class List in Class itself

This topic contains 9 replies, has 4 voices, and was last updated by  Pakz 1 year, 9 months ago.

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #8832

    Pakz
    Participant

    I created a class called unit which I store in a list. I tried to create a method in this class to update all the units inside this class but I got an error calling this method.

    I am not sure how to do something like this. Is it possible if the class is in a list? Do I need to use a stack for this and call the first item from the stack to run this method that updates all?

    #8833

    Jesse
    Participant

    I am not sure what you mean. Are you extending the list class with your own method or are you using it some other way?
    if you declare the list Global and initialize it only once, it should be possible.

    #8835

    Pakz
    Participant

    Here is a example. I want to have methods in the class that update everything. Here below it does not seem to work. I can call the method that should update all the things in the list but it only updates 1.

    #8836

    papgar
    Participant

    I’m not a OOP master, but I don’t think you want to do it the way you’re doing it.  Looks kinda fishy.

    But the issue with your code is it keeps drawing the first circle and none of the others.  Change the line to this so it references the objects in the loop:

    #8837

    Jesse
    Participant

    As papgar said and that’s how it’s supposed to be.
    also the method moveallright all and drawall should be functions that way there won’t be any confusion on its use. this way you won’t need to use miunit.first.etc.. all you need to do is use unit.moveallright() and unit.drawall()

    #8838

    Pakz
    Participant

    Ohh, I did not notice that 🙂

    Seems to work now.

    #8839

    Pakz
    Participant

    I am still a little bit confused on functions and methods. The difference between them.

    #8840

    Jesse
    Participant

    treat a function as you would use any independent function. with the exception that if the function is inside a class you can only call it by the class instantiation or by using the class name.functionname(). With method you can only call it if it has been instantiated. and only through it’s instance.

    #8847

    cocon
    Participant

    As a sidenote you could do something like this to make the codebase a bit more modular.

    #8895

    Pakz
    Participant

    @cocon

    That look as how I usually use classes. Only what is the Unitcollection for?

    btw – I just discovered a thing with functions and methods that took me a while to fix. I got a error when I had a function inside a class trying to use a list inside the same class. This is not possible. You need to make it a method. Methods have access to the data fields in the class it appears.

Viewing 10 posts - 1 through 10 (of 10 total)

You must be logged in to reply to this topic.