## Get digit of a number

Moderators: cry-nic, Community Volunteers

### Get digit of a number

#1
Hi, I try to get the first digit of a number, is it possible with flowgraph ?

### Re: Get digit of a number

#2
Now I'm no expert in flowgraph (I haven't done any visual programming in CRYENGINE yet), but it should be pretty easy to create something in C++ then expose it to flowgraph for your needs (or use available nodes? I'm not certain of how to do loops or conditional statements in flowgraph or even if you can).
Basically, you should run the number through a loop with the condition that the number divided by 10 is greater than 1. After the loop is finished you should have the first number of your original number, followed a decimal and a bunch of other useless numbers. Converting it to an integer should remove the extra numbers and return only the one integer that was the first number of your larger number.
{
double var = 568795; // original number, take as input
double n_var = var; // placeholder number
while (n_var / 10 >= 1) { n_var = n_var / 10; } // run through loop to reduce number
return int( n_var ); // return the first digit of your string of numbers, cleaned as an integer
}
Alternatively, if your number is lower than 1 (like 0.00568795), you can simply change the loop to be multiplied by 10 instead of divided, and shift the sign of the boolean conditional around.
Anyone with knowledge of flowgraph know if such simple processes are possible off the top of their heads?

### Re: Get digit of a number

#3
Taking a quick look at flowgraph (documentation), you can probably have a set of conditionals to define the value of a number. Basically, take the input number, check if it is greater than 1 (or 1 is less than it): if it isn't, divide it by ten then send it to a special conditional, if it is, return the number.
The special conditional should look something like the one in here: https://www.cryengine.com/community_arc ... 2&t=127801
Basically, just replace the loop I gave in the previous answer with some logic gates: https://www.cryengine.com/community_arc ... 22&t=83619

However, as a coder, I still think the way to go is c++ -> exposure to flowgraph as a node. After you've made your first custom flowgraph node, the process is actually easier to just make more finicky behavior in C++ than with a whole bunch of nodes, but to each their own.

### Re: Get digit of a number

#4
Sorry denalti I make a mistake I want the second digit of a number and I found the solution.
For the first digit I use a "math in range"
Attachments
Math.floor((15 / 1) % 10);
GetSecondDigit.png (25.54 KiB) Viewed 1732 times

### Re: Get digit of a number

#5
Oh you meant the first number on the right side? I had believed you meant the first number on the left side. (Ex: 12345, 1 instead of 5.)
Sorry for that confusion!

Could you explain why you divided your number by 1?

### Re: Get digit of a number

#6
My fault !
In this case it's useless , but if I want another digit of a big number like 123 if I want the second digit I must divide by 10.
I just keep it for later

I share the link where I found this solution: Holy stackoverflow xD
----> https://stackoverflow.com/questions/139 ... m-a-number

### Re: Get digit of a number

#7
Might be a bit late, but better late then never

Here is a quick and dirty way in Flowgraph to get the number:

Code: Select all

```<Graph Description="" Group="FG"> <Nodes> <Node Id="60" Class="Game:Start" pos="-774,666,0"> <Inputs InGame="1" InEditor="1"/> </Node> <Node Id="61" Class="Math:Less" pos="0,468,0"> <Inputs A="0" B="10"/> </Node> <Node Id="62" Class="Math:Calculate" pos="234,774,0"> <Inputs Op="3" A="0" B="10"/> </Node> <Node Id="64" Class="Logic:Any" pos="-234,900,0"> <Inputs /> </Node> <Node Id="66" Class="Logic:Any" pos="270,666,0" InHideMask="1023" OutHideMask="1"> <Inputs /> </Node> <Node Id="67" Class="Math:SetInteger" pos="-576,666,0"> <Inputs in="678756500"/> </Node> <Node Id="68" Name="loop_numbers_through_here" Class="_comment" pos="270,648,0"/> <Node Id="69" Name="Set_final_number_if_input_is_lower_then_10" Class="_comment" pos="0,468,0"/> <Node Id="70" Class="Math:SetInteger" pos="522,486,0"> <Inputs in="0"/> </Node> <Node Id="71" Class="Logic:Any" pos="720,486,0"> <Inputs /> </Node> <Node Id="72" Class="Logic:Any" pos="504,918,0" InHideMask="1023" OutHideMask="1"> <Inputs /> </Node> <Node Id="73" Class="Logic:Any" pos="-234,468,0"> <Inputs /> </Node> <Node Id="89" Name="Input_number" Class="_comment" pos="-576,648,0"/> <Node Id="90" Name="Get_First_Number" Class="_commentbox" pos="-792,396.34296,0"> <Inputs TextSize="1" Color="1,1,1" DisplayFilled="1" DisplayBox="1" SortPriority="16"/> <ResizeBorder X="0" Y="0" Width="1674" Height="666"/> <NodeSize Width="1674" Height="721.51636"/> </Node> <Node Id="91" Name="Output_number" Class="_comment" pos="720,468,0"/> </Nodes> <Edges> <Edge nodeIn="67" nodeOut="60" portIn="set" portOut="output" enabled="1"/> <Edge nodeIn="62" nodeOut="61" portIn="DoCalc" portOut="false" enabled="1"/> <Edge nodeIn="70" nodeOut="61" portIn="set" portOut="true" enabled="1"/> <Edge nodeIn="72" nodeOut="62" portIn="in1" portOut="out" enabled="1"/> <Edge nodeIn="62" nodeOut="64" portIn="A" portOut="out" enabled="1"/> <Edge nodeIn="66" nodeOut="64" portIn="in2" portOut="out" enabled="1"/> <Edge nodeIn="73" nodeOut="64" portIn="in2" portOut="out" enabled="1"/> <Edge nodeIn="70" nodeOut="66" portIn="in" portOut="out" enabled="1"/> <Edge nodeIn="64" nodeOut="67" portIn="in1" portOut="out" enabled="1"/> <Edge nodeIn="66" nodeOut="67" portIn="in1" portOut="out" enabled="1"/> <Edge nodeIn="73" nodeOut="67" portIn="in1" portOut="out" enabled="1"/> <Edge nodeIn="71" nodeOut="70" portIn="in1" portOut="out" enabled="1"/> <Edge nodeIn="64" nodeOut="72" portIn="in2" portOut="out" enabled="1"/> <Edge nodeIn="61" nodeOut="73" portIn="A" portOut="out" enabled="1"/> </Edges> </Graph> ```

Here another with settings:

Code: Select all

```<Graph Description="" Group="FG"> <Nodes> <Node Id="4" Class="Game:Start" pos="-990,-288,0"> <Inputs InGame="1" InEditor="1"/> </Node> <Node Id="14" Class="Math:Calculate" pos="72,-72,0"> <Inputs Op="3" A="0" B="10"/> </Node> <Node Id="20" Name="Input_number" Class="_comment" pos="-594,-198,0"/> <Node Id="23" Class="Logic:Any" pos="-396,0,0"> <Inputs /> </Node> <Node Id="26" Name="Output_number" Class="_comment" pos="468,-270,0"/> <Node Id="28" Class="Logic:Any" pos="144,-180,0" InHideMask="1023" OutHideMask="1"> <Inputs /> </Node> <Node Id="30" Class="Math:SetInteger" pos="-594,-180,0"> <Inputs in="678756500"/> </Node> <Node Id="31" Name="loop_numbers_through_here" Class="_comment" pos="144,-198,0"/> <Node Id="32" Name="Set_Output_number_if_input_matches_the_settings" Class="_comment" pos="-126,-270,0"/> <Node Id="34" Class="Math:SetInteger" pos="468,-252,0"> <Inputs in="0"/> </Node> <Node Id="36" Class="Logic:Any" pos="684,-252,0"> <Inputs /> </Node> <Node Id="40" Class="Logic:Any" pos="342,18,0" InHideMask="1023" OutHideMask="1"> <Inputs /> </Node> <Node Id="42" Class="Logic:Any" pos="-396,-378,0"> <Inputs /> </Node> <Node Id="43" Name="Get_Number" Class="_commentbox" pos="-792,-675.87579,0"> <Inputs TextSize="1" Color="1,1,1" DisplayFilled="1" DisplayBox="1" SortPriority="16"/> <ResizeBorder X="0" Y="0" Width="1638" Height="846"/> <NodeSize Width="1638" Height="901.51636"/> </Node> <Node Id="46" Class="Math:SetInteger" pos="-738,-576,0"> <Inputs in="3"/> </Node> <Node Id="49" Name="How_many_numbers_to_get?" Class="_comment" pos="-738,-594,0"/> <Node Id="53" Class="Time:FrameDelay" pos="-774,-180,0"> <Inputs /> </Node> <Node Id="59" Class="Math:LessCheck" pos="-126,-252,0"> <Inputs A="0" B="0"/> </Node> <Node Id="75" Class="Logic:DeMultiplexer" pos="-414,-576,0"> <Inputs Index="0" Mode="0"/> </Node> <Node Id="77" Class="Math:SetNumber" pos="-144,-594,0"> <Inputs in="10"/> </Node> <Node Id="78" Class="Math:SetNumber" pos="-144,-540,0"> <Inputs in="100"/> </Node> <Node Id="79" Class="Math:SetNumber" pos="-144,-486,0"> <Inputs in="1000"/> </Node> <Node Id="80" Class="Math:SetNumber" pos="-144,-432,0"> <Inputs in="10000"/> </Node> <Node Id="82" Class="Logic:Any" pos="54,-540,0"> <Inputs /> </Node> <Node Id="84" Class="Math:Sub" pos="-540,-576,0"> <Inputs A="0" B="1"/> </Node> <Node Id="85" Name="Settings" Class="_comment" pos="-738,-612,0"/> <Node Id="87" Class="Logic:Any" pos="-774,-288,0" InHideMask="1023" OutHideMask="1"> <Inputs /> </Node> <Node Id="88" Name="GET_NUMBER!" Class="_comment" pos="-774,-306,0"/> </Nodes> <Edges> <Edge nodeIn="87" nodeOut="4" portIn="in1" portOut="output" enabled="1"/> <Edge nodeIn="40" nodeOut="14" portIn="in1" portOut="out" enabled="1"/> <Edge nodeIn="14" nodeOut="23" portIn="A" portOut="out" enabled="1"/> <Edge nodeIn="28" nodeOut="23" portIn="in2" portOut="out" enabled="1"/> <Edge nodeIn="42" nodeOut="23" portIn="in2" portOut="out" enabled="1"/> <Edge nodeIn="34" nodeOut="28" portIn="in" portOut="out" enabled="1"/> <Edge nodeIn="23" nodeOut="30" portIn="in1" portOut="out" enabled="1"/> <Edge nodeIn="28" nodeOut="30" portIn="in1" portOut="out" enabled="1"/> <Edge nodeIn="42" nodeOut="30" portIn="in1" portOut="out" enabled="1"/> <Edge nodeIn="36" nodeOut="34" portIn="in1" portOut="out" enabled="1"/> <Edge nodeIn="23" nodeOut="40" portIn="in2" portOut="out" enabled="1"/> <Edge nodeIn="59" nodeOut="42" portIn="A" portOut="out" enabled="1"/> <Edge nodeIn="59" nodeOut="42" portIn="Check" portOut="out" enabled="1"/> <Edge nodeIn="84" nodeOut="46" portIn="A" portOut="out" enabled="1"/> <Edge nodeIn="30" nodeOut="53" portIn="set" portOut="out" enabled="1"/> <Edge nodeIn="14" nodeOut="59" portIn="DoCalc" portOut="false" enabled="1"/> <Edge nodeIn="34" nodeOut="59" portIn="set" portOut="true" enabled="1"/> <Edge nodeIn="77" nodeOut="75" portIn="set" portOut="Port0" enabled="1"/> <Edge nodeIn="78" nodeOut="75" portIn="set" portOut="Port1" enabled="1"/> <Edge nodeIn="79" nodeOut="75" portIn="set" portOut="Port2" enabled="1"/> <Edge nodeIn="80" nodeOut="75" portIn="set" portOut="Port3" enabled="1"/> <Edge nodeIn="82" nodeOut="77" portIn="in1" portOut="out" enabled="1"/> <Edge nodeIn="82" nodeOut="78" portIn="in2" portOut="out" enabled="1"/> <Edge nodeIn="82" nodeOut="79" portIn="in3" portOut="out" enabled="1"/> <Edge nodeIn="82" nodeOut="80" portIn="in4" portOut="out" enabled="1"/> <Edge nodeIn="59" nodeOut="82" portIn="B" portOut="out" enabled="1"/> <Edge nodeIn="75" nodeOut="84" portIn="Index" portOut="out" enabled="1"/> <Edge nodeIn="46" nodeOut="87" portIn="set" portOut="out" enabled="1"/> <Edge nodeIn="53" nodeOut="87" portIn="in" portOut="out" enabled="1"/> </Edges> </Graph> ```

If you want to use one FlowNode here is the C++ SubString FlowNode:
Int1 = Start retrieving from
Int2 = letters to retrieve

Code: Select all

``` class CFlowSubstringNode : public CFlowBaseNode<eNCT_Singleton> { public: CFlowSubstringNode(SActivationInfo* pActInfo) {}; virtual void GetConfiguration(SFlowNodeConfig& config) { static const SInputPortConfig in_config[] = { InputPortConfig_Void("Get", _HELP("Substring, retrieves letters from defined string.")), InputPortConfig<string>("String", _HELP("String to retrieve letters from")), InputPortConfig<int>("Int1", _HELP("Start retrieving from")), InputPortConfig<int>("Int2", _HELP("Number of letters to retrieve")), { 0 } }; static const SOutputPortConfig out_config[] = { OutputPortConfig<string>("Out"), { 0 } }; config.sDescription = _HELP("Substring"); config.pInputPorts = in_config; config.pOutputPorts = out_config; config.SetCategory(EFLN_APPROVED); } virtual void ProcessEvent(EFlowEvent evt, SActivationInfo* pActInfo) { switch (evt) { case eFE_Activate: if (IsPortActive(pActInfo, 0)) { string str1 = GetPortString(pActInfo, 1); int int1 = GetPortInt(pActInfo, 2); int int2 = GetPortInt(pActInfo, 3); if (int1 + int2 > str1.length()) { CryWarning( VALIDATOR_MODULE_FLOWGRAPH, VALIDATOR_WARNING, "Failed to get %d characters starting from %d in a string %d characters long.", int2, int1, str1.length()); } ActivateOutput(pActInfo, 0, str1.substr(int1, int2)); } break; } }; virtual void GetMemoryUsage(ICrySizer* s) const { s->Add(*this); } }; //Include Flownode Registering PLUGIN_FLOWNODE_REGISTER //Register Flownode REGISTER_FLOW_NODE("String:Substring", CFlowSubstringNode) ```

### Re: Get digit of a number

#8
Hi, I try to get the first digit of a number, is it possible with flowgraph ?
could you explain why you need to get different digits of numbers, seems odd for any reason, there is probably a better solution, I am willing to help you out.

### Re: Get digit of a number

#9
Thanks Chris, Good work !
In my case it's very easy because I just need the last digit of a decade
clockFG.png (59.48 KiB) Viewed 1076 times

So I just make a divide by ten and make a "floor math"
I need this for my real time clock to change material dynamically
And it work very well !
Attachments
clock.png (2.44 MiB) Viewed 1076 times

### Re: Get digit of a number

#10
much better way to do this kind of thing it get complex fast tho
i commented to try and break up and give hint's

if you can import and understand this you will get good at FG scripting fast