banananas
banananas
Explore posts from servers
MModular
Created by banananas on 10/23/2024 in #questions
Creating files from code
Is there a way to create a file from code? I've tried using a python script:
def create_proj(path: str):
with open(path + "/main.mojo", "x") as f:
f.write("text goes here")
def create_proj(path: str):
with open(path + "/main.mojo", "x") as f:
f.write("text goes here")
then:
from python import *

fn create_proj(folder: String) raises:
Python.add_to_path(".")
var file = Python.import_module("createproj")
file.create_proj(folder)
from python import *

fn create_proj(folder: String) raises:
Python.add_to_path(".")
var file = Python.import_module("createproj")
file.create_proj(folder)
but I have been getting this error:
Unhandled exception caught during execution: module 'createproj' has no attribute 'create_proj'
/home/[USERNAME]/pyregl/.magic/envs/default/bin/mojo: error: execution exited with a non-zero result: 1
Unhandled exception caught during execution: module 'createproj' has no attribute 'create_proj'
/home/[USERNAME]/pyregl/.magic/envs/default/bin/mojo: error: execution exited with a non-zero result: 1
10 replies
DDeno
Created by banananas on 10/11/2024 in #help
Getting the width and height of an image
I'm trying to get the width and height of an image but I'm not sure how to do so. Could anyone tell me how?
5 replies
DDeno
Created by banananas on 10/7/2024 in #help
(Littledivy's SDL2 bindings) textures not loading properly
I'm using this script:
// main.ts
import {WindowBuilder, EventType, Rect} from "sdl";
import img from "./utils/img.ts";

export const winScale = 4;

const windowbuilder: WindowBuilder = new WindowBuilder("UNDERTALE Twilight", 332 * winScale, 259 * winScale)
.highPixelDensity()
.borderless()
.transparent()

const window = windowbuilder.build()
export const canvas = window.canvas();
export const cAnchor = [6, 11];

while (true) {
// input
for await (const event of window.events()) {
switch (event.type) {
case EventType.Quit:
Deno.exit(0);
break;
}
}

// render
canvas.clear();
const wintex = img("./window.png", canvas.textureCreator());
canvas.copy(wintex, new Rect(0, 0, 332, 259), new Rect(0, 0, 332, 259));
canvas.present();
}
// main.ts
import {WindowBuilder, EventType, Rect} from "sdl";
import img from "./utils/img.ts";

export const winScale = 4;

const windowbuilder: WindowBuilder = new WindowBuilder("UNDERTALE Twilight", 332 * winScale, 259 * winScale)
.highPixelDensity()
.borderless()
.transparent()

const window = windowbuilder.build()
export const canvas = window.canvas();
export const cAnchor = [6, 11];

while (true) {
// input
for await (const event of window.events()) {
switch (event.type) {
case EventType.Quit:
Deno.exit(0);
break;
}
}

// render
canvas.clear();
const wintex = img("./window.png", canvas.textureCreator());
canvas.copy(wintex, new Rect(0, 0, 332, 259), new Rect(0, 0, 332, 259));
canvas.present();
}
With this script:
// utils/img.ts
import { Surface, Texture, TextureCreator } from "sdl";

export default function img(
image: string,
creator: TextureCreator,
): Texture {
const trackSurface = Surface.fromFile(image);
return creator.createTextureFromSurface(trackSurface);
}
// utils/img.ts
import { Surface, Texture, TextureCreator } from "sdl";

export default function img(
image: string,
creator: TextureCreator,
): Texture {
const trackSurface = Surface.fromFile(image);
return creator.createTextureFromSurface(trackSurface);
}
And using deno run --unstable-ffi --allow-ffi --allow-env main.ts (I'm pretty sure) this should render window.png at position 0, 0 on the screen, however it doesn't do that. On another note, the .transparent() method on the WindowBuilder class doesn't seem to actually make the window transparent. Could anyone help me to fix this issue? Thanks in advance.
18 replies
DDeno
Created by banananas on 10/6/2024 in #help
[SOLVED] dlopen failing
When I try to use Deno.dlopen() like this:
Deno.dlopen("./SDL2.dll", {} as const);
Deno.dlopen("./SDL2.dll", {} as const);
I get the following error:
error: Uncaught (in promise) TypeError: Deno.dlopen is not a function
Deno.dlopen("./SDL2.dll",{} as const);
^
error: Uncaught (in promise) TypeError: Deno.dlopen is not a function
Deno.dlopen("./SDL2.dll",{} as const);
^
Does anyone know why this could be happenning and how I could solve it? Thanks in advance.
4 replies
MModular
Created by banananas on 10/1/2024 in #questions
Using structs that inherit from a specific trait as a type?
If you have a trait and a struct that inherits from it, could you use that trait's children as a type in, for example, a list? E.g.:
trait MyTrait:
fn __init__(inout self): ...

struct MyStruct(MyTrait):
fn __init__(inout self): pass

fn main():
let my_list = List[structs that inherit from MyTrait](MyStruct())
trait MyTrait:
fn __init__(inout self): ...

struct MyStruct(MyTrait):
fn __init__(inout self): pass

fn main():
let my_list = List[structs that inherit from MyTrait](MyStruct())
15 replies
MModular
Created by banananas on 9/28/2024 in #questions
Calling methods from list indexes
When I try running update from here:
alias CptList = List[ComponentList]

struct Entity:
var components: CptList

fn __init__(inout self, components: CptList):
self.components = components

fn update(inout self):
for i in range(self.components.__len__()):
self.components[i].update()

fn addCpt(inout self, value: ComponentList):
self.components.append(value)
alias CptList = List[ComponentList]

struct Entity:
var components: CptList

fn __init__(inout self, components: CptList):
self.components = components

fn update(inout self):
for i in range(self.components.__len__()):
self.components[i].update()

fn addCpt(inout self, value: ComponentList):
self.components.append(value)
It returns this error:
error: 'Variant[BaseComponent]' value has no attribute 'update'
self.components[i].update()
~~~~~~~~~~~~~~~~~~^
error: 'Variant[BaseComponent]' value has no attribute 'update'
self.components[i].update()
~~~~~~~~~~~~~~~~~~^
ComponentList is defined here:
alias ComponentList = Variant[
BaseComponent,
]
alias ComponentList = Variant[
BaseComponent,
]
BaseComponent is defined here:
@value
struct BaseComponent: # example
fn __init__(inout self): print("hi!!!!!")

fn update(inout self): print("update method")
fn render(inout self): pass
@value
struct BaseComponent: # example
fn __init__(inout self): print("hi!!!!!")

fn update(inout self): print("update method")
fn render(inout self): pass
18 replies
MModular
Created by banananas on 9/25/2024 in #questions
Assigning additional traits to Variants
Is there a way to assign a trait to the Variant type? I'm specifically trying to use the __str__ method with List[Variant[types..]] specifically.
6 replies
MModular
Created by banananas on 9/23/2024 in #questions
"Linking" structs together?
I'm not talking about inheritance from other structs, but is there a way to refer to other structs as well when referring to one of them? For example:
var thing = MyStruct()
var other_thing = MyOtherStruct()
var list_of_things = List[MyStruct](thing, other_thing) # this is valid code since MyOtherStruct is linked to MyStruct
var thing = MyStruct()
var other_thing = MyOtherStruct()
var list_of_things = List[MyStruct](thing, other_thing) # this is valid code since MyOtherStruct is linked to MyStruct
44 replies
MModular
Created by banananas on 9/23/2024 in #questions
Is there a Struct type or something similar?
I'm trying to create a Set / List which can hold structs, but I'm not sure how to initialise this sort of type.
21 replies
MModular
Created by banananas on 9/21/2024 in #questions
Using the .append() method on structs
I have the following code:
alias Component = fn() -> None

fn baseComponent(): pass

struct Entity():
var components: List[Component]

fn __init__(inout self):
self.components = List[Component]()

fn add(self, method: Component):
self.components.append(method)
alias Component = fn() -> None

fn baseComponent(): pass

struct Entity():
var components: List[Component]

fn __init__(inout self):
self.components = List[Component]()

fn add(self, method: Component):
self.components.append(method)
However, line 12 gives an error even though I think it should be valid. What is the proper syntax in this case?
10 replies
MModular
Created by banananas on 9/15/2024 in #questions
Setting up SDL2 with Mojo on WSL
SDL is known for being notoriously hard to set up, and I only have experience with doing it in VS using NuGet (which is frankly a lot simpler). Does anybody know how to do it with Mojo and DLHandle?
5 replies
MModular
Created by banananas on 9/13/2024 in #questions
Removing a mojo project
Is there a way to remove a mojo project using the magic tool?
magic remove
magic remove
in the directory doesn't seem to do anything. Thanks in advance.
6 replies
MModular
Created by banananas on 9/13/2024 in #questions
Importing python modules
Whenever I try to import a python module using
import ctypes
import ctypes
(ctypes used as an example) VSCode gives me an error saying the module is inaccessible, and running mojo [file] says the same thing. I've checked and Python 3 is installed on both Windows 11 and WSL. (Using WSL) Thanks in advance.
4 replies
MModular
Created by banananas on 9/13/2024 in #questions
Adding a .mojo or .🔥file to a magic project
I've created a project using
magic init hello-world --format mojo-project
cd hello-world && magic shell
magic init hello-world --format mojo-project
cd hello-world && magic shell
but how could I add a file to this project / view its directory? (Using WSL Ubuntu) Thanks in advance.
5 replies
MModular
Created by banananas on 9/11/2024 in #questions
Installing Mojo on WSL?
I've got WSL downloaded and I have been trying to install Mojo, but all the tutorials that I find seem to be outdated, and running mojo commands isn't possible after installing Magic. Can someone tell me how to install Mojo with WSL?
7 replies
DDeno
Created by banananas on 9/8/2024 in #help
Can't install Deno on Windows
I'm trying to install deno using PowerShell using the command provided in the Deno installation guide (irm https://deno.land/install.ps1 | iex). However, when I run this command, I get the following error:
Remove-Item : Cannot find path 'C:\Users\piotr\.deno\bin\deno.zip' because it does not exist.
At line:39 char:1
+ Remove-Item $DenoZip
+ ~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (C:\Users\piotr\.deno\bin\deno.zip:String) [Remove-Item], ItemNotFoundEx
ception
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.RemoveItemCommand
Remove-Item : Cannot find path 'C:\Users\piotr\.deno\bin\deno.zip' because it does not exist.
At line:39 char:1
+ Remove-Item $DenoZip
+ ~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (C:\Users\piotr\.deno\bin\deno.zip:String) [Remove-Item], ItemNotFoundEx
ception
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.RemoveItemCommand
Can someone help me as to why this happens?
2 replies