Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411490 Posts in 69371 Topics- by 58428 Members - Latest Member: shelton786

April 25, 2024, 12:48:52 AM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)General thread for quick questions
Pages: [1] 2 3 ... 69
Print
Author Topic: General thread for quick questions  (Read 135351 times)
oahda
Level 10
*****



View Profile
« on: June 05, 2015, 10:42:53 AM »

Couldn't find one, so I'm making one. General thread for quick questions here so that we (at least I) don't have to create a new thread for everything.

My short question: has C++0x/C++11/C++14 introduced any safe way of getting the name of a class as a string? It's not super necessary, but I want to change my interface for registering custom classes to the script engine from register<T>(nameAsString) (AngelScript needs the name as a string) to just register<T> to make it less superfluous, without forcing the user of my engine to use a macro (tho I'd be okay with a macro getting used in the background to fix this somehow if there is no better way; I'm thinking if maybe modern C++ has an actually guaranteed counterpart of the old type_info::name).
Logged

BorisTheBrave
Level 10
*****


View Profile WWW
« Reply #1 on: June 05, 2015, 11:20:11 AM »

Nothing new, afaik. You can continue to use RTTI. C++ classes don't really have canonical names - typedefs are meant to be equivalent and symbol names are non-portable.

If you don't care about about pretty names you can make a function register<T>() that assigns a unique int to each type registered, which at least gives you *something* you can register with angelscript.
Logged
oahda
Level 10
*****



View Profile
« Reply #2 on: June 05, 2015, 11:21:23 AM »

I just realised some classes might need namespaces or the like anyway, so allowing for the AS name to be different from the C++ name is probably a good thing. At least I managed to cut out two arguments from the function after modifying it, so it's not too bad now.

Now I just need to move the function from the wrong class to the right one. Tongue
Logged

Sik
Level 10
*****


View Profile WWW
« Reply #3 on: June 05, 2015, 04:33:05 PM »

Don't forget the part where the name is not stored in the final executable (because all symbols are stripped) and the linker isn't specific to C++ (it just sees executable code with unresolved addresses) so some hack just for this would need to be implemented. Also the whole base/derived class mess, when you consider how they're implemented internally.

Since we continue with quick questions: normally modulo is used to generate a looping sequence (e.g. x % 10 gives you 0, 1, 2, ... 8, 9, 0, 1 ...) but that doesn't work as soon as the value becomes negative (doesn't help that different languages handle it in different ways). Is there an easy (generic) way to have it working regardless of sign?
Logged
valrus
Level 3
***


View Profile
« Reply #4 on: June 05, 2015, 10:18:21 PM »

Since we continue with quick questions: normally modulo is used to generate a looping sequence (e.g. x % 10 gives you 0, 1, 2, ... 8, 9, 0, 1 ...) but that doesn't work as soon as the value becomes negative (doesn't help that different languages handle it in different ways). Is there an easy (generic) way to have it working regardless of sign?

I think this works for that: x-(y*floor(x/y))
Logged
oahda
Level 10
*****



View Profile
« Reply #5 on: June 06, 2015, 01:04:05 AM »

Don't forget the part where the name is not stored in the final executable (because all symbols are stripped) and the linker isn't specific to C++ (it just sees executable code with unresolved addresses) so some hack just for this would need to be implemented. Also the whole base/derived class mess, when you consider how they're implemented internally.
Well, the question was whether modern C++ had introduced any way of doing this that's NOT a hack, but a standard language/STL feature, much like certain other modern languages, not how I could hack it (which I know is a bad idea). :p
Logged

oahda
Level 10
*****



View Profile
« Reply #6 on: June 06, 2015, 01:06:55 AM »

Since we continue with quick questions: normally modulo is used to generate a looping sequence (e.g. x % 10 gives you 0, 1, 2, ... 8, 9, 0, 1 ...) but that doesn't work as soon as the value becomes negative (doesn't help that different languages handle it in different ways). Is there an easy (generic) way to have it working regardless of sign?
Can't you just make it positive, modulo it, and then multiply by the original sign again, or am I misunderstanding what you want?
Logged

Sik
Level 10
*****


View Profile WWW
« Reply #7 on: June 06, 2015, 07:05:12 AM »

OK, let's say you have an angle of -750º, after correction you'll want it to be 330º. How do you fix that easily without a loop? That's what the question was about =P Anyway:

I think this works for that: x-(y*floor(x/y))

I thought this would work only for a single iteration but nope, it works just fine as usual. Neat. (also probably the flooring is not needed since % is normally integer-only and integer divisions always truncate like that)

EDIT: nevermind I'm dumb, tested it on positive integers only. I tried it against that value I mentioned and it gives me -30º, not 330º. I suppose it still makes it better than a loop since you can then just use a single if after the fact to correct for the issue, but still not fully correct.
Logged
ThemsAllTook
Administrator
Level 10
******



View Profile WWW
« Reply #8 on: June 06, 2015, 08:25:14 AM »

How about (a % b + b) % b?
Logged

oahda
Level 10
*****



View Profile
« Reply #9 on: June 06, 2015, 09:00:40 AM »

Oh, for correcting angles I just use a loop at least. I correct them pretty continuously so they usually don't have to loop more than one iteration every occasional frame or so.
Logged

valrus
Level 3
***


View Profile
« Reply #10 on: June 06, 2015, 12:55:19 PM »

I think this works for that: x-(y*floor(x/y))

I thought this would work only for a single iteration but nope, it works just fine as usual. Neat. (also probably the flooring is not needed since % is normally integer-only and integer divisions always truncate like that)

EDIT: nevermind I'm dumb, tested it on positive integers only. I tried it against that value I mentioned and it gives me -30º, not 330º. I suppose it still makes it better than a loop since you can then just use a single if after the fact to correct for the issue, but still not fully correct.

It would depend, I think, in which direction a language truncates negative division results -- if it truncates down, the result should be 330, if it truncates up, the result should be -30.

Or did you get -30 using "floor"?  That would be weird; I tested it in a few languages and got 330, so that'd suggest that flooring was implemented differently for negative numbers in different languages.
Logged
Sik
Level 10
*****


View Profile WWW
« Reply #11 on: June 06, 2015, 05:15:49 PM »

Wait derp, I misinterpreted floor for some reason =/ OK it works.
Logged
gimymblert
Level 10
*****


The archivest master, leader of all documents


View Profile
« Reply #12 on: June 06, 2015, 06:20:42 PM »

What computer spec you would advise me for CGI modeling within the 4000€ and the 2000€ ranges?
Logged

box_of_dirt
Level 0
*


God will cut you down.


View Profile WWW
« Reply #13 on: June 06, 2015, 10:16:37 PM »

I guess this is the place for this:

I'm trying to implement a finite state machine in Game Maker but I'm getting a compile error I can't figure out.

Player Creation Code:

enum player_state {
standing,
rising,
falling,
hanging
}

lifecount = 3;
jump_height  = 20;
y_pos = y;
on_ground = false;
var state = player_state.standing;

Player Step Code:

switch(state) {

    case standing:
        if keyboard_check_pressed(vk_left) x += 2;
        if keyboard_check_pressed(vk_right) x -= 2;
        
        if keyboard_check_pressed(ord('X')){
            vspeed-=10;
            state_= player_state.rising;
       }
    
    case rising:
        if keyboard_check_released(ord('X')) state_ = player_state.falling;
    
    case falling:
        gravity = 1;
}

When I compile, I get this:
___________________________________________
############################################################################################
FATAL ERROR in
action number 1
of  Step Event0
for object player:

Push :: Execution Error - Variable Get 100003.state(100004, -2147483648)
 at gml_Object_player_StepNormalEvent_1 (line 1) - switch(state) {
############################################################################################

I'm doing something stupid but I'm new with GM and I can't figure out what.
Logged
BorisTheBrave
Level 10
*****


View Profile WWW
« Reply #14 on: June 07, 2015, 01:01:55 AM »

I don't know GM, but I'd bet it's saying there's no "state" member variable as you declared it with var, making it local to the first block of code.
Logged
box_of_dirt
Level 0
*


God will cut you down.


View Profile WWW
« Reply #15 on: June 07, 2015, 07:38:54 AM »

Yeah, that's got it. I thought I had to declare state as a 'var' to set it to a global constant, but I guess I had something else wrong with my declaration the first time I tried it.

Anyways, thank you for your assistance.
Logged
Photon
Level 4
****


View Profile
« Reply #16 on: June 08, 2015, 03:50:05 PM »

Can someone give a practical example of how to use enums? As shocking as this may sound, I haven't really used them before but I feel like I'm missing something that could be rather important. Who, Me? I suppose one of the things I'm trying to understand too is how they are represented. For instance:
Code:
enum dimension {
x;
y;
z;}

Am I understanding correctly that each represents a distinct value that cannot be replicated by anything else? Kind of like mini-classes?
Logged
Glyph
Level 10
*****


Relax! It's all a dream! It HAS to be!


View Profile
« Reply #17 on: June 08, 2015, 04:40:59 PM »

Objects of the enum's 'type' can only hold the values stated. They're good for when you only want a small range of possible values, like a closed set. For example:

enum Sense {
SIGHT,
TOUCH,
HEARING,
TASTE,
SMELL;
}


You don't want any other identifier for something that's a Sense, so you use an enum for it.

Practically speaking, enums are good to use for restricting inputs. Like with a switch-case statement where you don't want just anything hitting default, if you pass an enum's value into it you don't have to worry.

But here's more:

http://crunchify.com/why-and-for-what-should-i-use-enum-java-enum-examples/
Logged


gimymblert
Level 10
*****


The archivest master, leader of all documents


View Profile
« Reply #18 on: June 08, 2015, 05:06:40 PM »

also you can expend the collection easily, useful for state like stuff or tag
Logged

Photon
Level 4
****


View Profile
« Reply #19 on: June 08, 2015, 06:00:35 PM »

So in response to the enum stuff, let's say in my ECS coding I have a virtual controller class/entity for mapping keyboard input. But let's say the input is transferred via component, so you've got something like a KeyComponent class and a couple dozen of them mapping certain keys. These components share the same class but they need a way to be distinguished when passed to the component map of the controller. Via Haxe enum standards, would something like this make sense:

Code:
enum DPad {
    UP;
    DOWN;
    LEFT;
    RIGHT;
}

var comps:Map<EnumValue, Component> = new Map<EnumValue, Component>();
comps.set(UP, key_1);
comps.set(DOWN, key_2);
comps.set(LEFT, key_3);
comps.set(RIGHT, key_4);

Where key_x vars are assumed to be the aforementioned KeyComponent objects passed from elsewhere.
Logged
Pages: [1] 2 3 ... 69
Print
Jump to:  

Theme orange-lt created by panic