Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
manage objects automatically
(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
free deadList[i]
in Father_Update with
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).

function Father_Update()
    if sizeof(fatherList)
        for i = 0 to sizeof(fatherList) -1
    if sizeof(deadList)
      for i = 0 to sizeof(deadList) -1
          ' Marcus
          'free deadList[i]
          free val fatherList, deadList[i]
    clear deadList

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).
Thank you very much for all the help, I really liked all the examples and I have learned a lot. Smile
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.

Attached Files
.zip (Size: 21.59 KB / Downloads: 5)
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.
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!
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.
I had a short look now, and I'm a bit confused Smile

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

while not keydown(KEY_ESCAPE,true)
    set color 0,0,0
    'eliminar todo
    if keydown(KEY_X,false)
    fwait 60

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.

Attached Files
.n7   example-father.n7 (Size: 706 bytes / Downloads: 2)
.n7   father1.n7 (Size: 2.47 KB / Downloads: 2)
I'm going to look at the corrections you've made. Smile
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

Attached Files
.zip (Size: 21.69 KB / Downloads: 6)
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.

Forum Jump:

Users browsing this thread: 3 Guest(s)