Drawing objects of a class
Monkey Forums/Monkey Programming/Drawing objects of a class
| ||
Hey people! I was wondering how you guys make it so you call a single function and all objects of a same class are drawn (I mean, they will execute a method). I was thinking of saving them to an array and draw them all using a for-statement but maybe is there an easier way to do it? Thanks in advance! :) |
| ||
I have an entire class hierarchy for my personal engine(s). Ignoring the specifics of the hierarchy, I basically deal with 'WorldObjects' ('Entity' / 'Actor' functionality is built on top of this). Each 'WorldObject' has fields for their position, rotation, and scale (As well as some other fields, such as object-flags). All of my functionality for objects in the actual "world" is based on this model. From there, 'WorldObject' inherits other classes and models, but you get the basic idea. I use a "culling" routine that's tied to part of my collision-code, so I basically check if something is in the camera's view. From there, I add the objects to a stack per-camera, and I sort them based on some very basic draw-ordering techniques. I also have a "layer" system tied to that. Nothing ground breaking, but it's a lot faster than drawing everything. Sure, Monkey's scissor functionality is great, but my own systems reduce unneeded overhead further. Collision detection is done through using object-defined modes, which are backed by a managing class. Object relations and collision are handled using relatively simple quad-tree setups. I should probably do some more benchmarks on this at some point. It may just be faster to overhaul this. That's how I handle my render step. It's basically just a matter of making a stack for each "camera", and sorting these stacks as I see fit. That being said, you personally may want to look into less memory intensive setups; for example, you could just sort the stack (Or similar collection) you use for your objects already. |
| ||
I usually have something like: Basically the game window knows what categories of stuff are on it, and draws them. Unless your game is very complex, you don;t really need to automate drawing so much as keep it tidy. I tend to have the window do the drawing as a rule (simple sprite-based games) hence the separate functions for different types. A more sophisticated system would be to have every drawable object inherit from a base class or implement an interface, and add itself to a single list of drawables when created. Then you could just call Draw() for each item in the list. |
| ||
Hey guys thanks for the response. I must confess I'm a newbie yet so I don't know how lots of stuff work in Monkey. This is what I made:Class Space Extends App Field Player:Ship[4] Field Playas:List< Ship > Method OnCreate() Seed=Millisecs Player[0]=New Ship(Rnd(8,632),Rnd(8,472),True) Player[1]=New Ship(Rnd(8,632),Rnd(8,472),False) Player[2]=New Ship(Rnd(8,632),Rnd(8,472),False) Player[3]=New Ship(Rnd(8,632),Rnd(8,472),False) End Method OnUpdate() Local I:Int=0 Repeat Player[I].Update() I+=1 Until I>Players-1 End Method OnRender() Cls DrawShips() '===================== that's what i was doing, works 'Local I:Int=0 'Repeat ' Player[I].Draw() ' I+=1 'Until I>Players-1 End Method DrawShips() For Local B:Ship = Eachin Playas B.Draw() End End End It does not work, unfornately. What am I doing wrong? PS: 'Players' (in OnUpdate - not relevant though) is a global variable outiside Space class. I put it outside the class (I'd like to make it a field, but I couldn't access it from the ship class). |
| ||
Figured it out. Thanks! :) |