The Mugen Fighters Guild

Help => M.U.G.E.N Development Help => Code Library => Topic started by: Dr.Chapatín on November 02, 2014, 05:06:48 pm

Title: The best way to economize Vars (Bit-flag)
Post by: Dr.Chapatín on November 02, 2014, 05:06:48 pm
"Simple Megaman example"

[_1_][_2_][_3_]
[_4_][_0_][_5_]
[_6_][_7_][_8_]

States 1000-1999 -> Ringman's stuff
Code:
...
[Statedef 1999]; Get equipped with
type=S

[State 1]
type=Varadd
trigger1 = !Time
v=1
value = 2 ** 1 ;<--[_1_]
...

States 2000-2999 -> Diveman's stuff
Code:
...
[Statedef 2999]; Get equipped with
type=S

[State 2]
type=Varadd
trigger1 = !Time
v=1
value = 2 ** 2 ;<--[_2_]
...

States 3000-3999 -> Skullman's stuff
Code:
...
[Statedef 3999]; Get equipped with
type=S

[State 3]
type=Varadd
trigger1 = !Time
v=1
value = 2 ** 3 ;<--[_3_]
...

States 4000-4999 -> Pharaohman's stuff
Code:
...
[Statedef 4999]; Get equipped with
type=S

[State 4]
type=Varadd
trigger1 = !Time
v=1
value = 2 ** 4 ;<--[_4_]
...

States 5000-5999 -> Brightman's stuff
Code:
...
[Statedef 5999]; Get equipped with
type=S

[State 5]
type=Varadd
trigger1 = !Time
v=1
value = 2 ** 5 ;<--[_5_]
...

States 6000-6999 -> Toadman's stuff
Code:
...
[Statedef 1999]; Get equipped with
type=S

[State 6]
type=Varadd
trigger1 = !Time
v=1
value = 2 ** 6 ;<--[_6_]
...

States 7000-7999 -> Drillman's stuff
Code:
...
[Statedef 7999]; Get equipped with
type=S

[State 7]
type=Varadd
trigger1 = !Time
v=1
value = 2 ** 7 ;<--[_7_]
...

States 8000-8999 -> Dustman's stuff
Code:
...
[Statedef 8999]; Get equipped with
type=S

[State 8]
type=Varadd
trigger1 = !Time
v=1
value = 2 ** 8 ;<--[_8_]
...

States 10000-10999 -> Painel
Code:
...
[Statedef 10000] ;Show the weapons
type=S

[State 1]; Ringman
type=Explod
triggerall=!Time
trigger1 = ( Var(1) > = 2 ** 1 )  ;Must have this value [_1_]
trigger1 = ( Var(1) % ( 2*(2 ** 1) ) >=( 2 ** 1 ) ) ;Study ;p
anim=801
...
[State 2]; Diveman
type=Explod
triggerall=!Time
trigger1 = ( Var(1) > = 2 ** 2 )  ;Must have this value [_2_]
trigger1 = ( Var(1) % ( 2*(2 ** 2) ) >=( 2 ** 2 ) ) ;Study ;p
anim=802
...
[State 3]; Skullman
type=Explod
triggerall=!Time
trigger1 = ( Var(1) > = 2 ** 3 )  ;Must have this value [_3_]
trigger1 = ( Var(1) % ( 2*(2 ** 3) ) >=( 2 ** 3 ) ) ;Study ;p
anim=803
...
[State 4]; Pharaohman
type=Explod
triggerall=!Time
trigger1 = ( Var(1) > = 2 ** 4 )  ;Must have this value [_4_]
trigger1 = ( Var(1) % ( 2*(2 ** 4) ) >=( 2 ** 4 ) ) ;Study ;p
anim=804
...
[State 5]; Brightman
type=Explod
triggerall=!Time
trigger1 = ( Var(1) > = 2 ** 5 )  ;Must have this value [_5_]
trigger1 = ( Var(1) % ( 2*(2 ** 5) ) >=( 2 ** 5 ) ) ;Study ;p
anim=805
...
[State 6]; Toadman
type=Explod
triggerall=!Time
trigger1 = ( Var(1) > = 2 ** 6 )  ;Must have this value [_6_]
trigger1 = ( Var(1) % ( 2*(2 ** 6) ) >=( 2 ** 6 ) ) ;Study ;p
anim=806
...
[State 7]; Drillman
type=Explod
triggerall=!Time
trigger1 = ( Var(1) > = 2 ** 7 )  ;Must have this value [_7_]
trigger1 = ( Var(1) % ( 2*(2 ** 7) ) >=( 2 ** 7 ) ) ;Study ;p
anim=807
...
[State 8]; Dustlman
type=Explod
triggerall=!Time
trigger1 = ( Var(1) > = 2 ** 8 )  ;Must have this value [_8_]
trigger1 = ( Var(1) % ( 2*(2 ** 8) ) >=( 2 ** 8 ) ) ;Study ;p
anim=808
...

Whatever the order... The values are save correctly.

Used in this projects:  ;)



cya
Title: Re: The best way to economize Vars (Bit-flag)
Post by: Алексей on November 02, 2014, 10:07:21 pm
I'm not sure why I'd want to do this. You're still setting the same var with a static number, so it's really just over complicating the hard-coding of simple values. Vars can hold up to the maximum size of a 32-bit integer, so this seems kind of counter-intuitive.
Title: Re: The best way to economize Vars (Bit-flag)
Post by: Jesuszilla on November 03, 2014, 12:43:28 am
No it's not. It's basically making one var hold a lot of booleans because all you need is 1 bit for a boolean, so there's no need to waste an entire variable on it.


This is the exact same thing as bitwise variables (http://mugenguild.com/forum/topics/bitwise-variables-demystified-137076.msg1482141.html#msg1482141), so this is old news.
Title: Re: The best way to economize Vars (Bit-flag)
Post by: Bastard Mami on November 03, 2014, 01:36:47 am
op also needs to memorize the powers of 2. any programmer worth it's salt knows them by memory.
Title: Re: The best way to economize Vars (Bit-flag)
Post by: Алексей on November 03, 2014, 01:38:47 pm
No it's not. It's basically making one var hold a lot of booleans because all you need is 1 bit for a boolean, so there's no need to waste an entire variable on it.
Yes, but that's for booleans. Just set one variables to a bunch of different numbers and you have exactly the same result without the mess.

Code:
var(1):=1
var(1):=2
var(1):=3

var(1) = 1 ; Do something
var(1) = 2 ; Do something else
var(1) = 3 ; Do something entirely different.

He's still hard-coding values regardless of being boolean or not. It's easier on the engine to do less calculations, so just go down the traditional route above and you'll be fine. There's no need to try bringing extra programming concepts like this into mugen. It's not actually any faster. In terms of convenience, I've done bitwise operations as well, but it was actually used in a way that couldn't be replicated in any other way with the same simplicity. This over-complicates things.
Title: Re: The best way to economize Vars (Bit-flag)
Post by: Dr.Chapatín on November 05, 2014, 10:10:41 pm
Code:
var(1):=1
var(1):=2
var(1):=3

var(1) = 1 ; Do something
var(1) = 2 ; Do something else
var(1) = 3 ; Do something entirely different.
In this case you set only one value each time.

Title: Re: The best way to economize Vars (Bit-flag)
Post by: jade_midori on November 05, 2014, 10:25:08 pm
It's easier on the engine to do less calculations

which is why the bitwise operators &, | and ^ exist, they simplify their triggers, for example if I wanted to check toadman's ability I'd just use "trigger1 = var(1)&64" instead of what was indicated by OP and to set that same flag I'd use var(1):=(var(1)|64) and to turn it off I'd use var(1):=(var(1)^64)
Title: Re: The best way to economize Vars (Bit-flag)
Post by: Sheng Long on November 06, 2014, 12:32:02 am
Why are you using states that are used for more common states like getting hit (5000-5999) and so forth?

Some of this doesn't make sense to me. But I haven't messed with Mugen in months. So I donno. XP
Title: Re: The best way to economize Vars (Bit-flag)
Post by: Алексей on November 06, 2014, 01:21:13 pm
In this case you set only one value each time.
But you are doing the same thing as well. Setting one value each time. You might be using the bits of an int, but you're not really doing it quicker. It's not saving any time or speeding up the engine's processing using your method. Please explain to me how you think your method is any different than setting one value each time? By nature, vars can only hold one value.

which is why the bitwise operators &, | and ^ exist, they simplify their triggers, for example if I wanted to check toadman's ability I'd just use "trigger1 = var(1)&64" instead of what was indicated by OP and to set that same flag I'd use var(1):=(var(1)|64) and to turn it off I'd use var(1):=(var(1)^64)
Exactly.

Title: Re: The best way to economize Vars (Bit-flag)
Post by: Jesuszilla on November 09, 2014, 06:54:01 am
This has nothing to do with speed. It has to do with getting around MUGEN's variable limit because if you move all your booleans into one variable, then you have more room in other variables to do other calculations.


And again, this is nothing new. Winane outlined it and I did a more in-depth explanation to help people understand what's going on.
Title: Re: The best way to economize Vars (Bit-flag)
Post by: XGargoyle on November 12, 2014, 10:10:26 pm
Helpers have their own set of variables, so if you run out of variables, just spawn a helper and you'll instantly get an additional 60 ints and 40 floats.

Also, you can use other methods as a substitute to variables, especially for those ON/OFF stuff. Create an Explod or a Helper and use it's ID as a boolean flag. You can even use Animation numbers on a Helper to trigger specific conditions, instead of a variable.




Title: Re: The best way to economize Vars (Bit-flag)
Post by: Jesuszilla on November 12, 2014, 10:28:59 pm
Use projectiles before you resort to helpers for flags. Much less memory consumption.
Title: Re: The best way to economize Vars (Bit-flag)
Post by: Bastard Mami on November 12, 2014, 10:34:45 pm
Helpers have their own set of variables, so if you run out of variables, just spawn a helper and you'll instantly get an additional 60 ints and 40 floats.

Also, you can use other methods as a substitute to variables, especially for those ON/OFF stuff. Create an Explod or a Helper and use it's ID as a boolean flag. You can even use Animation numbers on a Helper to trigger specific conditions, instead of a variable.
agreed, those methods should be used after the bitflag one, tho.