Need help with enemy spawn coords

(Cody) #21

Thanks @Pharap for your help I will try this today. Just woke up😴

(Cody) #22

i think its in above my head. :sob:
though do you think you could try and adapt my code to work to yours or make a simpler tutorial and work up to that?

(Pharap) #23

You woke up at what would have been 11pm in my time zone. Yay time differences.

Which parts are beyond your understanding?

The use of classes?
The use of template?
The const uint_t & part in functions?
The use of constexpr?

I am willing to explain it to you so you can learn something new or I can remove some of the features (templates, constexpr, references), but ultimately I think you should at least try to use classes for this because they make the code much easier to follow and they make it harder to introduce bugs in some cases.

Really though you only need to understand what a class is, what a constructor is, what loop and render are doing and maybe how the list works (or at least how to adapt it).

(Cody) #24

im baffled by
construct name
some void i dont get
something like that
and classes i dont even know whereto start also is a uint_t a big word for int?

(Pharap) #25

It would have been more helpful if you’d quoted the exact parts of the code instead of putting some random words around some curly braces.

uint8_t is an 8-bit unsigned integer, so on Arduboy it’s a synonym for unsigned char (and int8_t is an 8-bit signed integer, so on Arduboy it’s a synonym for signed char, and there’s more for 16-bit and 32-bit). I use them partly because they’re more ‘standard’, partly because they’re clearer and partly because they’re shorter/quicker to type.

Ok, so basically you’ve never even touched structs or classes?
Erm… this is going to be difficult. I’ll have a go at explaining them, but to be honest I think if you aren’t ready to learn about structs/classes and need a solution without them then I may not be the best person to help you.

Basically a class or struct is like a blueprint that describes a new type to the compiler.
Each ‘instance’ (or ‘object’) of that type is a bundle of data (kind of like variables) and possibly has some associated functions.

For example, take this very simple struct:

struct Point
  uint8_t x;
  uint8_t y;

This says to the compiler "a Point is a type that contains a uint8_t called x and a uint8_t called y".
So then you can do this:

Point coord;
coord.x = 5;
coord.y = 8;

When you refer to coord.x or coord.y they behave just as they would if they were standalone variables, but they’re bundled together in the same ‘object’ so they make things easier to manage.
For example, you can also do this:

Point coord2 = coord;

And coord2 will copy both the x and y of coord, so now coord2.x is 5 and coord2.y is 8.
You can then change coord2.x and coord2.y like so:

coord2.x = 10;
coord2.y = 9;

And coord2's x and y change, while coord's x and y stay the same.

Are you with me so far?

(Cody) #26

How did cord2 become 8 and 5?

(Simon) #27
Point coord;
coord.x = 5;
coord.y = 8;
Point coord2 = coord;

When you assign one struct to another, it copies all of the member values across. Therefore at this point coord2.x = 5 and coord2.y = 8.

(Cody) #28

Just mucked around with this and I have a good understanding​:smile::smile:

(Simon) #29

Of course you can … but imagine if you had a 20 enemies and you needed to keep their current X pos, Y pos, health, age, number of kills and a dozen other properties. You would need to declare and keep track of 400 variables.

Now, how would you loop through the enemies? You can add the 20 enemies (which were created from a single struct) to an array and loop through them as needed. Try doing that with your discrete variables.

(Simon) #30

Ahhh … you edited your response. Now mine makes no sense!

(Cody) #31

YESSSS yes I did :smirk: Any who now that I understand that how would I make it so when press a it spawns a enemie without a thousand ints or uint. I I mean like is there a better or easier way than @pharap 's

(Simon) #32

Structs and classes can achieve the same thing … structs are (generally) just used to hold related data together and allow you to pass them as parameters as a unit or collect them in an array. Classes can have code associated with them along with the data therefore encapsulating the logic. If you are finding the classes too hard then start with structs …

(Cody) #33

Class Clear()

(Simon) #34

Well … no. You would probably only make a class for something that you are likely to have many of.

(Cody) #35

But in theory it would work
But could you give me an example

(Simon) #36

@Pharap’s example is a good one.

(Pharap) #37

I can try to strim my example down to the bare minimum, i.e. I can make Enemy a struct with just an x and a y, no functions.

I can’t get rid of functions on the list though, they’re needed to make it work.
You’d have to put up with being able to use it but not necissarily understanding how it works, like you do with Arduboy2. I could break it down so it’s not a class - it’s an array and some functions - but then it wouldn’t be reusable, you’d be stuck with just one list, and it would be more likely to break and harder to manage.

Like @filmote says, the only alternative to using structs/classes here is to have to maintain loads of different variables and it will start to get really confusing. Having one list of Enemy where each enemy has an x and a y is better than having two lists of enemyX and enemyY. That might seem like the ‘easy’ way because it means not trying something new, but in the long run it’s harder because it makes the code messier, makes it harder to keep track of all the variables and makes it harder to find the variable you’re looking for.

(Cody) #38

So what your saying is just look through yours and try to understand that? Also why can’t I make cord.x++; the rect doesn’t move?

(Simon) #39

corrd.x++; will work.

When you say the rect doesn’t move, how have you determined that?

(Cody) #40

The rect’s x is assigned to cord.x and now think it’s because I have structure point in void loop and x = 0.