NaaLaa
manage objects automatically - Printable Version

+- NaaLaa (https://www.naalaa.com/forum)
+-- Forum: NaaLaa (https://www.naalaa.com/forum/forum-1.html)
+--- Forum: NaaLaa 7 Questions (https://www.naalaa.com/forum/forum-3.html)
+--- Thread: manage objects automatically (/thread-2.html)

Pages: 1 2 3


RE: manage objects automatically - Marcus - 11-16-2023

(11-14-2023, 05:12 PM)aliensoldier Wrote: I have created two examples based on the one you have shown me of oop, one with a list and the other without a list 

The one that uses the list library works fine, but the one that doesn't use the list library I can't get the trigger to be eliminated when exiting the screen. 

I show you the two examples and you can tell me where the error is or you can make a version of your example without using the list library

Sorry for the delay, got busy yesterday, but I've had a look at your code now Smile

I replaced
Code:
free deadList[i]
in Father_Update with
Code:
free val fatherList, deadList[i]

The new line removes the object in deadList from fatherList. That is, 'free val x, y' removes any occurrence of y from the array/table x and re-indexes the array (removes any holes).

Code:
function Father_Update()
    if sizeof(fatherList)
        for i = 0 to sizeof(fatherList) -1
            fatherList[i].Update()
        next
    endif   
   
    if sizeof(deadList)
      for i = 0 to sizeof(deadList) -1
          ' Marcus
          'free deadList[i]
          free val fatherList, deadList[i]
      next
    endif
    clear deadList
endfunc

You should never use 'free' when working with arrays. Always use 'free val' (remove any occurrence of a value) or 'free key' (remove an index/key and its value).


RE: manage objects automatically - aliensoldier - 11-16-2023

Thank you very much for all the help, I really liked all the examples and I have learned a lot. Smile


RE: manage objects automatically - aliensoldier - 11-29-2023

Hello Marcus.

I have prepared another, slightly more elaborate example. I would like you to look at the father1 file and tell me if it is programmed correctly or if there is something to change or correct.

You can also look at the rest, greetings.


RE: manage objects automatically - aliensoldier - 12-09-2023

Hello Marcus.

Have you looked at the code that I wrote a couple of weeks ago, I want to know if it is correct to start programming a small game.


RE: manage objects automatically - Marcus - 12-09-2023

Ah, sorry, I started looking at it when I was in a rush right after you posted it, but I got confused about something and then forgot about it Smile

I'll try and have a look at it tomorrow!


RE: manage objects automatically - Marcus - 12-18-2023

I'm terribly sorry for still not having had a look at your code. It's christmas times, and I barely have the time to start my computer. But if it it works, it works. And no matter how well it works, you'll find things and bugs while actually writing a game using the code. So start coding and see what happens Smile I will review the code eventually, when things get more calm over here.


RE: manage objects automatically - Marcus - 12-18-2023

I had a short look now, and I'm a bit confused Smile

Let's focus on the main file, example-father, for now:

Code:
while not keydown(KEY_ESCAPE,true)
    set color 0,0,0
    cls
    '--------------------------------------------------
    Father().Update_all()
    Father().Draw_all()
    'eliminar todo
    if keydown(KEY_X,false)
        Father().Clear()
    endif
    '--------------------------------------------------
    redraw
    fwait 60
wend

Here you create a new father object every time you call Father(), which is a bit "strange" and not optimal.

The problem is that you have made the "base class", Father, serve as a sprite manager. If I were you, I would remove the functions Add, Update_all, Draw_all, Destroy and Clear from Father and put them in regular functions, or create another "class" (a sprite manager) whose only job is to manage a list of sprites.

I've attached a new example-father.n7 and father1.n7 where I've made the change that I suggested. Every function that deals with the sprite list (fatherList) are now regular functions. I didn't remove the functions from the father "class", but they now call the independant functions.


RE: manage objects automatically - aliensoldier - 12-20-2023

I'm going to look at the corrections you've made. Smile


RE: manage objects automatically - aliensoldier - 12-21-2023

I have added all the corrections that you have mentioned and I have added a new function called Father_getId(), to obtain the id of each object and be able to access its properties.

Tell me if everything is correct and I'll start with my first game. Smile


RE: manage objects automatically - aliensoldier - 01-16-2024

Hello Marcus, have you looked at the last example I gave you to see if everything is correct or if there is something to correct.