Using local variables

Podge

New member
Joined
Nov 10, 2019
Messages
26
Hello all,

I am trying to be good and not use evil global variables, as I know how horrible this can get.
What I'm having trouble with is that if, I want to create a method to say, just draw a title screen, I have to do this in 2 parts.

On part renders, the other updates. While I understand the logic in this, I don't really see how I can use local variables.

So I have two questions.
1. does my question make sense?
2. Any suggestions?

Method render_board_select:Void()
Local x:Int = 1000
DrawImage(board_img, x, 0)
End Method

Method update_board_select:Void()
If x > 0 Then x=x-10
End Method

Many thanks,

Kurt
 
Last edited:

Phil7

Administrator
CX Code Contributor
3rd Party Tool Dev
Joined
Jun 26, 2017
Messages
643
@1. Your question completely makes sense. First of all as a rule of thumb you cannot avoid all global variables, but you should where it is possible. The scope of your variable should be as small as you can make it.

@2. You could create a Class called Board where you have x as a Field (a variable that only belongs to an instance of the Class). You can then use this Field from each Method in the Class.

Cerberus:
Strict

Import mojo


Function Main:Int()
    New MyApp()
    Return 0
End


Class MyApp Extends App   
    Field myBoard:Board
    
    
    Method OnCreate:Int()
        myBoard = New Board()
        Return 0
    End       
    
    
    Method OnUpdate:Int()
        myBoard.Update()
        Return 0
    End
    
    
    Method OnRender:Int()
        Cls()
        myBoard.Render()
        Return 0
    End   

End


Class Board
    Field x:Float = 500 
    
    
    Method Update:Void()
        If x > 0 Then x = x-1
    End
    
    
    Method Render:Void()
        DrawRect(x, 50, 30, 50)
    End
    
End
 

Podge

New member
Joined
Nov 10, 2019
Messages
26
Hello Phil,

Thanks a lot, I'm going to go with this!

Cheers,

Kurt
 

Jimmy

Active member
3rd Party Module Dev
Tutorial Author
Joined
Jan 2, 2020
Messages
912
I had the same problem when I started coding languages that uses this style of storage.
I'm not sure if my example would be as flexible as Phil7's but I avoided global using fields instead of locals.
To do a class is probably the next logical step but I sometimes prefer the more direct usage of fields like this.

Fields seem to have the same speed as locals but it was a mental hurdle to get into this kind of thinking.
It's the same kind of mental stepping stone as when you reliase that local actually only initialise variables the first time they are executed not every loop.

Code:
Strict
Import mojo

Function Main:Int()
    New MyApp()
    Return 0
End

Class MyApp Extends App 

    Field x:Float = 500

    Method OnCreate:Int()
        Return 0
    End     

    Method OnUpdate:Int()
    If x > 0 Then x = x-1
        Return 0
    End

    Method OnRender:Int()
        Cls
        DrawRect x, 50, 30, 50
        Return 0
    End 
  
End
 

Podge

New member
Joined
Nov 10, 2019
Messages
26
Hello Jimmy,
Thank you, this is also very helpful. My main issue is that I am used to using structs in C rather than objects and classes. I use OO languages and do everything possible to avoid oo :)

Cheers,

Kurt
 

Jimmy

Active member
3rd Party Module Dev
Tutorial Author
Joined
Jan 2, 2020
Messages
912
I'm not sure this will be as fast as the one I showd you first, I will make some speedtests later becuase that would be interesting to know.
This is in my view how you would simulate strucs in Cerberus, it sortof is a mix of Phil7's example and my take on it.

Code:
Import mojo

Function Main:Int()
    New MyApp()
    Return 0
End

Class MyApp Extends App 

    Field myBoard:Board
  
    Method OnCreate:Int()
        myBoard = New Board()
        Return 0
    End     
  
    Method OnUpdate:Int()
        If myBoard.x > 0 Then myBoard.x = myBoard.x-1
        Return 0
    End
  
    Method OnRender:Int()
        Cls
        DrawRect myBoard.x, 50, 30, 50
        Return 0
    End 

End

Class Board ' Board could be seen as a named "Struc" where you can add more variables/data and organise it

    Field x:Float = 500
  
End
 

Podge

New member
Joined
Nov 10, 2019
Messages
26
I'm not sure this will be as fast as the one I showd you first, I will make some speedtests later becuase that would be interesting to know.
This is in my view how you would simulate strucs in Cerberus, it sortof is a mix of Phil7's example and my take on it.

Code:
Import mojo

Function Main:Int()
    New MyApp()
    Return 0
End

Class MyApp Extends App

    Field myBoard:Board
 
    Method OnCreate:Int()
        myBoard = New Board()
        Return 0
    End    
 
    Method OnUpdate:Int()
        If myBoard.x > 0 Then myBoard.x = myBoard.x-1
        Return 0
    End
 
    Method OnRender:Int()
        Cls
        DrawRect myBoard.x, 50, 30, 50
        Return 0
    End

End

Class Board ' Board could be seen as a named "Struc" where you can add more variables/data and organise it

    Field x:Float = 500
 
End
Thanks again man. It's starting to click :)
 
Top Bottom