Raised This Month: $ Target: $400
 0% 

The type of a variable!!??


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
xbennY
New Member
Join Date: Jan 2014
Old 07-11-2014 , 15:14   The type of a variable!!??
Reply With Quote #1

Is it possible to know the type of a variable using some function?

Example:
Quote:
new bool:variable1;
new variable2;

Function(variable1) ---> returns bool
Function(variable2) ---> returns none or something like this.
*In particular i want to know if a variable is boolean.

Thanks!

Last edited by xbennY; 07-11-2014 at 15:22.
xbennY is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 07-11-2014 , 16:27   Re: The type of a variable!!??
Reply With Quote #2

There is no reason to use such a function since the variable type cannot change at run-time. You'll have to explain what you are actually trying to do because your request doesn't make any sense.
__________________

Last edited by fysiks; 07-11-2014 at 16:29.
fysiks is offline
xbennY
New Member
Join Date: Jan 2014
Old 07-11-2014 , 16:51   Re: The type of a variable!!??
Reply With Quote #3

I want to make ONE function to reset the data in a enum separated, this enum have a lot of type variables.

Example:

PHP Code:
enum eAccount
{
    
bool:Alive,
    
Kills
};


new 
gPlayer[33][eAccount]; 

*If i want to reset the data from "Alive": Reset_gPlayer(Alive); then the function resets that variable(Alive) in all "33"(32) gPlayers
**If i want to reset the data from "Kills": Reset_gPlayer(Kills); then the function resets that variable(Kills) in all "33"(32) gPlayers

Theoretically the function would look something like this:

PHP Code:
Reset_gPlayer(var)
{
    for(new 
033; ++i)
        
gPlayer[i][var] = 0;    

This would work perfectly in other programming languages ​​but it does not works in Pawn because when the variable is Boolean he can not take the value 0 or 1, only the values ​​true and false.
If it had some function that returns the type of the variable, I could just do something like this:

PHP Code:
Reset_gPlayer(var)
{
    for(new 
033; ++i)
        if(
isBool(var))
            
gPlayer[i][var] = false;
        else
            
gPlayer[i][var] = 0;    

I want this to automate my code and makes it cleaner. ---> To not put a lot of "for's" in my project.
I hope you understood my question now! Thanks...!

Last edited by xbennY; 07-11-2014 at 16:54.
xbennY is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 07-11-2014 , 17:05   Re: The type of a variable!!??
Reply With Quote #4

Quote:
Originally Posted by xbennY View Post
This would work perfectly in other programming languages ​​but it does not works in Pawn because when the variable is Boolean he can not take the value 0 or 1, only the values ​​true and false.
That is not true. Pawn is a typeless language therefore, true = 1 and false = 0. Tagging a variable in Pawn is merely artificial unlike a typed language. One exception might be the Float tag.

So, you can simple set a boolean to 0.

IMO, you should never be looping through variables of different types. I believe that in some other languages, you would create multiple functions with different headers to achieve something like this.

Code:
myfunction(bool:bVariable)
myfunction(Float:fVariable)
where each one would use the appropriate underlying code corresponding to the input.

In your case, you can simply make one function to reset kills and another to reset the alive variable.
__________________

Last edited by fysiks; 07-11-2014 at 17:07.
fysiks is offline
klippy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 07-11-2014 , 17:52   Re: The type of a variable!!??
Reply With Quote #5

Quote:
Originally Posted by fysiks View Post
That is not true. Pawn is a typeless language therefore, true = 1 and false = 0. Tagging a variable in Pawn is merely artificial unlike a typed language. One exception might be the Float tag.

So, you can simple set a boolean to 0.

IMO, you should never be looping through variables of different types. I believe that in some other languages, you would create multiple functions with different headers to achieve something like this.

Code:
myfunction(bool:bVariable)
myfunction(Float:fVariable)
where each one would use the appropriate underlying code corresponding to the input.

In your case, you can simply make one function to reset kills and another to reset the alive variable.
He might be able to make use of
PHP Code:
tagof(variable); 
No, it's not function, but I like to use parenthesis with these PAWN operators, even if they are not needed.
klippy is offline
xbennY
New Member
Join Date: Jan 2014
Old 07-11-2014 , 18:00   Re: The type of a variable!!??
Reply With Quote #6

@fysiks
Very nice!!
Was anyone here on the forum at some tutorial (I think that is fixed) who had spoken it.
I will try your tips, thanks!

@KliPPy
What is "tagof"? How does it works?

------------
Another question:
What's the best way to reset all that enum data?

Last edited by xbennY; 07-11-2014 at 19:10.
xbennY is offline
klippy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 07-11-2014 , 20:42   Re: The type of a variable!!??
Reply With Quote #7

Quote:
Originally Posted by xbennY View Post
@fysiks
Very nice!!
Was anyone here on the forum at some tutorial (I think that is fixed) who had spoken it.
I will try your tips, thanks!

@KliPPy
What is "tagof"? How does it works?

------------
Another question:
What's the best way to reset all that enum data?
I just can tell that not much people know about it, but "tagof" operator allows you to get tag of a specific variable. So you could probably do something like this:
PHP Code:
new CustomTagcustom_variable;
new 
tagof(CustomTag): another_variable
although, I doubt that will work at run-time, since it will probably just tag another_variable at compilation time. Tags are also indexed, so they are only a number. Take a look at PAWN Language presentation on their site, there you could find more info about "tagof" operator.
klippy is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 21:10.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode