• What Do You Need Help With? V6
    7,544 replies, posted
[QUOTE=WTF Nuke;45489205]I have some questions about move semantics. Lets say I have two instances of a class that contains a vector. I want to move the vector of one of these instances to the other. Is this possible? If so, why? Wouldn't the classes become non-contiguous in terms of their memory? Or do vectors(and other containers) work in such a way that they only contain certain pointers to whatever memory they are managing?[/QUOTE] It is possible. All you have to do is define something like[cpp]Foo& Foo::operator=(Foo&& rhs) { vec = std::move(rhs.vec); return *this; }[/cpp]This will effectively call this same operator on the vector class, which internally more or less just reassigns a pointer and nullifies the other one. The assumption with rvalue-references is, that whenever you receive one, the caller expects the instance to be effecively empty afterwards. That means that your move constructor and move-assignment operators should rip the contents out of the instance and push them into their own, then modify the instance so it does not try to claim any more ownership of such contents. As I said, for vector this would (in simplified terms) mean that the pointer to the underlying heap-allocated array is copied to your instance and set to nullptr on the other one to make sure the destructor will not delete it afterwards. You don't need to care too much about how exactly a built-in type like std::vector implements its move constructor and move-assignment operator, just use std::move to move them and be done with it.
[QUOTE=Dienes;45489613][...] std::vector [...][/QUOTE] Ohh, [I]that[/I] kind of vector :v:
This is totally fine then from what I understand right? [cpp]newNode.nodes = std::move(currentNode->nodes);[/cpp] It should clear the currentNode's nodes and the newNode should now own them (I read that vector's move assignment clears the vector).
Im working with hl2 trying to add animation based ironsights to my mod, but Im having trouble as to why my weapon when in ironsights, has weird pausing between shots. [code]if ( !bSighted && (pOwner->m_nButtons & IN_ATTACK2 ) ) { bSighted = true; SendWeaponAnim( ACT_VM_IRONSIGHT ); m_fSightedReady = gpGlobals->curtime + GetViewModelSequenceDuration(); } else if ( bSighted && !(pOwner->m_nButtons & IN_ATTACK2 ) ) { bSighted = false; SendWeaponAnim( ACT_VM_IDLE ); m_fSightedReady = gpGlobals->curtime + GetViewModelSequenceDuration(); } if ( bSighted ) { if ( gpGlobals->curtime > m_fSightedReady ) { bSighted = true; SendWeaponAnim( ACT_VM_IRONSIGHT ); m_fSightedReady = gpGlobals->curtime + GetViewModelSequenceDuration(); if (bSighted == true && (pOwner->m_afButtonPressed & IN_ATTACK)) { PrimaryAttackIronsight(); } } return; } else if ( bSighted ) { if ( gpGlobals->curtime > m_fSightedReady ) { bSighted = false; SendWeaponAnim( ACT_VM_IDLE ); m_fSightedReady = gpGlobals->curtime + GetViewModelSequenceDuration(); } return; }[/code]
[code]... if ( bSighted ) { ... } else if ( bSighted ) { ... }[/code] The code in the last if is never executed. Is one of those supposed to be if ( !bSighted )? If so, you don't need an else if, just the else. Not sure if this fixes your problem though.
[QUOTE=Fredo;45491562][code]... if ( bSighted ) { ... } else if ( bSighted ) { ... }[/code] The code in the last if is never executed. Is one of those supposed to be if ( !bSighted )? If so, you don't need an else if, just the else. Not sure if this fixes your problem though.[/QUOTE] Ah didnt even notice that, thanks for pointing it out! Unfortunately it doesn't solve my problem.
[code]if ( !bSighted && (pOwner->m_nButtons & IN_ATTACK2 ) ) { ... } else if ( bSighted && !(pOwner->m_nButtons & IN_ATTACK2 ) ) { ... }[/code] Just noticed this as well. Is the second if supposed to be opposite of the first? You have (!A && B) for the first and (A && !B) for the second. The second should be (A || !B) (but like before, just have an else instead of an else if) [url]http://en.wikipedia.org/wiki/De_Morgan%27s_laws[/url]
[QUOTE=Fredo;45491713][code]if ( !bSighted && (pOwner->m_nButtons & IN_ATTACK2 ) ) { ... } else if ( bSighted && !(pOwner->m_nButtons & IN_ATTACK2 ) ) { ... }[/code] Just noticed this as well. Is the second if supposed to be opposite of the first? You have (!A && B) for the first and (A && !B) for the second. The second should be (A || !B) (but like before, just have an else instead of an else if) [url]http://en.wikipedia.org/wiki/De_Morgan%27s_laws[/url][/QUOTE] The second is supposed to be opposite, and it works fine. The problem is within this if statement [code] if ( bSighted ) { if ( gpGlobals->curtime > m_fSightedReady ) { bSighted = true; SendWeaponAnim( ACT_VM_IRONSIGHT ); m_fSightedReady = gpGlobals->curtime + GetViewModelSequenceDuration(); if (bSighted == true && (pOwner->m_afButtonPressed & IN_ATTACK)) { PrimaryAttackIronsight(); } } return; [/code]
There are a couple redundant statements in there: bSighted = true doesn't do anything because it won't get called unless bSighted is already true You also check right after that if bSighted is true As for the problem, it looks like you have to wait GetViewModelSequenceDuration() amount of time before you can fire again. Since you increase the value of m_fSightedReady the second if won't be executed until that time passes. Is this what you want? Is this code run every frame/tick/whatever or is it only on mouse presses/something else?
Its in the ItemPostFrame of my weapon, which updates after every frame, so its constantly checking whether or not to call the PrimaryAttack function.
I was reading about the ECS pattern again recently and had a question arise. Let's say that a system needs two components to function, for example one that moves the entity would need both a position and a velocity. Both components are stored in their own arrays which are tightly packed (I assume that an entity's ID does not necessarily correspond to its component array's index, because then there would be many holes within the array). Since we need both the position and the velocity, would we be causing cache misses? Or is the cache large enough to store both chunks of memory for the position and the velocity?
[QUOTE=WTF Nuke;45493550]I was reading about the ECS pattern again recently and had a question arise. Let's say that a system needs two components to function, for example one that moves the entity would need both a position and a velocity. Both components are stored in their own arrays which are tightly packed (I assume that an entity's ID does not necessarily correspond to its component array's index, because then there would be many holes within the array). Since we need both the position and the velocity, would we be causing cache misses? Or is the cache large enough to store both chunks of memory for the position and the velocity?[/QUOTE] It should be able to store a few disjunct chunks of memory. If not in the L1 cache then definitely in one of the higher-order processor caches. However, if the components are all over the place in the array instead of being somewhat ordered I have no idea how that would behave.
Store meaningful parts together. There's little point in separating position and velocity if they're so often used together. Would you also separate the position X, Y and Z components? An example of when you could benefit from separating position and velocity could be if you're computing the norm of the velocity vector on the CPU as an independent step. In such a case you might want to store the velocities in a continuous array in order to make use of SSE.
How do you use edit and continue in visual c++ 2010 express? I dont see the option in the debug menu.
I have a problem regarding the drawing of outlined objects (using a screen-wide outline shader) when drawing orders are involved. I have layers. I can add objects to a layer and then the layer takes care of drawing them. I can sort objects in a layer based on some criteria, like their y position (for a top-down zelda-like angle game). I can set which layers should be drawn before or after each other. I can also make it so that a particular shader (like an outline shader) is applied to a layer. Now I want to be able to apply a shader to only some objects in a layer. The problem is that this breaks in-layer object ordering. For instance, suppose I have a Main layer where all objects are inserted into and they are all sorted based on their y position, like in the picture below: [IMG]http://puu.sh/ar8fo/a5e7d0e9e3.png[/IMG] Now suppose I wanna add an additional outline using an outline shader to the boxes, or the characters, doesn't matter, as long as it's only one type of object. The way I can do this is to create a canvas/framebuffer, draw only the boxes to it and then draw the canvas using the outline shader. However, since I'd be drawing the boxes all in one go, they'd all be in front of or behind of the rest of the objects in this layer. How would I go about applying a shader to the boxes only without breaking the layer's drawing order? I guess I could apply outlines to objects individually but for some reason this never seems to work properly, either the objects come out all white (which doesn't happen when I apply it to a whole canvas) or it flat out just doesn't work. I assume it's because a fragment shader (like an outline one) only applies itself to the pixels that object touches, but creating an outline touches additional pixels outside it's range so it never works, but I really don't know... Thanks for the help!
[QUOTE=adnzzzzZ;45500807]I have a problem regarding the drawing of outlined objects (using a screen-wide outline shader) when drawing orders are involved. [...][/QUOTE] I think the way it's usually done is using the stencil buffer: When you draw something that's going to be outline, you set a specific bit. When you draw something that shouldn't be outlines, unset that bit. Then you can run your outline shader with the stencil buffer as input, which outlines the group of things selected. If you want to have outlines depth-order correctly (which you probably want for in-game stuff but not for glow effects), you have to expand the quad you use for sprite rendering to include the potential outline and apply it immediately.
Outlining is just an example, I want this to work for any shader. But what is the stencil buffer? (I'm using LÖVE) And how do I expand the quad of a sprite?
[QUOTE=adnzzzzZ;45500938]Outlining is just an example, I want this to work for any shader.[/QUOTE] In that case you should look into applying effects, not complete shaders. The difference is that multiple effects can be combined into a single shader (using some kind of templating engine). (Cache them!) While applying effects, you can also manipulate other data, such as expanding the rendered quad as necessary while moving texture coordinates around to correct for that (though this can be done in the vertex shader too). I don't know any existing shader templating engine, but usually it should be enough to just agree on a bunch of variables that are initialised at the start of the shader, after which the effects manipulating them are pasted into it, after which you translate them to outputs. The graphics driver will cut everything unnecessary during compilation, so you don't have to worry about unused variables even if the initialisation requires some calculations.
I don't know what's the difference between an effect and a shader, but even if I combine everything into one shader, I don't see how this helps me with the main ordering problem. I'll still have to use a canvas and I'll still have to draw that canvas before/after the other non-shadered objects, which will break the draw order.
[QUOTE=adnzzzzZ;45500938][...] But what is the stencil buffer? (I'm using LÖVE) And how do I expand the quad of a sprite?[/QUOTE] The stencil buffer is usually used to "stencil out" shapes by checking for certain values and succeeding/failing based on a comparison function. You can use it to store some additional data though, since you can output flat values to it independently from the shaders. I don't know whether it's available for you, but it would be surprising if it wasn't. I'm not sure about LÖVE, but if you can specify a custom vertex shader, you can use the texture coordinates to find out which corner you're at, moving the position a bit in that direction and also the texture coordinates (by a different value) to preserve the original layout. For that to work you need to have enough room around sprites or use separate textures though, otherwise you have to do a bit of additional logic in the pixel shader to clamp texture lookups to the area in question. I'm assuming you can use custom GLSL shaders though, if that's not the case it could be problematic to use layers the way you currently do. [editline]26th July 2014[/editline] [QUOTE=adnzzzzZ;45501028]I don't know what's the difference between an effect and a shader, but even if I combine everything into one shader, I don't see how this helps me with the main ordering problem. I'll still have to use a canvas and I'll still have to draw that canvas before/after the other non-shadered objects, which will break the draw order.[/QUOTE] An "effect" is basically [I]what you want to accomplish[/I] using the shader, it's possible to composite those for a single draw command. (At least it should be, again no idea about LÖVE.) If that's not possible you may have to draw outline objects twice, one using the outline shader (I assume it turns out white because it draws only the outline) and once normally.
I still don't understand how all this helps me. I understand how the quad thing helps me, but what if I want to apply a screen wide effect and not to individual sprites? I don't get how drawing it twice will help, because I'll still have to draw those two canvases in some order but I won't be able to properly divide each object inside each canvas.
[QUOTE=adnzzzzZ;45501554]I still don't understand how all this helps me. I understand how the quad thing helps me, but what if I want to apply a screen wide effect and not to individual sprites? I don't get how drawing it twice will help, because I'll still have to draw those two canvases in some order but I won't be able to properly divide each object inside each canvas.[/QUOTE] Can you draw a canvas onto another canvas? If yes you should be able to outline some sprite and then draw that outline (or an outlined sprite) onto the main layer canvas before proceeding to the next one. Outlines as screen wide effects generally don't work in 2D with proper depth ordering as screen wide effects since they modify an object's outline. If you had a real depth buffer you could use that to do occlusion, but that may be impossible with (default) LÖVE if it's normally strictly a 2D framework. Other screen wide effects that don't modify the outline may be possible as separate shaders without the extra canvas, but you'd have to be able to use stenceling.
How would I go about storing function pointers where the parameters can change to a derived type? This should illustrate what I want: [cpp]void testFunc(const derivedEvent &){/*...*/} std::function<void(const baseEvent &)> func; func = std::bind(testFunc, std::placeholders::_1);[/cpp] I figure it out but I also have no idea how it works so if anyone could explain that would be great. The way I found to store this information is through the use of a wrapper like so: [cpp]template <typename Event> struct eventWrapper { eventWrapper(std::function<void(const event &)> callback): callback(callback) {} void operator()(const baseEvent* event) { callback(*(static_cast<const Event*>(event))); } std::function<void(const Event &)> callback; };[/cpp] However, I don't know why you can just stuff this wrapper into the std::function, like so: [cpp]std::function<void(baseEvent *)> func; func = EventCallbackWrapper<typeA>(std::bind(testFunc, std::placeholders::_1));[/cpp] Figured this out too. std::functions can be assigned to a class that overrides the () operator.
[QUOTE=Tamschi;45501855] Outlines as screen wide effects generally don't work in 2D with proper depth ordering as screen wide effects since they modify an object's outline. If you had a real depth buffer you could use that to do occlusion, but that may be impossible with (default) LÖVE if it's normally strictly a 2D framework.[/QUOTE] I think I'm gonna go for this solution. I've been meaning to create a depth map of sorts so I can use some other shaders I wanna try out and I think this is the best solution that would work for the most effects I can think of right now.
[QUOTE=adnzzzzZ;45504406]I think I'm gonna go for this solution. I've been meaning to create a depth map of sorts so I can use some other shaders I wanna try out and I think this is the best solution that would work for the most effects I can think of right now.[/QUOTE] If you go for this you may be able to completely avoid having to sort sprites too (as long as they are opaque). Since you already have box collision info for cuboids, you should be able to split the sprite into "front" and "top" faces automatically, giving you pixel-perfect occlusion. More complex sprites could require depth maps, but I suppose that's only necessary if you need special interlocking occlusion, and could lead to weird intersections otherwise.
I'm going for something pretty simple to not over complicate myself later: y position + some sprite based offset, which is the same criteria I use for sorting. So far the depth map seems to look OKish for the purposes I want to use it for: [IMG]http://puu.sh/arMhF/dc3c3c8197.png[/IMG]
[QUOTE=adnzzzzZ;45505001]I'm going for something pretty simple to not over complicate myself later: y position + some sprite based offset, which is the same criteria I use for sorting. So far the depth map seems to look OKish for the purposes I want to use it for: [IMG]http://puu.sh/arMhF/dc3c3c8197.png[/IMG][/QUOTE] Yes, that definitely should work. For the outline shader you'll simply have to check for adjacent/nearby outlined pixels that are closer. The floor needs to have a different value though, either put it behind everything or give it a gradient. [editline]26th July 2014[/editline] You'll probably also have to shift the shadows slightly back, so that they are separated from the characters.
Programmer's block! Okay so I wrote a vector class: [lua] vec = {0, 0, 0} setmetatable(vec, { __index = vec, __add = function(a, b) return vec(a.x+b.x,a.y+b.y,a.z+b.z) end, __sub = function(a, b) return vec(a.x-b.x,a.y-b.y,a.z-b.z) end, __unm = function(a) return vec() - a end, __tostring = function(a) return "vec(".. a.x .. ", ".. a.y.. ", "..a.z.. ")" end, __call = function(x, y, z) return setmetatable({x=x or 0,y=y or 0,z=z or 0}, { __index = vec}) end}) function vec:distance(a) return math.sqrt((a.x - self.x)^2 + (a.y - self.y)^2 + (a.z - self.z)^2) end function vec:unpack() local a = self return a.x, a.y, a.z end function vec:dot(a) local r = 0 for i = 1, #self do r = r + self[i] * a[i] end return r end [/lua] [i]But[/i] when I try to use the distance function: [lua]amp= 1 / ray:distance(vec())[/lua] Lua goes "nope" and overflows: [code] Error main.lua:24: loop in gettable Traceback main.lua:24: in function '__sub' main.lua:37: in function 'distance' *some other unrelated shit I can't be bothered to write down* [/code] ...Fuck. Can someone show me the [i]right[/i] way to do classes?
[QUOTE=Ott;45508687] [lua] vec = {0, 0, 0} -- those are [1], [2], [3] and not x, y, and z as you refer to them in the metamethods setmetatable(vec, { __index = vec, -- __index pointing to original table? does this even make sense? i think this is what makes your table overflow, -- because when you try to index 'x' from a metamethod, it's not found, then it goes into __index which is the very same table, can't find 'x' in it, grabs __index (which, again, is the same table) __add = function(a, b) return vec(a.x+b.x,a.y+b.y,a.z+b.z) end, __sub = function(a, b) return vec(a.x-b.x,a.y-b.y,a.z-b.z) end, __unm = function(a) return vec() - a end, __tostring = function(a) return "vec(".. a.x .. ", ".. a.y.. ", "..a.z.. ")" end, __call = function(x, y, z) return setmetatable({x=x or 0,y=y or 0,z=z or 0}, { __index = vec}) end}) function vec:distance(a) return math.sqrt((a.x - self.x)^2 + (a.y - self.y)^2 + (a.z - self.z)^2) end function vec:unpack() local a = self return a.x, a.y, a.z end function vec:dot(a) local r = 0 for i = 1, #self do r = r + self[i] * a[i] end return r end [/lua] [/QUOTE] Just troubleshooting, maybe it's something else.
Hi there, I'm working on an area system. When player is inside an area (inside areas table) it will set the variable to that name. Now I have to reset the variable to a default string I defined when player exists the area but I can't seem to get it to work. Tried several things, can't get my head around it. [lua]-- Function to check in what area the player is located. function getArea(player) local areas = PLUGIN.areas local default = "Somewhere in the freaking map"; -- Check if any areas where found, and proceed if so. if #areas > 0 then for k = 1, #areas do local v = areas[k] for k2, v2 in pairs(ents.FindInBox(v.pos1, v.pos2)) do if v2:IsPlayer() and v2:Alive() and v2._Area != v.name then -- Set the _Area variable. v2._Area = v.name; end end -- @@ end end end timer.Create("getArea", 1, 0, getArea, player)[/lua] Now, where I put the "-- @@" comment I guess I should reset the variable, but when I tried player._Area = default; it gave an error saying 'player' is a nil value.
Sorry, you need to Log In to post a reply to this thread.