C#
C#

help

Root Question Message

minjub
minjub10/23/2022
How to make a function stays

I want to make like building mechanic to my game, but if I just "if(buttonclick){drawBlock}", the block just stays when the button is being clicked, and disappear when the button released
qqdev
qqdev10/23/2022
Can you show some code?
minjub
minjub10/23/2022
which part?
cap5lut
cap5lut10/23/2022
u could use a simple boolean variable, which is set to true on button click. during rendering u would check the variable to determine if u want draw the block or not
bool shouldDrawBlock = false;

void OnMouseButtonDown() // ur button click handler
{
  shouldDrawBlock = true;
}

void Render() // ur render method
{
  if (shouldDrawBlock)
  {
    drawBlock();
  }
}
minjub
minjub10/23/2022
but that only works once
minjub
minjub10/23/2022
and the block that drawn are the same
minjub
minjub10/23/2022
I want it to draw difference entity everytime it getclick
cap5lut
cap5lut10/23/2022
hmm, ur description is a bit too vague for me to really help ya, can ya give more details?
minjub
minjub10/23/2022
if I do your way,the block that drawn is the same
minjub
minjub10/23/2022
if I click it again, the nothing will happen
minjub
minjub10/23/2022
(i belive)
cap5lut
cap5lut10/23/2022
well, if ya want it to toggle, then just use shouldDrawBlock = !shouldDrawBlock; in the button click handler
minjub
minjub10/23/2022
but it still only work once right?
minjub
minjub10/23/2022
cuz after it true for once, its going to be flse again
minjub
minjub10/23/2022
which make the block dissappear
cap5lut
cap5lut10/23/2022
that should persist the state unless ur shouldDrawBlock is a local variable instead of a member of ur class
minjub
minjub10/23/2022
wdym?
minjub
minjub10/23/2022
any example code?
cap5lut
cap5lut10/23/2022
something like
public abstract class GameObject
{
    public bool IsVisible { get; set; } = true;

    public abstract void Draw();
}

public class Game
{
    private readonly List<GameObject> _objects = new(); // fill with some actual objects

    public void OnMouseClick()
    {
        GameObject gameObject = GetGameObjectFromMouseCoordsOrSomething();
        gameObject.IsVisible = !gameObject.IsVisible;
    }

    public void Draw()
    {
        foreach (var gameObject in _objects)
        {
            if (gameObject.IsVisible)
            {
                gameObject.Draw();
            }
        }
    }
}
cap5lut
cap5lut10/23/2022
ur block would be an instance of a type that inherits from GameObject and be stored in Game._objects
now each block (and other game objects) have their own visibility state and they will be only rendered if its true.
minjub
minjub10/23/2022
why does it need to an abstract class?
cap5lut
cap5lut10/23/2022
because its a base type that doesnt know how to draw itself, that would be defined by subclasses
cap5lut
cap5lut10/23/2022
and ofc its just a rough example
minjub
minjub10/23/2022
GetGameObjectFromMouseCoordsOrSomething(); part is for the obj pos right?
minjub
minjub10/23/2022
like that?
minjub
minjub10/23/2022
minjub
minjub10/23/2022
now it's doing this
cap5lut
cap5lut10/23/2022
so when u click on the block u actually want to hide it, and when u click on it again u want to show it, right?
cap5lut
cap5lut10/23/2022
if yes, that would mean, that u would first want to find the correct Ground tile u clicked on
// update state
if (Raylib.IsMouseButtonPressed(MouseButton.MOUSE_BUTTON_LEFT))
{
  var pos = Raylib.GetMousePosition();
  foreach (var obj in _objects)
  {
    if (/*check if pos is inside of the tile boundaries*/)
    {
      obj.IsVisible = !tile.IsVisible;
      break;
    }
  }
}

// render scene
foreach (var obj in _objects)
{
  if (obj.IsVisible)
  {
    obj.Draw();
  }
}


i dunno anything about Raylib, so if (Raylib.IsMouseButtonPressed(MouseButton.MOUSE_BUTTON_LEFT)) could cause some flickering, then u would have to write some mechanic to only trigger it on actual changes
cap5lut
cap5lut10/23/2022
for the latter something like:
public class ObservableValue<T>
{
    private T _value;

    public ObservableValue(T initialValue)
    {
        _value = initialValue;
    }
    
    public T Value
    {
        get => _value;
        set
        {
            if (value is null)
            {
                if (_value is null) return;
            }
            else if (value.Equals(_value))
            {
                return;
            }
                
            var oldValue = _value;
            _value = value;
            OnChange?.Invoke(oldValue, value);
        }
    }
    
    public event Action<T, T>? OnChange;
}
Message Not Public

Sign In and Join Server To See

10/24/2022
Message Not Public

Sign In and Join Server To See

10/24/2022
cap5lut
cap5lut10/24/2022
oh, then forget all about the IsVisble, then ya can simple add the new block to object list before rendering
if (Raylib.IsMouseButtonPressed(MouseButton.MOUSE_BUTTON_LEFT))
{
  _objects.Add(new Block {
    Position = Raylib.GetMousePosition()
  });
}
Message Not Public

Sign In and Join Server To See

10/24/2022
Message Not Public

Sign In and Join Server To See

10/24/2022
cap5lut
cap5lut10/24/2022
well i guess u have a drawing method like
    public void Draw()
    {
        foreach (var gameObject in _objects)
        {
            gameObject.Draw();
        }
    }
to draw all ur entities.
by adding the block to the _objects list, it will be rendered on the next frame
ContactFrequently Asked QuestionsJoin The DiscordBugs & Feature RequestsTerms & Privacy