Can anyone give me some tips on how to make and store NPCs and objects?
-
Been running into a lot of problems lately trying to put some baddies in my game. At first I had Arrays of structs of baddies made by a function, but that didn’t work well for some Reason. I put them all in one list, and took the ‘making of an array’ out of the function, to try and make one global big list, but ran into problems with that too. I’m now at the point where i’m thinking I have to try something completely new. Or at least stop using the stuff I have now and write some fresh code that’ll hopefully work better this time.
So I’m here asking for tips before I do that. It’s a 3d game, i have a player character and a few fireballs. What would be a good way to have some enemies, non-combative NPCs, objects to pick up and throw, and buildings and trees etc that don’t move? Any discussion is welcome :)
-
I can't give you tips, but I know there are various threads in the forum about arrays of structures;
maybe you just haven't yet tried out everything that's possible with structures? -
I would definitely still use structs in arrays. I couldn’t really think of another way of doing it really...
Just be careful with a few things, like when copying a struct, always copy each element by itself, and not the whole struct. Also, when passing a struct to a function, the struct is “by reference” until you change anything in it, then it becomes a copy.
I would write code that worked with any number of items in the arrays; so adding a new enemy would just mean adding to the array, and then the game would just incorporate the new enemy automatically.
The struct would contain every bit of info for the enemy (or item or whatever), like coordinates, how it is facing, health, etc.
-
“ Also, when passing a struct to a function, the struct is “by reference” until you change anything in it, then it becomes a copy.”
That’s a brain twister...
-
That’s how I understand it at least. It’s quite weird.
-
Who was it who did ‘linked lists’ by attaching extra properties to sprites? I wonder if you can do that with 3d objects...
-
@toxibunny I think I tried this already once. I bet its not supported with 3D objects.
-
I’m going to have to do some sort of write-up when I figure out a good way to do it. I have my cats and dinos in a list, so clearly it’s possible. I should have a massive clean up of my code - it’s more barnacle than boat at the moment. Maybe after I get the cruft out of the way and reorganise, things will become clearer..
-
@toxibunny I thought your asking about adding properties to 3D object handles like @pianofire's example for custom properties with sprites.
obj = placeObject( cube, { 0, 0, 0 }, { 2, 2, 2 } ) obj.customProperty = 1234
What i have in mind is not working.
-
No, the 3d objects can't have properties now.
What has worked for me is stuffing the 3d object in a struct with other properties I want to associate with it, like
'''
S = [.obj=placeObject(...),
.hp=100,
.pos={0,0,0},
//etc.
]
''' -
Yep, that’s what I do. It’s having those structs in an array, and then trying to change those structs using functions that’s causing problems. At the moment, I’m solving it by having my arrays global, and putting all the code that deals with them in my main loop, inside ’if then endif’s .
Which seems like kindof a dumb and untidy way to do it, but that’s the level I’m at at the moment, and apparently this way doesn’t affect performance. Nothing worthy of a full writeup, but I will be talking about it in my WIP thread when I get around to it. My game is going well at the moment :)
-
@toxibunny That's great!
I actually also do the global array of structs approach. For my functions that manipulate an individual entity/object, I just pass the array index as an argument. Then I manipulate the strict within the global array itself, without needing to worry about copying structs.
That works well in my game, too, without me noticing or measuring any real performance penalty.