Skyrimfus

call function from a variable

5 posts in this topic

#1 ·  Posted (edited)

Hello. I want to call a function with parameters from a variable.

 

Code so far:

 

$fileRead = FileReadLine("C:\users\test.txt",1)


Call($fileRead)



Func _testFunc($a)

Msgbox(0,"Box",$a)


EndFunc



EDIT: the test.txt file contains: _testFunc(222)

It doesn't work

Edited by Skyrimfus

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

This works for me, if line 1 of my text file reads _myFunc:

Local $sLine = FileReadLine(@DesktopDir & "\Test.txt", 1)

Call($sLine)

Func _myFunc()
    MsgBox(0, "Test", "Function called from reading line 1 of my text file")
EndFunc

As to passing your $a variable, as you don't have that predefined that is going to fail anyway. If you had declared it, you could do something like this:

Local $sLine = FileReadLine(@DesktopDir & "\Test.txt", 1)
Local $a = 16
Call($sLine, $a)

Func _myFunc($a)
    ConsoleWrite($a & @CRLF)
    MsgBox($a, "Test", "Function called from reading line 1 of my text file")
EndFunc

 

Edited by JLogan3o13

√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites
8 minutes ago, JLogan3o13 said:

This works for me, if line 1 of my text file reads _myFunc:

Local $sLine = FileReadLine(@DesktopDir & "\Test.txt", 1)

Call($sLine)

Func _myFunc()
    MsgBox(0, "Test", "Function called from reading line 1 of my text file")
EndFunc

As to passing your $a variable, as you don't have that predefined that is going to fail anyway. If you had declared it, you could do something like this:

Local $sLine = FileReadLine(@DesktopDir & "\Test.txt", 1)
Local $a = 16
Call($sLine, $a)

Func _myFunc($a)
    ConsoleWrite($a & @CRLF)
    MsgBox($a, "Test", "Function called from reading line 1 of my text file")
EndFunc

  I have it declared but like this in my Test.txt file.

This is what's inside my Test.txt file

_myFunc(20)

Share this post


Link to post
Share on other sites

You can't do that, as the variables have to be separated by a comma in the Call line. If your line was formatted like this:

Quote

_myFunc, 16

You could do it with StringSplit like this:

Local $sLine = FileReadLine(@DesktopDir & "\Test.txt", 1)
Local $a = 0
Local $aSplit = StringSplit($sLine, ",")
Call($aSplit[1], $aSplit[2])

Func _myFunc($a)
    MsgBox($a, "Test", "Function called from reading line 1 of my text file")
EndFunc

 


√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites
Just now, JLogan3o13 said:

You can't do that, as the variables have to be separated by a comma in the Call line. If your line was formatted like this:

You could do it with StringSplit like this:

Local $sLine = FileReadLine(@DesktopDir & "\Test.txt", 1)
Local $a = 0
Local $aSplit = StringSplit($sLine, ",")
Call($aSplit[1], $aSplit[2])

Func _myFunc($a)
    MsgBox($a, "Test", "Function called from reading line 1 of my text file")
EndFunc

 

Thanks, Didn't think of it this way!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now