Jump to content

How could I convert a string type which I got from editlinewidget to an int type?


photo

Recommended Posts

Posted

How could I convert a string type which I got from editlinewidget to an int type?

 

THANKYOU!

Posted

Hi Zheng Guan,

 

If you use UnigineScript, you easily use this type conversion function:

https://developer.unigine.com/en/docs/2.1.1/scripting/core/types#int_variable

 

 

Please, take into account that parser will return only first non-whitespace characters. In the example below, the log.message function will return 344

/* ... */
Gui gui = engine.getGui();
WidgetEditLine line = new WidgetEditLine(gui, "344 343");
int i = int(line.getText());
log.message("%i\n",i);

We've lost this article in 2.2.1 documentation and will add it ASAP.

Thank you!

Posted

oh thank you!

and now i have another question

why i got an error "object is null in class widgetEditLine * __ptr64 class" when i used the function “line.getText()” as your example?

i defined a widgeteditline in .ui file and set a callback function like this <callback type="changed">Interface::get_day</callback>

when i input numbers in the editline,  the function run correctly but the error came out

what should i do?

Posted

Hi Zheng Guan,

As I can see, you created your own class called Interface and try to call the callback from it.
Well, in this case, you should create a callback redirector for calling callbacks.

 

Look through the "samples/widgets/ui_02.ui" sample or just implement something similar:

I've create ui_test.ui file:

<?xml version="1.0" encoding="utf-8"?>
<ui version="1.0">
<editline name="EditLine::editline" export="1" width="100" capacity="10">
	<callback type="changed" instance="EditLine::instance" string="changed">EditLine::callback_redirector</callback>
</editline>
</ui> 

and added callback_redirector method inside my EditLine class:

class EditLine {

	EditLine instance;
	WidgetEditLine editline;
	UserInterface ui;

	EditLine() {
		// we use this to know what's the instance is linked.
		// this helps if you create 2 WdigetEditLines in UI and want to control them by this class
		instance = this;
		ui = new UserInterface(engine.getGui(),"ui_test.ui");
	}

	// add the editline widget to scene
	void show(int x,int y) {
		Gui gui = engine.getGui();
		gui.addChild(editline,GUI_ALIGN_OVERLAP);
		editline.setPosition(x,y);
	}

	// callback to be called on changing editline
	void changed() {
		int i = int(editline.getText());
		log.message("the line is %i\n", i);
	}

	// redirector. calls corresponding callback by using its name.
	void callback_redirector(string func,EditLine editline){
		editline.call(func);
	}
}; 

in the world script's init() method, I create an instance of EditLIne and call show() method:

EditLine el = new EditLine();
el.show(200,200);

Hope this helps!

Thank you!

×
×
  • Create New...