Games Access to libGDX in a CodeModule

Space

Member
Licensed User
Hi,

If I have, for example, the following code in 'main' that is located in LG_Create:

B4X:
Font = FG.CreateFontWithParam( lGdx.Files.internal( "testfont.otf ), Param)

Is it working perfectly, because I can access the lGdx variable.
But what do I do in a CodeModule?
How do I get the lGdx variable into the CodeModule to use it?"

thanks
 

Space

Member
Licensed User
That was also my first attempt.

for example:

B4X:
Sub Process_Globals
    Private lGdx As LibGDX
End Sub

public Sub init( l As LibGDX )
    lGdx = l
End Sub

public Sub something
    Dim Param As lgFontGeneratorParameters
    Param.Size = 20 * Density
    Dim FG As lgFontGenerator
    Dim Font As lgBitmapFont
    Font = FG.CreateFontWithParam(lGdx.Files.internal("font/liquidcrystal.otf"), Param)
End Sub

But with error:

Error description:
Cannot access activity object from sub Process_Globals.
Error occurred on line:2
 

Space

Member
Licensed User
Ilan, I see what you mean.

B4X:
public Sub something( lGdx As LibGDX )
    Dim Param As lgFontGeneratorParameters
    Param.Size = 20 * Density
    Dim FG As lgFontGenerator
    Dim Font As lgBitmapFont
    Font = FG.CreateFontWithParam(lGdx.Files.internal("font/liquidcrystal.otf"), Param)
End Sub

But then I have to pass the lgdx variable from sub to sub. I have a gfx code module with functions that are called from the whole program. It would be great if I could store the lgdx variable. Only how?
 

Space

Member
Licensed User
Sorry, but unfortunately it still doesn't work.

I have made a standard class: (test.bas)

B4X:
Sub Class_Globals
    Private lGdx As LibGDX
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize (    plGdx As LibGDX )
    lGdx = plGdx
End Sub

And in Process_Globals in main if I write "Private tt As test"
The error appears:
Main - 38: Cannot access activity object from sub Process_Globals.

Any suggestions please?
 

Space

Member
Licensed User
Does that have anything to do with it? ( From: Introduction to the libGDX library )

ttttt.png
 

ilan

Expert
Licensed User
Longtime User
can you upload a small example that demonstrate your issue?
it will be easier to test it like this.
 

Space

Member
Licensed User
As you wrote, classes are the solution. However, I wrote "Private tt As test" in Process_Globals, which causes an error. As soon as I wrote it in Globals, it works.

I will have to investigate the difference between Process_Globals and Globals.

Thank you Ilan
 

ilan

Expert
Licensed User
Longtime User
You can also try to set the libgdx object in main activity as PUBLIC. And then use
Main.lgdx…
Without to pass the object to functions.
That should work too.
 

Space

Member
Licensed User
So either you're right or I am. Personally, I would bet on you being right, but in my test program lgdx is not visible from the test class using "main."
 

Attachments

  • t1.png
    t1.png
    9.6 KB · Views: 108
  • t2.png
    t2.png
    13.3 KB · Views: 107
  • BitmapFont.zip
    13.5 KB · Views: 107

ilan

Expert
Licensed User
Longtime User
I would bet on you being right, but in my test program lgdx is not visible from the test class using "main."
yes you are right. it cannot be accessed from Globals .

so what exactly is your problem now?
if you create a class and you initialize the font you can then return it from the class and use it.

i have updated the example. check it out and see if it helps you. (i have not tested it!)
 

Attachments

  • fontexample2.zip
    7.6 KB · Views: 108

Space

Member
Licensed User
My issue was resolved at post number 9. I was just confused because you wrote about the global and public. Thank you very much!
 
Top